2019-02-07 23:19:21 -08:00
|
|
|
#!/usr/bin/python3
|
2019-03-13 02:19:20 -07:00
|
|
|
"""
|
2019-04-14 21:01:29 -07:00
|
|
|
.. moduleauthor:: Eric Torres
|
|
|
|
|
2019-03-13 02:19:20 -07:00
|
|
|
Command-Line Arguments
|
|
|
|
======================
|
2019-04-14 21:01:29 -07:00
|
|
|
|
2019-04-17 21:58:01 -07:00
|
|
|
-c, --use-checksums use rsync's checksum feature to detect file changes
|
|
|
|
--debug show debug messages
|
|
|
|
-n, --name name to give to the backup snapshot
|
|
|
|
-s, --run-post-sync run sync syscall after backup
|
2019-04-23 21:53:20 -07:00
|
|
|
-u, --umask umask value to use while running backup process
|
2019-03-17 18:27:20 -07:00
|
|
|
|
2019-04-28 22:17:03 -07:00
|
|
|
Run a local backup, creating a snapshot in the process.
|
2019-04-23 23:18:46 -07:00
|
|
|
|
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-03-13 02:19:20 -07:00
|
|
|
|
2019-04-19 23:57:13 -07:00
|
|
|
import rbackup.config.config_files as config
|
2019-04-27 10:13:10 -07:00
|
|
|
import rbackup.logging
|
2019-04-23 22:47:05 -07:00
|
|
|
import rbackup.script as script
|
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-13 20:43:12 -07:00
|
|
|
EXTRA_RSYNC_OPTS = {
|
|
|
|
"delete": "--delete-after",
|
|
|
|
"checksum": "--checksum",
|
|
|
|
"update": "--update",
|
|
|
|
}
|
2019-03-13 02:19:20 -07:00
|
|
|
|
2019-04-28 22:20:15 -07:00
|
|
|
DESCRIPTION = "Run a backup on a repository within a local path"
|
|
|
|
|
2019-03-17 19:16:21 -07:00
|
|
|
# ----- Error Codes -----
|
2019-04-16 21:28:17 -07:00
|
|
|
E_PERMISSION = 13
|
2019-03-17 18:59:02 -07:00
|
|
|
|
2019-04-23 22:47:05 -07:00
|
|
|
|
2019-03-13 02:19:20 -07:00
|
|
|
# ========== Logging Setup ==========
|
2019-03-17 18:27:20 -07:00
|
|
|
syslog = logging.getLogger("rbackup")
|
2019-03-13 20:43:12 -07:00
|
|
|
syslog.setLevel(logging.DEBUG)
|
|
|
|
|
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-04-28 22:20:15 -07:00
|
|
|
parser = argparse.ArgumentParser(description=DESCRIPTION)
|
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-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",
|
|
|
|
)
|
2019-04-23 22:47:40 -07:00
|
|
|
parser.add_argument(
|
|
|
|
"-u",
|
|
|
|
"--umask",
|
|
|
|
type=int,
|
|
|
|
default=None,
|
|
|
|
help="umask value to use while running backup process",
|
2019-04-23 21:53:20 -07:00
|
|
|
)
|
2019-03-17 18:27:20 -07:00
|
|
|
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-16 17:12:06 -07:00
|
|
|
# ========== Main Script ==========
|
|
|
|
if __name__ == "__main__":
|
2019-04-10 19:21:00 -07:00
|
|
|
args = parse_cmdline_arguments()
|
2019-04-17 19:01:38 -07:00
|
|
|
|
2019-04-27 10:13:10 -07:00
|
|
|
stdout_handler, stderr_handler = rbackup.logging.retrieve_console_handlers(
|
|
|
|
debug=args.debug
|
|
|
|
)
|
|
|
|
syslog.addHandler(stdout_handler)
|
|
|
|
syslog.addHandler(stderr_handler)
|
2019-04-24 09:19:31 -07:00
|
|
|
|
2019-04-28 22:17:03 -07:00
|
|
|
dest = args.repository
|
2019-04-23 23:17:42 -07:00
|
|
|
|
2019-04-16 21:28:17 -07:00
|
|
|
try:
|
2019-04-23 22:47:05 -07:00
|
|
|
parsed_config = config.parse_configfile()
|
2019-04-23 23:17:42 -07:00
|
|
|
repo = Repository(dest)
|
2019-04-16 21:28:17 -07:00
|
|
|
except PermissionError as e:
|
|
|
|
syslog.critical(e)
|
|
|
|
exit(E_PERMISSION)
|
2019-04-23 22:47:05 -07:00
|
|
|
else:
|
|
|
|
script.do_backup(repo, parsed_config, args)
|
2019-04-10 19:21:00 -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()
|