7 Commits
3.3.0 ... 3.5.0

Author SHA1 Message Date
970d3dde1e Add manual selection of rsync options 2023-06-15 21:30:43 +02:00
4d23bde906 Check that inputs from command line exist
Check that the inputs specified on the command line (i.e. with the
option '-i' or '--input') exist and print a warning when they don't.
If no valid inputs are found, exit.
2023-06-15 18:44:22 +02:00
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 119 additions and 22 deletions

View File

@ -59,9 +59,44 @@ Default behavior is to remove old backups after successfully completing the back
.TP
.B \-\-no\-syslog
Don't use systemd journal for logging
.TP
.B \-\-rsync\-options OPTIONS [OPTION...]
By default, the following rsync options are used:
.RS
.PP
\-a \-r \-c \-v \-h \-H \-X
.PP
Using \-\-rsync\-options it is possible to manually select which options to use. Supported values are the following:
.PP
\-a, \-l, \-p, \-t, \-g, \-o, \-c, \-h, \-D, \-H, \-X
.PP
Options \-r and \-v are used in any case. Not that options must be specified without dash (\-), for example:
.PP
.EX
simple_backup \-\-rsync\-options a l p
.EE
.TP
Check rsync(1) for details about the options.
.RE
.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.5.0'

View File

@ -1,14 +1,14 @@
# 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]
@ -173,7 +186,7 @@ class Backup:
self.create_backup_dir()
_, self._inputs_path = mkstemp(prefix='tmp_inputs', text=True)
_, self._exclude_path = mkstemp(prefix='tmp_exclude', text=True)
count = 0
with open(self._inputs_path, 'w') as fp:
for i in self.inputs:
@ -182,6 +195,19 @@ class Backup:
else:
fp.write(i)
fp.write('\n')
count += 1
if count == 0:
logger.info('No existing files or directories specified for backup. Nothing to do')
try:
notify('Backup finished. No files copied')
except NameError:
pass
return 1
_, self._exclude_path = mkstemp(prefix='tmp_exclude', text=True)
with open(self._exclude_path, 'w') as fp:
if self.exclude is not None:
@ -229,12 +255,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',
@ -253,12 +283,32 @@ def _parse_arguments():
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')
parser.add_argument('--rsync-options', nargs='+',
choices=['a', 'l', 'p', 't', 'g', 'o', 'c', 'h', 'D', 'H', 'X'],
help='Specify options for rsync')
args = parser.parse_args()
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 +320,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')
@ -319,19 +372,28 @@ def simple_backup():
if args.keep is not None:
keep = args.keep
if args.rsync_options is None:
if args.checksum:
backup_options = '-arcvh -H -X'
rsync_options = '-arcvh -H -X'
else:
backup_options = '-arvh -H -X'
rsync_options = '-arvh -H -X'
else:
rsync_options = '-r -v '
backup = Backup(inputs, output, exclude, keep, backup_options, remove_before=args.remove_before_backup)
for ro in args.rsync_options:
rsync_options = rsync_options + f'-{ro} '
if backup.check_params():
backup.run()
if '-c ' not in rsync_options and args.checksum:
rsync_options = rsync_options + '-c'
return 0
backup = Backup(inputs, output, exclude, keep, rsync_options, remove_before=args.remove_before_backup)
return 1
return_code = backup.check_params()
if return_code == 0:
return backup.run()
return return_code
if __name__ == '__main__':