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