From 1a46311ddd71002ab0e268f455b4c31ccbfd4246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Flemstr=C3=B6m?= Date: Sat, 1 Jun 2024 21:32:27 +0200 Subject: [PATCH] Delete snapshots from the future --- snapborg/retention.py | 5 +++-- snapborg/snapper.py | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/snapborg/retention.py b/snapborg/retention.py index fa07781..31c12b6 100644 --- a/snapborg/retention.py +++ b/snapborg/retention.py @@ -14,7 +14,8 @@ def get_retained_snapshots(snapshots, date_key, keep_last=1, keep_minutely=0, ke start_of_today = datetime.combine(today, time.min) retained = set() with_date = sorted([(date_key(snapshot), snapshot) - for snapshot in snapshots], key=lambda entry: entry[0]) + for snapshot in snapshots + if date_key(snapshot) <= now], key=lambda entry: entry[0]) if keep_last > 0: retained.update(it[1] for it in with_date[-keep_last:]) # Transform each retainment setting (minutely, hourly, ...) into a tuple of the following form: @@ -41,7 +42,7 @@ def get_retained_snapshots(snapshots, date_key, keep_last=1, keep_minutely=0, ke while nr_keep > 0 and len(snapshots_remaining) > 0: start, end = interval snapshots_to_consider, snapshots_remaining = split( - snapshots_remaining, lambda x: x[0] >= start and x[0] < end) + snapshots_remaining, lambda x: start <= x[0] < end) # when pruning, borg keeps the last snapshot of an interval. By selecting the last # snapshot here, we ensure we aren't backing up snapshots just to prune them right away # https://borgbackup.readthedocs.io/en/stable/usage/prune.html#description diff --git a/snapborg/snapper.py b/snapborg/snapper.py index ee233be..726c7a2 100644 --- a/snapborg/snapper.py +++ b/snapborg/snapper.py @@ -136,3 +136,6 @@ def restore_cleanup_state(self, dryrun=False): ["modify", "--cleanup-algorithm", self._cleanup, f"{self.get_number()}"], self.config.name, dryrun=dryrun ) + + def __str__(self): + return f"SnapperSnapshot({self.get_date()})"