|
|
|
@ -71,12 +71,13 @@ class MyFormatter(argparse.RawTextHelpFormatter, argparse.ArgumentDefaultsHelpFo
|
|
|
|
|
|
|
|
|
|
class Backup:
|
|
|
|
|
|
|
|
|
|
def __init__(self, inputs, output, exclude, keep, options):
|
|
|
|
|
def __init__(self, inputs, output, exclude, keep, options, remove_before=False):
|
|
|
|
|
self.inputs = inputs
|
|
|
|
|
self.output = output
|
|
|
|
|
self.exclude = exclude
|
|
|
|
|
self.options = options
|
|
|
|
|
self.keep = keep
|
|
|
|
|
self.remove_before = remove_before
|
|
|
|
|
self._last_backup = ''
|
|
|
|
|
self._output_dir = ''
|
|
|
|
|
self._inputs_path = ''
|
|
|
|
@ -162,16 +163,6 @@ class Backup:
|
|
|
|
|
self.create_backup_dir()
|
|
|
|
|
self.find_last_backup()
|
|
|
|
|
|
|
|
|
|
if os.path.islink(f'{self.output}/simple_backup/last_backup'):
|
|
|
|
|
try:
|
|
|
|
|
os.remove(f'{self.output}/simple_backup/last_backup')
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
logger.error('Failed to remove last_backup link. File not found')
|
|
|
|
|
self._err_flag = True
|
|
|
|
|
except PermissionError:
|
|
|
|
|
logger.error('Failed to remove last_backup link. Permission denied')
|
|
|
|
|
self._err_flag = True
|
|
|
|
|
|
|
|
|
|
_, self._inputs_path = mkstemp(prefix='tmp_inputs', text=True)
|
|
|
|
|
_, self._exclude_path = mkstemp(prefix='tmp_exclude', text=True)
|
|
|
|
|
|
|
|
|
@ -188,6 +179,9 @@ class Backup:
|
|
|
|
|
fp.write(e)
|
|
|
|
|
fp.write('\n')
|
|
|
|
|
|
|
|
|
|
if self.keep != -1 and self.remove_before:
|
|
|
|
|
self.remove_old_backups()
|
|
|
|
|
|
|
|
|
|
logger.info('Copying files. This may take a long time...')
|
|
|
|
|
|
|
|
|
|
if self._last_backup == '':
|
|
|
|
@ -202,11 +196,24 @@ class Backup:
|
|
|
|
|
p = Popen(rsync, stdout=PIPE, stderr=STDOUT, shell=True)
|
|
|
|
|
output, _ = p.communicate()
|
|
|
|
|
|
|
|
|
|
if p.returncode != 0:
|
|
|
|
|
self._err_flag = True
|
|
|
|
|
|
|
|
|
|
output = output.decode("utf-8").split('\n')
|
|
|
|
|
|
|
|
|
|
logger.info(f'rsync: {output[-3]}')
|
|
|
|
|
logger.info(f'rsync: {output[-2]}')
|
|
|
|
|
|
|
|
|
|
if os.path.islink(f'{self.output}/simple_backup/last_backup'):
|
|
|
|
|
try:
|
|
|
|
|
os.remove(f'{self.output}/simple_backup/last_backup')
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
logger.error('Failed to remove last_backup link. File not found')
|
|
|
|
|
self._err_flag = True
|
|
|
|
|
except PermissionError:
|
|
|
|
|
logger.error('Failed to remove last_backup link. Permission denied')
|
|
|
|
|
self._err_flag = True
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
os.symlink(self._output_dir, f'{self.output}/simple_backup/last_backup', target_is_directory=True)
|
|
|
|
|
except FileExistsError:
|
|
|
|
@ -216,7 +223,7 @@ class Backup:
|
|
|
|
|
logger.error('Failed to create last_backup link. Permission denied')
|
|
|
|
|
self._err_flag = True
|
|
|
|
|
|
|
|
|
|
if self.keep != -1:
|
|
|
|
|
if self.keep != -1 and not self.remove_before:
|
|
|
|
|
self.remove_old_backups()
|
|
|
|
|
|
|
|
|
|
os.remove(self._inputs_path)
|
|
|
|
@ -242,6 +249,8 @@ def _parse_arguments():
|
|
|
|
|
parser.add_argument('-k', '--keep', type=int, help='Number of old backups to keep')
|
|
|
|
|
parser.add_argument('-s', '--checksum', action='store_true',
|
|
|
|
|
help='Use checksum rsync option to compare files (MUCH SLOWER)')
|
|
|
|
|
parser.add_argument('--remove-before-backup', action='store_true',
|
|
|
|
|
help='Remove old backups before executing the backup, instead of after')
|
|
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
@ -288,7 +297,7 @@ def simple_backup():
|
|
|
|
|
else:
|
|
|
|
|
backup_options = '-arvh -H -X'
|
|
|
|
|
|
|
|
|
|
backup = Backup(inputs, output, exclude, keep, backup_options)
|
|
|
|
|
backup = Backup(inputs, output, exclude, keep, backup_options, remove_before=args.remove_before_backup)
|
|
|
|
|
|
|
|
|
|
if backup.check_params():
|
|
|
|
|
backup.run()
|
|
|
|
|