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