From d1f8c2392c6617a417c11b41fc253d80ee341649 Mon Sep 17 00:00:00 2001 From: Kristjan Eimre Date: Mon, 26 Feb 2024 09:46:04 +0100 Subject: [PATCH] Backups: default `keep=None`, which keeps all previous backups (#167) Default `keep=1` might be dangerous, in case that users want to keep many backups, as accidentally forgetting to specify `--keep` will delete them down to 2 by default. --- disk_objectstore/backup_utils.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/disk_objectstore/backup_utils.py b/disk_objectstore/backup_utils.py index be9fb84..d536f04 100644 --- a/disk_objectstore/backup_utils.py +++ b/disk_objectstore/backup_utils.py @@ -50,7 +50,7 @@ class BackupManager: def __init__( self, dest: str, - keep: int = 1, + keep: Optional[int] = None, rsync_exe: Optional[str] = None, ) -> None: self.dest = dest @@ -60,7 +60,7 @@ def __init__( # Validate the backup config inputs - if self.keep < 0: + if self.keep is not None and self.keep < 0: raise ValueError( "Input validation failed: keep variable can't be negative!" ) @@ -247,14 +247,15 @@ def get_last_backup_folder(self): def delete_old_backups(self): """Get all folders matching the backup pattern, and delete oldest ones.""" - sorted_folders = sorted(self.get_existing_backup_folders()) - to_delete = sorted_folders[: -(self.keep + 1)] - for folder in to_delete: - success = self.run_cmd(["rm", "-rf", folder])[0] - if success: - LOGGER.info("Deleted old backup: %s", folder) - else: - LOGGER.warning("Warning: couldn't delete old backup: %s", folder) + if self.keep is not None: + sorted_folders = sorted(self.get_existing_backup_folders()) + to_delete = sorted_folders[: -(self.keep + 1)] + for folder in to_delete: + success = self.run_cmd(["rm", "-rf", folder])[0] + if success: + LOGGER.info("Deleted old backup: %s", folder) + else: + LOGGER.warning("Warning: couldn't delete old backup: %s", folder) def backup_auto_folders(self, backup_func: Callable) -> None: """Create a backup, managing live and previous backup folders automatically