diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/debug.py | 2 | ||||
-rw-r--r-- | src/xbackup/backup.py | 100 | ||||
-rw-r--r-- | src/xbackup/config.py | 2 | ||||
-rw-r--r-- | src/xbackup/script.py | 37 |
4 files changed, 134 insertions, 7 deletions
diff --git a/src/debug.py b/src/debug.py index f075e25..6daa264 100644 --- a/src/debug.py +++ b/src/debug.py @@ -1,5 +1,5 @@ #!/usr/bin/python3 -# vim: shiftwidth=4 tabstop=4 expandtab +# vim: shiftwidth=4 softtabstop=4 tabstop=4 expandtab """A Debug Script for Development Purposes""" diff --git a/src/xbackup/backup.py b/src/xbackup/backup.py new file mode 100644 index 0000000..b56e0cc --- /dev/null +++ b/src/xbackup/backup.py @@ -0,0 +1,100 @@ +#!/usr/bin/python3 +# vim: shiftwidth=4 softtabstop=4 tabstop=4 expandtab + + +"""Backup Functionality of xbackup""" + + +import os +import sys +import subprocess + + +def backup(config): + """perform a file-based full system backup""" + + # generate all necessary paths based on config + paths = Filepaths(config) + print(paths) + + # init borg repository (accepting failure on already existing repo) + shell("borg init -e none {}".format(paths.borg_repo), panic=False) + + # run backup + command = "borg create -v --stats --compression zstd \\" + command += """ + {repo}::'{{hostname}}-{{user}}-{{now:%Y-%m-%d_%H:%M:%S}}' \\""".format( + repo=paths.borg_repo + ) + command += "\n {} \\".format(paths.backup_source) + for key,value in enumerate(paths.blacklist): + command += "\n --exclude '{}'".format(value) + if key != len(paths.blacklist) -1: + command += " \\" + + shell(command) + + +class Filepaths(object): + """A Container Class to hold Filepaths for the Borg Backup Tool""" + + def __init__(self, config): + """The Constructor""" + + # parse backup source (usually / for system backups) + self.backup_source = config["backup_source"] + + # + self.borg_repos_folder = config["borg_repos_folder"] + + # path to the borg repository + hostname = os.uname()[1] + self.borg_repo = os.path.join(self.borg_repos_folder, hostname) + + # create blacklist of folders to be excluded from the backup + self.blacklist = [] + for entry in config["blacklist"]: + self.blacklist.append(os.path.join(self.backup_source, entry)) + + # avoid matroska-style backups of backups (yes, it is important) + self.blacklist.append(self.borg_repos_folder) + + def __str__(self): + """Converts an Object based on this Class to a String""" + + retval = "" + + retval += "\nbackup_source:\n" + self.backup_source + "\n" + + retval += "\nblacklist:\n" + for item in self.blacklist: + retval += item + "\n" + + retval += "\nborg_repo:\n" + self.borg_repo + "\n" + + return retval + + +def shell(command, panic=True): + """Savely execute a Shell Command + + - set panic=False to continue with execution on non-zero return code + """ + + # print command + print("\nExecuting '" + command + "' ...") + + # command execution + return_code = subprocess.call(command, shell=True) + + # handle non-zero return code + if return_code != 0 and panic: + print("Command '{}'\nfailed with return code {}".format( + command, + str(return_code) + )) + sys.exit(return_code) + + # final message and return + print("... done!") + return diff --git a/src/xbackup/config.py b/src/xbackup/config.py index 1e115a5..4c72915 100644 --- a/src/xbackup/config.py +++ b/src/xbackup/config.py @@ -1,5 +1,5 @@ #!/usr/bin/python3 -# vim: shiftwidth=4 tabstop=4 expandtab +# vim: shiftwidth=4 softtabstop=4 tabstop=4 expandtab """Module for Configuration Parsing Functionality of xbackup""" diff --git a/src/xbackup/script.py b/src/xbackup/script.py index b69da00..c8e619b 100644 --- a/src/xbackup/script.py +++ b/src/xbackup/script.py @@ -1,5 +1,5 @@ #!/usr/bin/python3 -# vim: shiftwidth=4 tabstop=4 expandtab +# vim: shiftwidth=4 softtabstop=4 tabstop=4 expandtab """Module for the Script Functionality of xbackup""" @@ -10,6 +10,7 @@ import time import sys from xbackup import config +from xbackup import backup def run(): @@ -18,15 +19,16 @@ def run(): args = parse_arguments() cfg = config.get(args.config) + welcome() + if args.command == "backup": - print("Performing dummy backup based on '{}'".format(args.config)) - print("Config is:\n{}".format(config.dump(cfg))) - time.sleep(1) - print("Done!") + backup.backup(cfg["backup"]) else: print("Unknown command '{}'".format(args.command)) sys.exit(1) + bye() + def parse_arguments(): """handles argument parsing with the argparse module""" @@ -53,3 +55,28 @@ def parse_arguments(): # argument parsing args = parser.parse_args() return args + + +def welcome(): + """Print Welcome Text""" + + print(""" +################################################################################ + _ _ + __ _| |__ __ _ ___| | ___ _ _ __ + \ \/ / '_ \ / _` |/ __| |/ / | | | '_ \ + > <| |_) | (_| | (__| <| |_| | |_) | + /_/\_\_.__/ \__,_|\___|_|\_\\\\__,_| .__/ + |_| + +################################################################################ +""", end="") + + +def bye(): + """Print final Text""" + + print(""" +################################################################################ + +""", end="") |