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:
@@ -295,9 +295,15 @@ class Backup:
|
||||
count += 1
|
||||
else:
|
||||
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:
|
||||
return
|
||||
except PermissionError:
|
||||
logger.error('Cannot list old backups. Permission denied. Old backups will not be removed')
|
||||
|
||||
return
|
||||
|
||||
n_backup = len(dirs)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user