From d6d9fbf6e4a48063a3a7cc46ce7d14e9bf948aad Mon Sep 17 00:00:00 2001 From: Fuxino Date: Sun, 4 Jun 2023 10:16:50 +0200 Subject: [PATCH 1/2] Add no-syslog option --- man/simple_backup.1 | 3 +++ simple_backup/simple_backup.py | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/man/simple_backup.1 b/man/simple_backup.1 index c37cc03..9c5f125 100644 --- a/man/simple_backup.1 +++ b/man/simple_backup.1 @@ -56,6 +56,9 @@ Same as rsync option \(aq\-\-checksum\(aq, use checksums instead of mod\-time an .B \-\-remove\-before\-backup Remove old backups (if necessary) before creating the new backup. Useful to free some space before performing the backup. Default behavior is to remove old backups after successfully completing the backup. +.TP +.B \-\-no\-syslog +Don't use systemd journal for logging .SH CONFIGURATION An example configuration file is provided at \(aq/etc/simple_backup/simple_backup.conf\(aq. Copy it to the default location ($HOME/.config/simple_backup) and edit it as needed. diff --git a/simple_backup/simple_backup.py b/simple_backup/simple_backup.py index 191e3d5..df9f24a 100755 --- a/simple_backup/simple_backup.py +++ b/simple_backup/simple_backup.py @@ -252,6 +252,7 @@ def _parse_arguments(): help='Use checksum rsync option to compare files (MUCH SLOWER)') parser.add_argument('--remove-before-backup', action='store_true', help='Remove old backups before executing the backup, instead of after') + parser.add_argument('--no-syslog', action='store_true', help='Disable systemd journal logging') args = parser.parse_args() @@ -297,6 +298,13 @@ def notify(text): def simple_backup(): args = _parse_arguments() + + if args.no_syslog: + try: + logger.removeHandler(j_handler) + except NameError: + pass + inputs, output, exclude, keep = _read_config(args.config) if args.input is not None: From 56df958c5bf568589105305f7afd31de97143a0c Mon Sep 17 00:00:00 2001 From: Fuxino Date: Sun, 4 Jun 2023 12:09:30 +0200 Subject: [PATCH 2/2] Add expansion of params in config file Allow using wildcards (i.e. * to match any character and ~ to match the user's home directory) in inputs and ouput variables in config file --- simple_backup/simple_backup.conf | 14 +++++++------- simple_backup/simple_backup.py | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/simple_backup/simple_backup.conf b/simple_backup/simple_backup.conf index 6adc9a4..3afb3be 100644 --- a/simple_backup/simple_backup.conf +++ b/simple_backup/simple_backup.conf @@ -1,14 +1,14 @@ -#Example config file for simple_backup +# Example config file for simple_backup [default] -#Input directories. Use a comma to separate items +# Files and directories to backup. Multiple items can be separated using a comma (','). It is possible to use wildcards (i.e. '*' to match multiple characters and '~' for the user's home directory). inputs=/home/my_home,/etc -#Output directory +# Output directory. backup_dir=/media/Backup -#Exclude patterns. Use a comma to separate items -exclude=.gvfs,.local/share/gvfs-metadata,.cache,.dbus,.Trash,.local/share/Trash,.macromedia,.adobe,.recently-used,.recently-used.xbel,.thumbnails +# Files, directories and patterns to exclude from the backup. Multiple items can be separated using a comma. +exclude=*.bak -#Number of snapshots to keep (use -1 to keep all) -keep=-1 \ No newline at end of file +# Number of old backups (i.e. excluding the one that's being created) to keep (use -1 to keep all) +keep=-1 diff --git a/simple_backup/simple_backup.py b/simple_backup/simple_backup.py index df9f24a..e9ef4f0 100755 --- a/simple_backup/simple_backup.py +++ b/simple_backup/simple_backup.py @@ -12,8 +12,11 @@ from timeit import default_timer from subprocess import Popen, PIPE, STDOUT from datetime import datetime from tempfile import mkstemp +from glob import glob + from dotenv import load_dotenv + try: from systemd import journal except ImportError: @@ -32,6 +35,7 @@ if euid == 0: user = os.getenv('SUDO_USER') homedir = os.path.expanduser(f'~{user}') else: + user = os.getenv('USER') homedir = os.getenv('HOME') logging.getLogger().setLevel(logging.DEBUG) @@ -259,6 +263,20 @@ def _parse_arguments(): return args +def _expand_inputs(inputs): + expanded_inputs = [] + + for i in inputs: + i_ex = glob(os.path.expanduser(i.replace('~', f'~{user}'))) + + if len(i_ex) == 0: + logger.warning(f'No file or directory matching input {i}. Skipping...') + else: + expanded_inputs.extend(glob(os.path.expanduser(i.replace('~', f'~{user}')))) + + return expanded_inputs + + def _read_config(config_file): if not os.path.isfile(config_file): logger.warning(f'Config file {config_file} does not exist') @@ -270,7 +288,10 @@ def _read_config(config_file): inputs = config.get('default', 'inputs') inputs = inputs.split(',') + inputs = _expand_inputs(inputs) + inputs = list(set(inputs)) output = config.get('default', 'backup_dir') + output = os.path.expanduser(output.replace('~', f'~{user}')) exclude = config.get('default', 'exclude') exclude = exclude.split(',') keep = config.getint('default', 'keep')