2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-17 02:25:12 +00:00

Merge pull request #943 from middelink/fix-names

Small changes to cmd_forget and snapshot_filter
This commit is contained in:
Alexander Neumann 2017-05-07 10:15:21 +02:00
commit 0f057bd440
3 changed files with 43 additions and 24 deletions

View File

@ -90,7 +90,7 @@ func runForget(opts ForgetOptions, gopts GlobalOptions, args []string) error {
} }
snapshotGroups := make(map[string]restic.Snapshots) snapshotGroups := make(map[string]restic.Snapshots)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(gopts.ctx)
defer cancel() defer cancel()
for sn := range FindFilteredSnapshots(ctx, repo, opts.Host, opts.Tags, opts.Paths, args) { for sn := range FindFilteredSnapshots(ctx, repo, opts.Host, opts.Tags, opts.Paths, args) {
if len(args) > 0 { if len(args) > 0 {

View File

@ -125,49 +125,46 @@ func y(d time.Time) int {
} }
// apply moves snapshots from Unprocess to either Keep or Remove. It sorts the // apply moves snapshots from Unprocess to either Keep or Remove. It sorts the
// snapshots into buckets according to the return of fn, and then moves the // snapshots into buckets according to the return value of fn, and then moves
// newest snapshot in each bucket to Keep and all others to Remove. When max // the newest snapshot in each bucket to Keep and all others to Remove. When
// snapshots were found, processing stops. // max snapshots were found, processing stops.
func (f *filter) apply(fn func(time.Time) int, max int) { func (f *filter) apply(fn func(time.Time) int, max int) {
if max == 0 || len(f.Unprocessed) == 0 { if max == 0 || len(f.Unprocessed) == 0 {
return return
} }
sameDay := Snapshots{} sameBucket := Snapshots{}
lastDay := fn(f.Unprocessed[0].Time) lastBucket := fn(f.Unprocessed[0].Time)
for len(f.Unprocessed) > 0 { for len(f.Unprocessed) > 0 {
cur := f.Unprocessed[0] cur := f.Unprocessed[0]
day := fn(cur.Time) bucket := fn(cur.Time)
// if the snapshots are from a new day, forget all but the first (=last // if the snapshots are from a new bucket, forget all but the first
// in time) snapshot from the previous day. // (=last in time) snapshot from the previous bucket.
if day != lastDay { if bucket != lastBucket {
f.Keep = append(f.Keep, sameDay[0]) f.Keep = append(f.Keep, sameBucket[0])
for _, snapshot := range sameDay[1:] { f.Remove = append(f.Remove, sameBucket[1:]...)
f.Remove = append(f.Remove, snapshot)
}
sameDay = Snapshots{} sameBucket = Snapshots{}
lastDay = day lastBucket = bucket
max-- max--
if max == 0 { if max == 0 {
break return
} }
} }
// collect all snapshots for the current day // collect all snapshots for the current bucket
sameDay = append(sameDay, cur) sameBucket = append(sameBucket, cur)
f.Unprocessed = f.Unprocessed[1:] f.Unprocessed = f.Unprocessed[1:]
} }
if len(sameDay) > 0 { // if we have leftovers, process them too.
f.Keep = append(f.Keep, sameDay[0]) if len(sameBucket) > 0 {
for _, snapshot := range sameDay[1:] { f.Keep = append(f.Keep, sameBucket[0])
f.Remove = append(f.Remove, snapshot) f.Remove = append(f.Remove, sameBucket[1:]...)
}
} }
} }

View File

@ -91,6 +91,28 @@ func TestFilterSnapshots(t *testing.T) {
} }
} }
func TestExpireSnapshotOps(t *testing.T) {
data := []struct {
expectEmpty bool
expectSum int
p *restic.ExpirePolicy
}{
{true, 0, &restic.ExpirePolicy{}},
{true, 0, &restic.ExpirePolicy{Tags: []string{}}},
{false, 22, &restic.ExpirePolicy{Daily: 7, Weekly: 2, Monthly: 3, Yearly: 10}},
}
for i, d := range data {
isEmpty := d.p.Empty()
if isEmpty != d.expectEmpty {
t.Errorf("empty test %v: wrong result, want:\n %#v\ngot:\n %#v", i, d.expectEmpty, isEmpty)
}
hasSum := d.p.Sum()
if hasSum != d.expectSum {
t.Errorf("sum test %v: wrong result, want:\n %#v\ngot:\n %#v", i, d.expectSum, hasSum)
}
}
}
var testExpireSnapshots = restic.Snapshots{ var testExpireSnapshots = restic.Snapshots{
{Time: parseTimeUTC("2014-09-01 10:20:30")}, {Time: parseTimeUTC("2014-09-01 10:20:30")},
{Time: parseTimeUTC("2014-09-02 10:20:30")}, {Time: parseTimeUTC("2014-09-02 10:20:30")},