2019-02-07 23:19:21 -08:00
|
|
|
#!/usr/bin/python3
|
2019-03-13 02:19:20 -07:00
|
|
|
"""
|
2019-04-10 19:21:00 -07:00
|
|
|
Run a backup, creating a snapshot in the process.
|
2019-03-13 02:19:20 -07:00
|
|
|
|
|
|
|
Command-Line Arguments
|
|
|
|
======================
|
2019-03-17 18:27:20 -07:00
|
|
|
* -c, --use-checksums use rsync's checksum feature to detect file changes
|
|
|
|
* -d, --dry-run make this backup a dry run
|
2019-04-10 19:21:00 -07:00
|
|
|
* --debug show debug messages
|
|
|
|
* -n, --name name to give to the backup snapshot
|
2019-03-17 18:27:20 -07:00
|
|
|
* -s, --run-post-sync run sync syscall after backup
|
2019-04-10 19:21:00 -07:00
|
|
|
* -v, --verbose show info messages
|
2019-03-17 18:27:20 -07:00
|
|
|
|
|
|
|
On each run of this script, a new snapshot is made and any unchanged
|
2019-04-10 19:21:00 -07:00
|
|
|
files are hardlinked into the new snapshot.
|
2019-03-13 02:19:20 -07:00
|
|
|
"""
|
|
|
|
import argparse
|
|
|
|
import logging
|
2019-03-17 18:27:20 -07:00
|
|
|
import os
|
2019-04-12 10:04:18 -07:00
|
|
|
import re
|
2019-03-17 18:27:20 -07:00
|
|
|
import sys
|
2019-04-12 10:04:18 -07:00
|
|
|
from contextlib import contextmanager
|
|
|
|
from pathlib import Path
|
2019-04-10 19:21:00 -07:00
|
|
|
from subprocess import CalledProcessError
|
2019-04-12 10:04:18 -07:00
|
|
|
from tempfile import NamedTemporaryFile
|
2019-03-13 02:19:20 -07:00
|
|
|
|
2019-03-17 18:27:20 -07:00
|
|
|
from rbackup.rsync import rsync
|
2019-04-10 19:21:00 -07:00
|
|
|
from rbackup.struct.repository import Repository
|
2019-03-13 02:19:20 -07:00
|
|
|
|
|
|
|
# ========== Constants ==========
|
2019-03-17 18:27:20 -07:00
|
|
|
LOGFORMAT = "==> %(levelname)s %(message)s"
|
2019-03-13 02:19:20 -07:00
|
|
|
RSYNC_DEFAULT_OPTS = [
|
2019-03-17 18:27:20 -07:00
|
|
|
"--acls",
|
2019-03-13 02:19:20 -07:00
|
|
|
"--archive",
|
2019-03-17 18:27:20 -07:00
|
|
|
"--backup",
|
2019-04-10 19:21:00 -07:00
|
|
|
"--backup-dir=backup",
|
2019-03-13 02:19:20 -07:00
|
|
|
"--hard-links",
|
2019-03-17 18:27:20 -07:00
|
|
|
"--ignore-missing-args",
|
2019-03-17 18:59:02 -07:00
|
|
|
"--prune-empty-dirs",
|
2019-03-17 18:27:20 -07:00
|
|
|
"--suffix=.old",
|
2019-04-10 20:20:50 -07:00
|
|
|
"--recursive",
|
2019-03-13 20:43:12 -07:00
|
|
|
"--xattrs",
|
2019-03-13 02:19:20 -07:00
|
|
|
]
|
2019-03-13 20:43:12 -07:00
|
|
|
EXTRA_RSYNC_OPTS = {
|
|
|
|
"dry_run": "--dry-run",
|
|
|
|
"delete": "--delete-after",
|
|
|
|
"checksum": "--checksum",
|
|
|
|
"update": "--update",
|
|
|
|
}
|
2019-03-13 02:19:20 -07:00
|
|
|
|
2019-04-10 19:21:00 -07:00
|
|
|
# ----- File Options -----
|
2019-03-17 19:16:21 -07:00
|
|
|
CONFIG_DIR = "/etc/rbackup"
|
2019-04-12 10:04:18 -07:00
|
|
|
FILE_OPTS = [f"--exclude-from={CONFIG_DIR}/home-exclude.conf"]
|
|
|
|
INCLUDE_PATHS = [f"{CONFIG_DIR}/etc-include.conf", "{CONFIG_DIR}/system-include.conf"]
|
|
|
|
COMMENT_REGEX = r"^[^#; ]+"
|
2019-04-10 19:21:00 -07:00
|
|
|
|
2019-03-17 18:27:20 -07:00
|
|
|
|
2019-03-17 19:16:21 -07:00
|
|
|
# ----- Error Codes -----
|
2019-03-17 18:59:02 -07:00
|
|
|
E_FAILED_PROCESS = 1
|
2019-04-10 19:21:00 -07:00
|
|
|
E_INVALID_SNAPSHOT_NAME = 2
|
2019-03-17 18:59:02 -07:00
|
|
|
|
2019-03-13 02:19:20 -07:00
|
|
|
# ========== Logging Setup ==========
|
2019-03-13 20:43:12 -07:00
|
|
|
console_formatter = logging.Formatter(LOGFORMAT)
|
2019-03-17 18:27:20 -07:00
|
|
|
syslog = logging.getLogger("rbackup")
|
2019-03-13 20:43:12 -07:00
|
|
|
syslog.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
stdout_handler = logging.StreamHandler(sys.stdout)
|
|
|
|
stdout_handler.setLevel(logging.INFO)
|
|
|
|
stdout_handler.setFormatter(console_formatter)
|
|
|
|
stdout_handler.addFilter(lambda record: record.levelno <= logging.INFO)
|
|
|
|
|
|
|
|
stderr_handler = logging.StreamHandler(sys.stderr)
|
|
|
|
stderr_handler.setLevel(logging.WARNING)
|
|
|
|
stderr_handler.setFormatter(console_formatter)
|
|
|
|
|
|
|
|
syslog.addHandler(stdout_handler)
|
|
|
|
syslog.addHandler(stderr_handler)
|
2019-03-13 02:19:20 -07:00
|
|
|
|
2019-03-17 18:27:20 -07:00
|
|
|
|
|
|
|
# ========== Functions ==========
|
2019-04-12 08:12:35 -07:00
|
|
|
def parse_cmdline_arguments(**kwargs):
|
2019-04-10 18:08:36 -07:00
|
|
|
"""Parse command line arguments passed to the script.
|
2019-04-12 08:12:35 -07:00
|
|
|
All kwargs are passed to ArgumentParser.parse_args().
|
2019-03-17 19:16:21 -07:00
|
|
|
|
2019-04-10 18:08:36 -07:00
|
|
|
:rtype: argparse.Namespace object
|
2019-03-17 19:16:21 -07:00
|
|
|
"""
|
2019-03-13 02:19:20 -07:00
|
|
|
parser = argparse.ArgumentParser()
|
2019-03-13 20:43:12 -07:00
|
|
|
parser.add_argument(
|
|
|
|
"-c",
|
|
|
|
"--use-checksums",
|
|
|
|
action="store_const",
|
|
|
|
dest="extra_rsync_opts",
|
|
|
|
const=EXTRA_RSYNC_OPTS["checksum"],
|
2019-03-17 18:27:20 -07:00
|
|
|
help="use rsync's checksumming feature to look for changed files",
|
2019-03-13 20:43:12 -07:00
|
|
|
)
|
2019-03-13 02:19:20 -07:00
|
|
|
parser.add_argument(
|
|
|
|
"-d",
|
|
|
|
"--dry-run",
|
|
|
|
action="append_const",
|
|
|
|
dest="extra_rsync_opts",
|
|
|
|
const=EXTRA_RSYNC_OPTS["dry_run"],
|
|
|
|
help="pass --dry-run to rsync",
|
|
|
|
)
|
2019-04-10 19:21:00 -07:00
|
|
|
parser.add_argument("--debug", action="store_true", help="log debug messages")
|
|
|
|
parser.add_argument(
|
|
|
|
"-n", "--name", default=None, help="name to give to the snapshot"
|
|
|
|
)
|
2019-03-13 20:43:12 -07:00
|
|
|
parser.add_argument(
|
2019-03-17 18:27:20 -07:00
|
|
|
"-s",
|
|
|
|
"--run-post-sync",
|
|
|
|
action="store_true",
|
|
|
|
help="run sync operation after backup is complete",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2019-04-10 19:21:00 -07:00
|
|
|
"-v", "--verbose", action="store_true", help="log info messages"
|
2019-03-13 20:43:12 -07:00
|
|
|
)
|
2019-04-10 19:21:00 -07:00
|
|
|
parser.add_argument("repository", help="repository to back up to", metavar="repo")
|
2019-03-13 02:19:20 -07:00
|
|
|
|
2019-04-12 08:12:35 -07:00
|
|
|
return parser.parse_args(**kwargs)
|
2019-03-13 02:19:20 -07:00
|
|
|
|
2019-03-17 18:27:20 -07:00
|
|
|
|
2019-04-12 10:04:18 -07:00
|
|
|
@contextmanager
|
|
|
|
def merge_files(*config_files):
|
|
|
|
"""Parse, filter, and sort through config files to create a single
|
|
|
|
--files-from argument.
|
|
|
|
|
|
|
|
Any files included that do not exist send a warning to the log.
|
|
|
|
|
|
|
|
>>> merge_files('/etc/rbackup/etc-include.conf', '/etc/rbackup/system-include.conf') # doctest: +ELLIPSIS
|
|
|
|
>>> '/tmp/...'
|
|
|
|
|
|
|
|
:param config_files: files including paths to read from
|
|
|
|
:type config_files: iterable of str
|
|
|
|
:returns: path to file that lists include paths
|
|
|
|
:rtype: str
|
|
|
|
"""
|
|
|
|
file_paths = [Path(p) for p in config_files]
|
|
|
|
include_lines = []
|
|
|
|
|
|
|
|
for config_file in file_paths:
|
|
|
|
if config_file.exists():
|
|
|
|
with config_file.open(mode="r") as opened_file:
|
|
|
|
include_lines.extend(
|
|
|
|
l for l in opened_file.readlines() if re.match(COMMENT_REGEX, l)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
syslog.warning(f"{config_file} does not exist, ignoring")
|
|
|
|
|
|
|
|
include_lines.sort()
|
|
|
|
|
|
|
|
with NamedTemporaryFile(mode="w", delete=False) as include_paths:
|
|
|
|
include_paths.writelines(include_lines)
|
|
|
|
|
|
|
|
tmpfile = Path(include_paths.name)
|
|
|
|
yield tmpfile
|
|
|
|
tmpfile.unlink()
|
|
|
|
|
|
|
|
|
2019-04-10 19:21:00 -07:00
|
|
|
# ========== Main Script ==========
|
|
|
|
if __name__ == "__main__":
|
|
|
|
args = parse_cmdline_arguments()
|
2019-03-17 18:27:20 -07:00
|
|
|
repo = Repository(args.repository)
|
2019-03-13 02:19:20 -07:00
|
|
|
|
2019-04-10 19:21:00 -07:00
|
|
|
rsync_opts = RSYNC_DEFAULT_OPTS.copy()
|
|
|
|
|
|
|
|
if args.extra_rsync_opts is not None:
|
|
|
|
rsync_opts.extend(args.extra_rsync_opts)
|
|
|
|
|
2019-03-13 20:43:12 -07:00
|
|
|
if args.verbose:
|
2019-04-10 19:21:00 -07:00
|
|
|
stdout_handler.setLevel(logging.INFO)
|
2019-03-13 20:43:12 -07:00
|
|
|
|
2019-04-10 19:21:00 -07:00
|
|
|
if args.debug:
|
|
|
|
stdout_handler.setLevel(logging.DEBUG)
|
2019-03-17 18:27:20 -07:00
|
|
|
|
2019-04-10 19:21:00 -07:00
|
|
|
# We want to iterate through the repository and create the --link-dest
|
|
|
|
# options before creating the new snapshot
|
|
|
|
link_dests = tuple(f"--link-dest={s.path}" for s in repo)
|
2019-03-17 18:27:20 -07:00
|
|
|
|
2019-04-10 19:21:00 -07:00
|
|
|
curr_snapshot = None
|
2019-03-17 18:27:20 -07:00
|
|
|
|
2019-03-17 18:59:02 -07:00
|
|
|
try:
|
2019-04-12 10:04:18 -07:00
|
|
|
with merge_files(INCLUDE_PATHS) as include_file:
|
|
|
|
curr_snapshot = repo.create_snapshot(args.name)
|
|
|
|
rsync(
|
|
|
|
*rsync_opts,
|
|
|
|
*FILE_OPTS,
|
|
|
|
f"--files-from={include_file}",
|
|
|
|
*link_dests,
|
|
|
|
"/",
|
|
|
|
str(curr_snapshot.path),
|
|
|
|
)
|
2019-04-10 19:21:00 -07:00
|
|
|
except ValueError as e:
|
|
|
|
syslog.critical(e)
|
|
|
|
exit(E_INVALID_SNAPSHOT_NAME)
|
2019-03-17 18:59:02 -07:00
|
|
|
except CalledProcessError as e:
|
2019-04-10 19:21:00 -07:00
|
|
|
syslog.critical("Backup process failed")
|
|
|
|
syslog.critical(f"Failing command: {e.cmd}")
|
2019-03-17 18:59:02 -07:00
|
|
|
exit(E_FAILED_PROCESS)
|
2019-03-17 18:27:20 -07:00
|
|
|
|
|
|
|
snapshot_symlink = repo.path / "current"
|
2019-03-13 20:43:12 -07:00
|
|
|
|
2019-03-17 18:27:20 -07:00
|
|
|
try:
|
|
|
|
snapshot_symlink.unlink()
|
2019-04-12 08:12:35 -07:00
|
|
|
snapshot_symlink.symlink_to(curr_snapshot, target_is_directory=True)
|
2019-03-17 18:27:20 -07:00
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2019-04-12 08:12:35 -07:00
|
|
|
except PermissionError as e:
|
|
|
|
syslog.error(e)
|
2019-03-13 02:19:20 -07:00
|
|
|
|
2019-03-17 18:27:20 -07:00
|
|
|
if args.run_post_sync:
|
2019-04-12 08:12:35 -07:00
|
|
|
syslog.info("Running sync operation")
|
2019-03-17 18:27:20 -07:00
|
|
|
os.sync()
|