forked from TrueCloudLab/restic
forget: Add --unsafe-allow-remove-all option
To prevent accidentally wiping all snapshots from a repository, that option can only be used if either a snapshot filter or a keep policy is specified. Essentially, the option allows `forget --tag something --unsafe-allow-remove-all` calls to remove all snapshots with a specific tag.
This commit is contained in:
parent
5b7952e426
commit
57f9739573
3 changed files with 17 additions and 1792 deletions
|
@ -92,6 +92,8 @@ type ForgetOptions struct {
|
||||||
WithinYearly restic.Duration
|
WithinYearly restic.Duration
|
||||||
KeepTags restic.TagLists
|
KeepTags restic.TagLists
|
||||||
|
|
||||||
|
UnsafeAllowRemoveAll bool
|
||||||
|
|
||||||
restic.SnapshotFilter
|
restic.SnapshotFilter
|
||||||
Compact bool
|
Compact bool
|
||||||
|
|
||||||
|
@ -121,6 +123,7 @@ func init() {
|
||||||
f.VarP(&forgetOptions.WithinMonthly, "keep-within-monthly", "", "keep monthly snapshots that are newer than `duration` (eg. 1y5m7d2h) relative to the latest snapshot")
|
f.VarP(&forgetOptions.WithinMonthly, "keep-within-monthly", "", "keep monthly snapshots that are newer than `duration` (eg. 1y5m7d2h) relative to the latest snapshot")
|
||||||
f.VarP(&forgetOptions.WithinYearly, "keep-within-yearly", "", "keep yearly snapshots that are newer than `duration` (eg. 1y5m7d2h) relative to the latest snapshot")
|
f.VarP(&forgetOptions.WithinYearly, "keep-within-yearly", "", "keep yearly snapshots that are newer than `duration` (eg. 1y5m7d2h) relative to the latest snapshot")
|
||||||
f.Var(&forgetOptions.KeepTags, "keep-tag", "keep snapshots with this `taglist` (can be specified multiple times)")
|
f.Var(&forgetOptions.KeepTags, "keep-tag", "keep snapshots with this `taglist` (can be specified multiple times)")
|
||||||
|
f.BoolVar(&forgetOptions.UnsafeAllowRemoveAll, "unsafe-allow-remove-all", false, "allow deleting all snapshots of a snapshot group")
|
||||||
|
|
||||||
initMultiSnapshotFilter(f, &forgetOptions.SnapshotFilter, false)
|
initMultiSnapshotFilter(f, &forgetOptions.SnapshotFilter, false)
|
||||||
f.StringArrayVar(&forgetOptions.Hosts, "hostname", nil, "only consider snapshots with the given `hostname` (can be specified multiple times)")
|
f.StringArrayVar(&forgetOptions.Hosts, "hostname", nil, "only consider snapshots with the given `hostname` (can be specified multiple times)")
|
||||||
|
@ -223,7 +226,14 @@ func runForget(ctx context.Context, opts ForgetOptions, pruneOptions PruneOption
|
||||||
}
|
}
|
||||||
|
|
||||||
if policy.Empty() {
|
if policy.Empty() {
|
||||||
return errors.Fatal("no policy was specified, no snapshots will be removed")
|
if opts.UnsafeAllowRemoveAll {
|
||||||
|
if opts.SnapshotFilter.Empty() {
|
||||||
|
return errors.Fatal("--unsafe-allow-remove-all is not allowed unless a snapshot filter option is specified")
|
||||||
|
}
|
||||||
|
// UnsafeAllowRemoveAll together with snapshot filter is fine
|
||||||
|
} else {
|
||||||
|
return errors.Fatal("no policy was specified, no snapshots will be removed")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printer.P("Applying Policy: %v\n", policy)
|
printer.P("Applying Policy: %v\n", policy)
|
||||||
|
|
|
@ -94,7 +94,11 @@ func (e ExpirePolicy) String() (s string) {
|
||||||
s += fmt.Sprintf("all snapshots within %s of the newest", e.Within)
|
s += fmt.Sprintf("all snapshots within %s of the newest", e.Within)
|
||||||
}
|
}
|
||||||
|
|
||||||
s = "keep " + s
|
if s == "" {
|
||||||
|
s = "remove"
|
||||||
|
} else {
|
||||||
|
s = "keep " + s
|
||||||
|
}
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
@ -186,16 +190,6 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots, reason
|
||||||
// sort newest snapshots first
|
// sort newest snapshots first
|
||||||
sort.Stable(list)
|
sort.Stable(list)
|
||||||
|
|
||||||
if p.Empty() {
|
|
||||||
for _, sn := range list {
|
|
||||||
reasons = append(reasons, KeepReason{
|
|
||||||
Snapshot: sn,
|
|
||||||
Matches: []string{"policy is empty"},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return list, remove, reasons
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(list) == 0 {
|
if len(list) == 0 {
|
||||||
return list, nil, nil
|
return list, nil, nil
|
||||||
}
|
}
|
||||||
|
|
1781
internal/restic/testdata/policy_keep_snapshots_0
vendored
1781
internal/restic/testdata/policy_keep_snapshots_0
vendored
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue