forget: Add policy to keep snapshots before a date

This commit is contained in:
Matthew Holt
2018-04-23 14:34:37 -06:00
committed by Alexander Neumann
parent 159badf5ba
commit b52f2aa9a4
4 changed files with 137 additions and 28 deletions

View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"sort"
"strings"
"time"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic"
@@ -27,13 +28,14 @@ data after 'forget' was run successfully, see the 'prune' command. `,
// ForgetOptions collects all options for the forget command.
type ForgetOptions struct {
Last int
Hourly int
Daily int
Weekly int
Monthly int
Yearly int
KeepTags restic.TagLists
Last int
Hourly int
Daily int
Weekly int
Monthly int
Yearly int
NewerThan time.Duration
KeepTags restic.TagLists
Host string
Tags restic.TagLists
@@ -58,6 +60,7 @@ func init() {
f.IntVarP(&forgetOptions.Weekly, "keep-weekly", "w", 0, "keep the last `n` weekly snapshots")
f.IntVarP(&forgetOptions.Monthly, "keep-monthly", "m", 0, "keep the last `n` monthly snapshots")
f.IntVarP(&forgetOptions.Yearly, "keep-yearly", "y", 0, "keep the last `n` yearly snapshots")
f.DurationVar(&forgetOptions.NewerThan, "keep-newer-than", 0, "keep snapshots that were created within this timeframe")
f.Var(&forgetOptions.KeepTags, "keep-tag", "keep snapshots with this `taglist` (can be specified multiple times)")
// Sadly the commonly used shortcut `H` is already used.
@@ -163,14 +166,20 @@ func runForget(opts ForgetOptions, gopts GlobalOptions, args []string) error {
}
}
var ageCutoff time.Time
if opts.NewerThan > 0 {
ageCutoff = time.Now().Add(-opts.NewerThan)
}
policy := restic.ExpirePolicy{
Last: opts.Last,
Hourly: opts.Hourly,
Daily: opts.Daily,
Weekly: opts.Weekly,
Monthly: opts.Monthly,
Yearly: opts.Yearly,
Tags: opts.KeepTags,
Last: opts.Last,
Hourly: opts.Hourly,
Daily: opts.Daily,
Weekly: opts.Weekly,
Monthly: opts.Monthly,
Yearly: opts.Yearly,
NewerThan: ageCutoff,
Tags: opts.KeepTags,
}
if policy.Empty() && len(args) == 0 {