diff --git a/internal/restic/snapshot_policy.go b/internal/restic/snapshot_policy.go index 0775f4024..929da93ed 100644 --- a/internal/restic/snapshot_policy.go +++ b/internal/restic/snapshot_policy.go @@ -34,34 +34,34 @@ func (e ExpirePolicy) Empty() bool { } // ymdh returns an integer in the form YYYYMMDDHH. -func ymdh(d time.Time) int { +func ymdh(d time.Time, _ int) int { return d.Year()*1000000 + int(d.Month())*10000 + d.Day()*100 + d.Hour() } // ymd returns an integer in the form YYYYMMDD. -func ymd(d time.Time) int { +func ymd(d time.Time, _ int) int { return d.Year()*10000 + int(d.Month())*100 + d.Day() } // yw returns an integer in the form YYYYWW, where WW is the week number. -func yw(d time.Time) int { +func yw(d time.Time, _ int) int { year, week := d.ISOWeek() return year*100 + week } // ym returns an integer in the form YYYYMM. -func ym(d time.Time) int { +func ym(d time.Time, _ int) int { return d.Year()*100 + int(d.Month()) } // y returns the year of d. -func y(d time.Time) int { +func y(d time.Time, _ int) int { return d.Year() } // always returns a unique number for d. -func always(d time.Time) int { - return int(d.UnixNano()) +func always(d time.Time, nr int) int { + return nr } // ApplyPolicy returns the snapshots from list that are to be kept and removed @@ -79,7 +79,7 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots) { var buckets = [6]struct { Count int - bucker func(d time.Time) int + bucker func(d time.Time, nr int) int Last int }{ {p.Last, always, -1}, @@ -90,7 +90,7 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots) { {p.Yearly, y, -1}, } - for _, cur := range list { + for nr, cur := range list { var keepSnap bool // Tags are handled specially as they are not counted. @@ -103,7 +103,7 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots) { // Now update the other buckets and see if they have some counts left. for i, b := range buckets { if b.Count > 0 { - val := b.bucker(cur.Time) + val := b.bucker(cur.Time, nr) if val != b.Last { keepSnap = true buckets[i].Last = val diff --git a/internal/restic/snapshot_policy_test.go b/internal/restic/snapshot_policy_test.go index e0cdeeeba..69111fab1 100644 --- a/internal/restic/snapshot_policy_test.go +++ b/internal/restic/snapshot_policy_test.go @@ -114,6 +114,10 @@ var testExpireSnapshots = restic.Snapshots{ {Time: parseTimeUTC("2015-10-11 10:20:30")}, {Time: parseTimeUTC("2015-10-20 10:20:30")}, {Time: parseTimeUTC("2015-10-22 10:20:30")}, + {Time: parseTimeUTC("2015-10-22 10:20:30")}, + {Time: parseTimeUTC("2015-10-22 10:20:30"), Tags: []string{"foo", "bar"}}, + {Time: parseTimeUTC("2015-10-22 10:20:30"), Tags: []string{"foo", "bar"}}, + {Time: parseTimeUTC("2015-10-22 10:20:30"), Tags: []string{"foo", "bar"}, Paths: []string{"path1", "path2"}}, {Time: parseTimeUTC("2015-11-08 10:20:30")}, {Time: parseTimeUTC("2015-11-10 10:20:30")}, {Time: parseTimeUTC("2015-11-12 10:20:30")}, diff --git a/internal/restic/testdata/policy_keep_snapshots_0 b/internal/restic/testdata/policy_keep_snapshots_0 index 159d55d0c..875f0e44e 100644 --- a/internal/restic/testdata/policy_keep_snapshots_0 +++ b/internal/restic/testdata/policy_keep_snapshots_0 @@ -149,6 +149,41 @@ "tree": null, "paths": null }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": [ + "path1", + "path2" + ], + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, { "time": "2015-10-20T10:20:30Z", "tree": null, diff --git a/internal/restic/testdata/policy_keep_snapshots_18 b/internal/restic/testdata/policy_keep_snapshots_18 index 3ca6727a0..de7cea914 100644 --- a/internal/restic/testdata/policy_keep_snapshots_18 +++ b/internal/restic/testdata/policy_keep_snapshots_18 @@ -1,4 +1,34 @@ [ + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": [ + "path1", + "path2" + ], + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, { "time": "2014-11-15T10:20:30Z", "tree": null, diff --git a/internal/restic/testdata/policy_keep_snapshots_19 b/internal/restic/testdata/policy_keep_snapshots_19 index af86beb84..5e0cb619b 100644 --- a/internal/restic/testdata/policy_keep_snapshots_19 +++ b/internal/restic/testdata/policy_keep_snapshots_19 @@ -1,4 +1,34 @@ [ + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": [ + "path1", + "path2" + ], + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, { "time": "2014-11-15T10:20:30Z", "tree": null, diff --git a/internal/restic/testdata/policy_keep_snapshots_20 b/internal/restic/testdata/policy_keep_snapshots_20 index 004b12156..51101c49b 100644 --- a/internal/restic/testdata/policy_keep_snapshots_20 +++ b/internal/restic/testdata/policy_keep_snapshots_20 @@ -1,4 +1,34 @@ [ + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": [ + "path1", + "path2" + ], + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, { "time": "2014-11-15T10:20:30Z", "tree": null, diff --git a/internal/restic/testdata/policy_keep_snapshots_3 b/internal/restic/testdata/policy_keep_snapshots_3 index 159d55d0c..4c2846221 100644 --- a/internal/restic/testdata/policy_keep_snapshots_3 +++ b/internal/restic/testdata/policy_keep_snapshots_3 @@ -149,6 +149,41 @@ "tree": null, "paths": null }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": [ + "path1", + "path2" + ], + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, { "time": "2015-10-20T10:20:30Z", "tree": null, @@ -522,25 +557,5 @@ "time": "2014-08-13T10:20:30.1Z", "tree": null, "paths": null - }, - { - "time": "2014-08-13T10:20:30Z", - "tree": null, - "paths": null - }, - { - "time": "2014-08-12T10:20:30Z", - "tree": null, - "paths": null - }, - { - "time": "2014-08-10T10:20:30Z", - "tree": null, - "paths": null - }, - { - "time": "2014-08-08T10:20:30Z", - "tree": null, - "paths": null } ] \ No newline at end of file diff --git a/internal/restic/testdata/policy_keep_snapshots_4 b/internal/restic/testdata/policy_keep_snapshots_4 index 159d55d0c..875f0e44e 100644 --- a/internal/restic/testdata/policy_keep_snapshots_4 +++ b/internal/restic/testdata/policy_keep_snapshots_4 @@ -149,6 +149,41 @@ "tree": null, "paths": null }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": [ + "path1", + "path2" + ], + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, { "time": "2015-10-20T10:20:30Z", "tree": null,