5 Commits
3.2.2 ... 3.2.4

Author SHA1 Message Date
a07fce1b6c Version bump 2023-05-25 19:01:50 +02:00
11c1ab0952 Add options to remove old backups before copying files 2023-05-25 19:01:19 +02:00
a42ade4f2b Version bump 2023-05-25 17:27:04 +02:00
c6432c1350 Check rsync return code 2023-05-25 17:20:32 +02:00
fa1d8f410e Remove old last_backup link after copying files 2023-05-25 17:10:38 +02:00
3 changed files with 24 additions and 15 deletions

View File

@ -3,7 +3,7 @@
# Maintainer: Daniele Fucini <dfucini@gmail.com> # Maintainer: Daniele Fucini <dfucini@gmail.com>
pkgname=simple_backup pkgname=simple_backup
pkgver=3.1.2.r1.ga4c4b88 pkgver=3.2.3.r1.ga42ade4
pkgrel=1 pkgrel=1
pkgdesc='Simple backup script that uses rsync to copy files' pkgdesc='Simple backup script that uses rsync to copy files'
arch=('any') arch=('any')

View File

@ -1,3 +1,3 @@
"""Init.""" """Init."""
__version__ = '3.2.2' __version__ = '3.2.4'

View File

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