Skip to content

Commit

Permalink
Backups: default keep=None, which keeps all previous backups (#167)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eimrek authored Feb 26, 2024
1 parent d23dac4 commit d1f8c23
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions disk_objectstore/backup_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!"
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d1f8c23

Please sign in to comment.