Fix remote removal of old backups deleting the wrong folders
Old backups on the server were listed with ls, which also lists files and prints nothing on errors. A stray file made one backup too many get removed, and an empty or unreadable backup folder was counted as one backup with an empty name, which made rm -r delete the whole simple_backup folder. List only directories with find, and don't remove anything if listing fails.
This commit is contained in:
@@ -248,10 +248,23 @@ class Backup:
|
||||
if self._remote:
|
||||
assert self._ssh is not None
|
||||
|
||||
_, stdout, _ = self._ssh.exec_command(
|
||||
f'ls {shlex.quote(self.output + "/simple_backup")}')
|
||||
backup_dir = shlex.quote(self.output + '/simple_backup/')
|
||||
|
||||
dirs = stdout.read().decode('utf-8').strip().split('\n')
|
||||
# List only directories, and nothing (without errors) if no backup was made yet
|
||||
_, stdout, stderr = self._ssh.exec_command(
|
||||
f'if [ -d {backup_dir} ]; then find {backup_dir} -mindepth 1 -maxdepth 1 -type d; fi')
|
||||
|
||||
output = stdout.read().decode('utf-8')
|
||||
err = stderr.read().decode('utf-8').strip()
|
||||
|
||||
if err != '':
|
||||
# Don't risk removing the wrong directories if the list may be incomplete
|
||||
logger.error('Cannot list old backups on the server. Old backups will not be removed')
|
||||
logger.error(err)
|
||||
|
||||
return
|
||||
|
||||
dirs = [os.path.basename(d) for d in output.split('\n') if d != '']
|
||||
|
||||
n_backup = len(dirs)
|
||||
|
||||
@@ -276,7 +289,7 @@ class Backup:
|
||||
|
||||
if err != '':
|
||||
logger.error(
|
||||
'Error while removing backup %s.', {dirs[i]})
|
||||
'Error while removing backup %s.', dirs[i])
|
||||
logger.error(err)
|
||||
else:
|
||||
count += 1
|
||||
|
||||
Reference in New Issue
Block a user