From 2081bd12fbfec4574df75796c1bb2336d1b2ec05 Mon Sep 17 00:00:00 2001 From: Magnus Thor Torfason Date: Sat, 24 Jul 2021 15:58:26 +0000 Subject: [PATCH] forget: Ensure future snapshots do not affect --keep-within-* Ensure that only snapshots made in the past are taken into account when running restic forget with the within switches (--keep-within, --keep-within- hourly, and friends) --- internal/restic/snapshot_policy.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/restic/snapshot_policy.go b/internal/restic/snapshot_policy.go index d063a9ec2..8ce6cb2e3 100644 --- a/internal/restic/snapshot_policy.go +++ b/internal/restic/snapshot_policy.go @@ -147,15 +147,19 @@ func always(d time.Time, nr int) int { return nr } -// findLatestTimestamp returns the time stamp for the newest snapshot. +// findLatestTimestamp returns the time stamp for the latest (newest) snapshot, +// for use with policies based on time relative to latest. func findLatestTimestamp(list Snapshots) time.Time { if len(list) == 0 { panic("list of snapshots is empty") } var latest time.Time + now := time.Now() for _, sn := range list { - if sn.Time.After(latest) { + // Find the latest snapshot in the list + // The latest snapshot must, however, not be in the future. + if sn.Time.After(latest) && sn.Time.Before(now) { latest = sn.Time } }