From 8da5a6649bd2016a164b6bb471bafaf7fe2d3d1a Mon Sep 17 00:00:00 2001 From: Gautam Menghani Date: Sat, 17 Jun 2023 18:04:35 +0530 Subject: [PATCH] Preserve oldest snapshot when keep-within* does not collect enough --- internal/restic/snapshot_policy.go | 2 +- .../restic/testdata/policy_keep_snapshots_35 | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/internal/restic/snapshot_policy.go b/internal/restic/snapshot_policy.go index 22939bd6c..0ff0c5ec8 100644 --- a/internal/restic/snapshot_policy.go +++ b/internal/restic/snapshot_policy.go @@ -278,7 +278,7 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots, reason if cur.Time.After(t) { val := b.bucker(cur.Time, nr) - if val != b.Last { + if val != b.Last || nr == len(list)-1 { debug.Log("keep %v, time %v, ID %v, bucker %v, val %v %v\n", b.reason, cur.Time, cur.id.Str(), i, val, b.Last) keepSnap = true bucketsWithin[i].Last = val diff --git a/internal/restic/testdata/policy_keep_snapshots_35 b/internal/restic/testdata/policy_keep_snapshots_35 index a4def907a..ece4ddbd2 100644 --- a/internal/restic/testdata/policy_keep_snapshots_35 +++ b/internal/restic/testdata/policy_keep_snapshots_35 @@ -44,6 +44,11 @@ "time": "2014-11-22T10:20:30Z", "tree": null, "paths": null + }, + { + "time": "2014-08-08T10:20:30Z", + "tree": null, + "paths": null } ], "reasons": [ @@ -152,6 +157,17 @@ "yearly within 9999y" ], "counters": {} + }, + { + "snapshot": { + "time": "2014-08-08T10:20:30Z", + "tree": null, + "paths": null + }, + "matches": [ + "yearly within 9999y" + ], + "counters": {} } ] } \ No newline at end of file