Only count directories when removing old local backups

Old backups were listed with os.listdir, so a stray file or symlink
in the backup folder counted as a backup: depending on its name,
rmtree crashed on it or one backup too many was removed. An
unreadable backup folder also crashed with PermissionError. Count
only directories, and log an error if the folder can't be read.
This commit is contained in:
Fuxino
2026-10-08 20:38:20 +02:00
parent e6ac9e6cab
commit d815b0832a
+7 -1
View File
@@ -295,9 +295,15 @@ class Backup:
count += 1 count += 1
else: else:
try: try:
dirs = os.listdir(f'{self.output}/simple_backup') # Only count directories, so that stray files don't count as backups
dirs = [f.name for f in os.scandir(f'{self.output}/simple_backup')
if f.is_dir(follow_symlinks=False)]
except FileNotFoundError: except FileNotFoundError:
return return
except PermissionError:
logger.error('Cannot list old backups. Permission denied. Old backups will not be removed')
return
n_backup = len(dirs) n_backup = len(dirs)