From e6ac9e6cab7ee8159f780408c39739a7271d01b9 Mon Sep 17 00:00:00 2001 From: Fuxino Date: Thu, 8 Oct 2026 20:36:55 +0200 Subject: [PATCH] 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. --- simple_backup/simple_backup.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/simple_backup/simple_backup.py b/simple_backup/simple_backup.py index f996878..9e4d7f2 100755 --- a/simple_backup/simple_backup.py +++ b/simple_backup/simple_backup.py @@ -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