Merge branch 'master' into development

This commit is contained in:
2023-06-04 16:53:22 +02:00
3 changed files with 39 additions and 9 deletions

View File

@ -1,20 +1,20 @@
# Example config file for simple_backup
[backup]
# 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)
# Number of old backups (i.e. excluding the one that's being created) to keep (use -1 to keep all)
keep=-1
# Uncomment the following section to enable backup to remote server through ssh
#[server]
#host=
#username=
#ssh_keyfile=
# [server]
# host=
# username=
# ssh_keyfile=

View File

@ -26,6 +26,7 @@ from subprocess import Popen, PIPE, STDOUT
from datetime import datetime
from tempfile import mkstemp
from getpass import getpass
from glob import glob
from dotenv import load_dotenv
import paramiko
@ -52,6 +53,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)
@ -501,12 +503,27 @@ def _parse_arguments():
parser.add_argument('-z', '--compress', action='store_true', help='Compress data during the transfer')
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()
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('No file or directory matching input %s. Skipping...', i)
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('Config file %s does not exist', config_file)
@ -518,7 +535,10 @@ def _read_config(config_file):
inputs = config.get('backup', 'inputs')
inputs = inputs.split(',')
inputs = _expand_inputs(inputs)
inputs = list(set(inputs))
output = config.get('backup', 'backup_dir')
output = os.path.expanduser(output.replace('~', f'~{user}'))
exclude = config.get('backup', 'exclude')
exclude = exclude.split(',')
keep = config.getint('backup', 'keep')
@ -560,6 +580,13 @@ def simple_backup():
"""Main"""
args = _parse_arguments()
if args.no_syslog:
try:
logger.removeHandler(j_handler)
except NameError:
pass
inputs, output, exclude, keep, host, username, ssh_keyfile = _read_config(args.config)
if args.input is not None: