5 Commits
3.3.0 ... 3.4.0

Author SHA1 Message Date
ff70358496 Version bump 2023-06-15 17:38:00 +02:00
6f1e91e2cd Add more return codes 2023-06-15 17:14:17 +02:00
b3fee0d022 Add some meaningful return codes 2023-06-15 16:58:56 +02:00
d63eb8f771 Improve handling of missing inputs 2023-06-15 15:34:00 +02:00
56df958c5b 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
2023-06-04 12:09:30 +02:00
4 changed files with 72 additions and 22 deletions

View File

@ -62,6 +62,22 @@ 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.
.SH EXIT STATUS
.TP
.B 0
The backup was completed without errors
.TP
.B 1
No valid inputs selected for backup
.TP
.B 2
Backup failed because output directory for storing the backup does not exist
.TP
.B 3
Permission denied to access the output directory
.TP
.B 4
rsync error (rsync returned a non-zero value)
.SH SEE ALSO
.BR rsync (1)
.SH AUTHORS

View File

@ -1,3 +1,3 @@
"""Init."""
__version__ = '3.3.0'
__version__ = '3.4.0'

View File

@ -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)
# Number of old backups (i.e. excluding the one that's being created) to keep (use -1 to keep all)
keep=-1

View File

@ -1,6 +1,7 @@
#!/usr/bin/python3
# Import libraries
import sys
import os
from functools import wraps
from shutil import rmtree
@ -12,6 +13,8 @@ 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:
@ -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)
@ -91,26 +95,26 @@ class Backup:
def check_params(self):
if self.inputs is None or len(self.inputs) == 0:
logger.info('No files or directory specified for backup.')
logger.info('No existing files or directories specified for backup. Nothing to do')
return False
return 1
if self.output is None:
logger.critical('No output path specified. Use -o argument or specify output path in configuration file')
return False
return 2
if not os.path.isdir(self.output):
logger.critical('Output path for backup does not exist')
return False
return 2
self.output = os.path.abspath(self.output)
if self.keep is None:
self.keep = -1
return True
return 0
# Function to create the actual backup directory
def create_backup_dir(self):
@ -153,6 +157,15 @@ class Backup:
logger.info('No previous backups available')
return
except PermissionError:
logger.critical('Cannot access the backup directory. Permission denied')
try:
notify('Backup failed (check log for details)')
except NameError:
pass
sys.exit(3)
try:
self._last_backup = dirs[-1]
@ -177,9 +190,6 @@ class Backup:
with open(self._inputs_path, 'w') as fp:
for i in self.inputs:
if not os.path.exists(i):
logger.warning(f'Input {i} not found. Skipping')
else:
fp.write(i)
fp.write('\n')
@ -229,12 +239,16 @@ class Backup:
notify('Backup finished with errors (check log for details)')
except NameError:
pass
return 4
else:
try:
notify('Backup finished')
except NameError:
pass
return 0
def _parse_arguments():
parser = argparse.ArgumentParser(prog='simple_backup',
@ -259,6 +273,23 @@ def _parse_arguments():
return args
def _expand_inputs(inputs):
expanded_inputs = []
for i in inputs:
if i == '':
continue
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(i_ex)
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 +301,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')
@ -326,12 +360,12 @@ def simple_backup():
backup = Backup(inputs, output, exclude, keep, backup_options, remove_before=args.remove_before_backup)
if backup.check_params():
backup.run()
return_code = backup.check_params()
return 0
if return_code == 0:
return backup.run()
return 1
return return_code
if __name__ == '__main__':