From 87f30bc7878a8bd8cd6a7cf5591f7744b8dc923a Mon Sep 17 00:00:00 2001 From: Srigovind Nayak Date: Sun, 1 Sep 2024 17:10:32 +0530 Subject: [PATCH 1/4] forget: indicate why the oldest snapshot in a group is kept When the oldest snapshot in the list is retained, the reason is now prefixed with "oldest" to clearly indicate why it's being kept. --- internal/restic/snapshot_policy.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/restic/snapshot_policy.go b/internal/restic/snapshot_policy.go index 950c26c91..f5c90d6e4 100644 --- a/internal/restic/snapshot_policy.go +++ b/internal/restic/snapshot_policy.go @@ -260,6 +260,9 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots, reason if buckets[i].Count > 0 { buckets[i].Count-- } + if nr == len(list)-1 { + b.reason = fmt.Sprintf("oldest %v", b.reason) + } keepSnapReasons = append(keepSnapReasons, b.reason) } } @@ -276,6 +279,9 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots, reason 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 + if nr == len(list)-1 { + b.reason = fmt.Sprintf("oldest %v", b.reason) + } keepSnapReasons = append(keepSnapReasons, fmt.Sprintf("%v %v", b.reason, b.Within)) } } From d656a508523c2ddfa7f02eab6448243ae6e285b0 Mon Sep 17 00:00:00 2001 From: Srigovind Nayak Date: Sun, 1 Sep 2024 17:22:39 +0530 Subject: [PATCH 2/4] forget: update tests to reflect specific reasons for keeping oldest snapshots in a group --- internal/restic/testdata/policy_keep_snapshots_16 | 2 +- internal/restic/testdata/policy_keep_snapshots_17 | 2 +- internal/restic/testdata/policy_keep_snapshots_35 | 2 +- internal/restic/testdata/policy_keep_snapshots_36 | 2 +- internal/restic/testdata/policy_keep_snapshots_37 | 4 ++-- internal/restic/testdata/policy_keep_snapshots_38 | 2 +- internal/restic/testdata/policy_keep_snapshots_39 | 4 ++-- internal/restic/testdata/policy_keep_snapshots_4 | 2 +- 8 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/restic/testdata/policy_keep_snapshots_16 b/internal/restic/testdata/policy_keep_snapshots_16 index da6f43a1c..07444102b 100644 --- a/internal/restic/testdata/policy_keep_snapshots_16 +++ b/internal/restic/testdata/policy_keep_snapshots_16 @@ -68,7 +68,7 @@ "paths": null }, "matches": [ - "yearly snapshot" + "oldest yearly snapshot" ], "counters": { "yearly": 6 diff --git a/internal/restic/testdata/policy_keep_snapshots_17 b/internal/restic/testdata/policy_keep_snapshots_17 index ee728d4e0..de489d445 100644 --- a/internal/restic/testdata/policy_keep_snapshots_17 +++ b/internal/restic/testdata/policy_keep_snapshots_17 @@ -214,7 +214,7 @@ "paths": null }, "matches": [ - "yearly snapshot" + "oldest yearly snapshot" ], "counters": { "yearly": 6 diff --git a/internal/restic/testdata/policy_keep_snapshots_35 b/internal/restic/testdata/policy_keep_snapshots_35 index ece4ddbd2..afc2017dd 100644 --- a/internal/restic/testdata/policy_keep_snapshots_35 +++ b/internal/restic/testdata/policy_keep_snapshots_35 @@ -165,7 +165,7 @@ "paths": null }, "matches": [ - "yearly within 9999y" + "oldest yearly within 9999y" ], "counters": {} } diff --git a/internal/restic/testdata/policy_keep_snapshots_36 b/internal/restic/testdata/policy_keep_snapshots_36 index cce4cf537..6023e993e 100644 --- a/internal/restic/testdata/policy_keep_snapshots_36 +++ b/internal/restic/testdata/policy_keep_snapshots_36 @@ -1978,7 +1978,7 @@ "paths": null }, "matches": [ - "last snapshot" + "oldest last snapshot" ], "counters": { "last": -1 diff --git a/internal/restic/testdata/policy_keep_snapshots_37 b/internal/restic/testdata/policy_keep_snapshots_37 index 9856a83d6..0cf643ab6 100644 --- a/internal/restic/testdata/policy_keep_snapshots_37 +++ b/internal/restic/testdata/policy_keep_snapshots_37 @@ -2169,8 +2169,8 @@ "paths": null }, "matches": [ - "last snapshot", - "hourly snapshot" + "oldest last snapshot", + "oldest hourly snapshot" ], "counters": { "last": -1, diff --git a/internal/restic/testdata/policy_keep_snapshots_38 b/internal/restic/testdata/policy_keep_snapshots_38 index f5d7136d4..ef4eb6777 100644 --- a/internal/restic/testdata/policy_keep_snapshots_38 +++ b/internal/restic/testdata/policy_keep_snapshots_38 @@ -1708,7 +1708,7 @@ "paths": null }, "matches": [ - "hourly snapshot" + "oldest hourly snapshot" ], "counters": { "hourly": -1 diff --git a/internal/restic/testdata/policy_keep_snapshots_39 b/internal/restic/testdata/policy_keep_snapshots_39 index f5fb4b1bf..fc06d8994 100644 --- a/internal/restic/testdata/policy_keep_snapshots_39 +++ b/internal/restic/testdata/policy_keep_snapshots_39 @@ -238,8 +238,8 @@ "paths": null }, "matches": [ - "monthly snapshot", - "yearly snapshot" + "oldest monthly snapshot", + "oldest yearly snapshot" ], "counters": { "monthly": -1, diff --git a/internal/restic/testdata/policy_keep_snapshots_4 b/internal/restic/testdata/policy_keep_snapshots_4 index ff572d6a0..66e5cebe2 100644 --- a/internal/restic/testdata/policy_keep_snapshots_4 +++ b/internal/restic/testdata/policy_keep_snapshots_4 @@ -1978,7 +1978,7 @@ "paths": null }, "matches": [ - "last snapshot" + "oldest last snapshot" ], "counters": { "last": 97 From b69c6408a6cec3700915469c285c279a4356e9dc Mon Sep 17 00:00:00 2001 From: Srigovind Nayak Date: Sat, 7 Sep 2024 15:00:21 +0530 Subject: [PATCH 3/4] forget: make oldest snapshot marker more strict Now, a snapshot is only marked as oldest if it's the last in the list AND its values matches the last seen value for that bucket. Also, updated the corresponding golden files for the tests. --- internal/restic/snapshot_policy.go | 10 +++++----- internal/restic/testdata/policy_keep_snapshots_36 | 2 +- internal/restic/testdata/policy_keep_snapshots_37 | 4 ++-- internal/restic/testdata/policy_keep_snapshots_38 | 2 +- internal/restic/testdata/policy_keep_snapshots_4 | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/restic/snapshot_policy.go b/internal/restic/snapshot_policy.go index f5c90d6e4..28f871f4a 100644 --- a/internal/restic/snapshot_policy.go +++ b/internal/restic/snapshot_policy.go @@ -256,13 +256,13 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots, reason if val != b.Last || nr == len(list)-1 { debug.Log("keep %v %v, bucker %v, val %v\n", cur.Time, cur.id.Str(), i, val) keepSnap = true + if val == b.Last && nr == len(list)-1 { + b.reason = fmt.Sprintf("oldest %v", b.reason) + } buckets[i].Last = val if buckets[i].Count > 0 { buckets[i].Count-- } - if nr == len(list)-1 { - b.reason = fmt.Sprintf("oldest %v", b.reason) - } keepSnapReasons = append(keepSnapReasons, b.reason) } } @@ -278,10 +278,10 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots, reason 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 - if nr == len(list)-1 { + if val == b.Last && nr == len(list)-1 { b.reason = fmt.Sprintf("oldest %v", b.reason) } + bucketsWithin[i].Last = val keepSnapReasons = append(keepSnapReasons, fmt.Sprintf("%v %v", b.reason, b.Within)) } } diff --git a/internal/restic/testdata/policy_keep_snapshots_36 b/internal/restic/testdata/policy_keep_snapshots_36 index 6023e993e..cce4cf537 100644 --- a/internal/restic/testdata/policy_keep_snapshots_36 +++ b/internal/restic/testdata/policy_keep_snapshots_36 @@ -1978,7 +1978,7 @@ "paths": null }, "matches": [ - "oldest last snapshot" + "last snapshot" ], "counters": { "last": -1 diff --git a/internal/restic/testdata/policy_keep_snapshots_37 b/internal/restic/testdata/policy_keep_snapshots_37 index 0cf643ab6..9856a83d6 100644 --- a/internal/restic/testdata/policy_keep_snapshots_37 +++ b/internal/restic/testdata/policy_keep_snapshots_37 @@ -2169,8 +2169,8 @@ "paths": null }, "matches": [ - "oldest last snapshot", - "oldest hourly snapshot" + "last snapshot", + "hourly snapshot" ], "counters": { "last": -1, diff --git a/internal/restic/testdata/policy_keep_snapshots_38 b/internal/restic/testdata/policy_keep_snapshots_38 index ef4eb6777..f5d7136d4 100644 --- a/internal/restic/testdata/policy_keep_snapshots_38 +++ b/internal/restic/testdata/policy_keep_snapshots_38 @@ -1708,7 +1708,7 @@ "paths": null }, "matches": [ - "oldest hourly snapshot" + "hourly snapshot" ], "counters": { "hourly": -1 diff --git a/internal/restic/testdata/policy_keep_snapshots_4 b/internal/restic/testdata/policy_keep_snapshots_4 index 66e5cebe2..ff572d6a0 100644 --- a/internal/restic/testdata/policy_keep_snapshots_4 +++ b/internal/restic/testdata/policy_keep_snapshots_4 @@ -1978,7 +1978,7 @@ "paths": null }, "matches": [ - "oldest last snapshot" + "last snapshot" ], "counters": { "last": 97 From 5468e852220ea52afd94f2222b42a6f54dc4f93f Mon Sep 17 00:00:00 2001 From: Srigovind Nayak Date: Sat, 7 Sep 2024 15:06:21 +0530 Subject: [PATCH 4/4] docs: mention that the oldest snapshot is marked `oldest` in the reasons of the forget comman --- doc/060_forget.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/060_forget.rst b/doc/060_forget.rst index fe0236f12..b211148cb 100644 --- a/doc/060_forget.rst +++ b/doc/060_forget.rst @@ -214,7 +214,8 @@ The ``forget`` command accepts the following policy options: run) and these snapshots will hence not be removed. .. note:: If there are not enough snapshots to keep one for each duration related - ``--keep-{within-,}*`` option, the oldest snapshot is kept additionally. + ``--keep-{within-,}*`` option, the oldest snapshot is kept additionally and + marked as ``oldest`` in the output (e.g. ``oldest hourly snapshot``). .. note:: Specifying ``--keep-tag ''`` will match untagged snapshots only.