rbackup/bin/backup

180 lines
5.2 KiB
Plaintext
Raw Normal View History

2019-02-07 23:19:21 -08:00
#!/usr/bin/python3
2019-03-13 02:19:20 -07:00
"""
.. moduleauthor:: Eric Torres
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-04-17 21:58:01 -07:00
-c, --use-checksums use rsync's checksum feature to detect file changes
-d, --dry-run make this backup a dry run
--debug show debug messages
-n, --name name to give to the backup snapshot
2019-04-23 21:52:06 -07:00
-p, --port port that ssh on the destination is listening on
2019-04-17 21:58:01 -07:00
-s, --run-post-sync run sync syscall after backup
-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
import sys
from contextlib import contextmanager
2019-04-10 19:21:00 -07:00
from subprocess import CalledProcessError
2019-03-13 02:19:20 -07:00
import rbackup.config.config_files as config
2019-04-14 22:08:37 -07:00
import rbackup.rsync
2019-04-10 19:21:00 -07:00
from rbackup.struct.repository import Repository
2019-03-13 02:19:20 -07:00
# ========== Constants ==========
SCRIPT_UMASK = 0000
2019-03-17 18:27:20 -07:00
LOGFORMAT = "==> %(levelname)s %(message)s"
EXTRA_RSYNC_OPTS = {
"dry_run": "--dry-run",
"delete": "--delete-after",
"checksum": "--checksum",
"update": "--update",
}
2019-03-13 02:19:20 -07:00
2019-03-17 18:27:20 -07:00
# ----- Error Codes -----
2019-04-10 19:21:00 -07:00
E_INVALID_SNAPSHOT_NAME = 2
2019-04-16 21:28:17 -07:00
E_PERMISSION = 13
2019-03-17 18:59:02 -07:00
2019-03-13 02:19:20 -07:00
# ========== Logging Setup ==========
console_formatter = logging.Formatter(LOGFORMAT)
2019-03-17 18:27:20 -07:00
syslog = logging.getLogger("rbackup")
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):
"""Parse command line arguments passed to the script.
2019-04-12 08:12:35 -07:00
All kwargs are passed to ArgumentParser.parse_args().
:rtype: argparse.Namespace object
"""
2019-03-13 02:19:20 -07:00
parser = argparse.ArgumentParser()
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 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-04-23 21:52:06 -07:00
parser.add_argument(
"-p", "--port", default=22, help="port that ssh on the destination is listening on"
)
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-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
@contextmanager
def change_umask():
"""Creates a context manager in which the umask is changed. This is to ensure that
the script's desired umask is not visible to the user.
"""
try:
old_umask = os.umask(SCRIPT_UMASK)
yield
finally:
os.umask(old_umask)
# ========== Main Script ==========
if __name__ == "__main__":
2019-04-10 19:21:00 -07:00
args = parse_cmdline_arguments()
2019-04-14 22:08:37 -07:00
parsed_config = config.parse_configfile()
2019-04-16 21:28:17 -07:00
2019-04-17 19:01:38 -07:00
repo = None
2019-04-16 21:28:17 -07:00
try:
repo = Repository(args.repository)
except PermissionError as e:
syslog.critical(e)
exit(E_PERMISSION)
2019-03-13 02:19:20 -07:00
2019-04-14 22:17:16 -07:00
rsync_opts = config.load_list_from_option(
parsed_config,
section="main",
option="RsyncOptions",
fallback=rbackup.rsync.DEFAULT_RSYNC_OPTS.copy(),
2019-04-14 22:08:37 -07:00
)
2019-04-10 19:21:00 -07:00
if args.extra_rsync_opts is not None:
rsync_opts.extend(args.extra_rsync_opts)
if args.verbose:
2019-04-10 19:21:00 -07:00
stdout_handler.setLevel(logging.INFO)
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
with change_umask(), config.merge_include_files() as include_file, config.merge_exclude_files() as exclude_file:
try:
curr_snapshot = repo.create_snapshot(args.name)
2019-04-14 22:08:37 -07:00
rbackup.rsync.rsync(
*rsync_opts,
f"--files-from={include_file}",
2019-04-14 22:17:16 -07:00
f"--exclude-from={exclude_file}",
*link_dests,
"/",
str(curr_snapshot),
)
except ValueError as e:
syslog.critical(e)
exit(E_INVALID_SNAPSHOT_NAME)
except CalledProcessError as e:
syslog.critical("Backup process failed")
syslog.critical(f"Failing command: {e.cmd}")
syslog.critical(e.stderr)
exit(e.returncode)
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()