From 56df958c5bf568589105305f7afd31de97143a0c Mon Sep 17 00:00:00 2001 From: Fuxino Date: Sun, 4 Jun 2023 12:09:30 +0200 Subject: [PATCH] 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')