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
This commit is contained in:
daniele 2023-06-04 12:09:30 +02:00
parent d6d9fbf6e4
commit 56df958c5b
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514
2 changed files with 28 additions and 7 deletions

View File

@ -1,14 +1,14 @@
#Example config file for simple_backup # Example config file for simple_backup
[default] [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 inputs=/home/my_home,/etc
#Output directory # Output directory.
backup_dir=/media/Backup backup_dir=/media/Backup
#Exclude patterns. Use a comma to separate items # Files, directories and patterns to exclude from the backup. Multiple items can be separated using a comma.
exclude=.gvfs,.local/share/gvfs-metadata,.cache,.dbus,.Trash,.local/share/Trash,.macromedia,.adobe,.recently-used,.recently-used.xbel,.thumbnails exclude=*.bak
#Number of snapshots to keep (use -1 to keep all) # Number of old backups (i.e. excluding the one that's being created) to keep (use -1 to keep all)
keep=-1 keep=-1

View File

@ -12,8 +12,11 @@ from timeit import default_timer
from subprocess import Popen, PIPE, STDOUT from subprocess import Popen, PIPE, STDOUT
from datetime import datetime from datetime import datetime
from tempfile import mkstemp from tempfile import mkstemp
from glob import glob
from dotenv import load_dotenv from dotenv import load_dotenv
try: try:
from systemd import journal from systemd import journal
except ImportError: except ImportError:
@ -32,6 +35,7 @@ if euid == 0:
user = os.getenv('SUDO_USER') user = os.getenv('SUDO_USER')
homedir = os.path.expanduser(f'~{user}') homedir = os.path.expanduser(f'~{user}')
else: else:
user = os.getenv('USER')
homedir = os.getenv('HOME') homedir = os.getenv('HOME')
logging.getLogger().setLevel(logging.DEBUG) logging.getLogger().setLevel(logging.DEBUG)
@ -259,6 +263,20 @@ def _parse_arguments():
return args 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): def _read_config(config_file):
if not os.path.isfile(config_file): if not os.path.isfile(config_file):
logger.warning(f'Config file {config_file} does not exist') 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 = config.get('default', 'inputs')
inputs = inputs.split(',') inputs = inputs.split(',')
inputs = _expand_inputs(inputs)
inputs = list(set(inputs))
output = config.get('default', 'backup_dir') output = config.get('default', 'backup_dir')
output = os.path.expanduser(output.replace('~', f'~{user}'))
exclude = config.get('default', 'exclude') exclude = config.get('default', 'exclude')
exclude = exclude.split(',') exclude = exclude.split(',')
keep = config.getint('default', 'keep') keep = config.getint('default', 'keep')