This commit is contained in:
Michael Eischer 2024-04-19 18:12:03 +02:00 committed by GitHub
commit eb4d10ae64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 71 additions and 52 deletions

View File

@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"strconv" "strconv"
@ -88,6 +89,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
@ -117,6 +120,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)")
@ -209,58 +213,65 @@ func runForget(ctx context.Context, opts ForgetOptions, pruneOptions PruneOption
Tags: opts.KeepTags, Tags: opts.KeepTags,
} }
if policy.Empty() && len(args) == 0 { if policy.Empty() {
if !gopts.JSON { if opts.UnsafeAllowRemoveAll {
Verbosef("no policy was specified, no snapshots will be removed\n") 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")
} }
} }
if !policy.Empty() { if !gopts.JSON {
if !gopts.JSON { Verbosef("Applying Policy: %v\n", policy)
Verbosef("Applying Policy: %v\n", policy) }
}
for k, snapshotGroup := range snapshotGroups { for k, snapshotGroup := range snapshotGroups {
if gopts.Verbose >= 1 && !gopts.JSON { if gopts.Verbose >= 1 && !gopts.JSON {
err = PrintSnapshotGroupHeader(globalOptions.stdout, k) err = PrintSnapshotGroupHeader(globalOptions.stdout, k)
if err != nil { if err != nil {
return err
}
}
var key restic.SnapshotGroupKey
if json.Unmarshal([]byte(k), &key) != nil {
return err return err
} }
}
var fg ForgetGroup var key restic.SnapshotGroupKey
fg.Tags = key.Tags if json.Unmarshal([]byte(k), &key) != nil {
fg.Host = key.Hostname return err
fg.Paths = key.Paths }
keep, remove, reasons := restic.ApplyPolicy(snapshotGroup, policy) var fg ForgetGroup
fg.Tags = key.Tags
fg.Host = key.Hostname
fg.Paths = key.Paths
if len(keep) != 0 && !gopts.Quiet && !gopts.JSON { keep, remove, reasons := restic.ApplyPolicy(snapshotGroup, policy)
Printf("keep %d snapshots:\n", len(keep))
PrintSnapshots(globalOptions.stdout, keep, reasons, opts.Compact)
Printf("\n")
}
fg.Keep = asJSONSnapshots(keep)
if len(remove) != 0 && !gopts.Quiet && !gopts.JSON { if !policy.Empty() && len(keep) == 0 {
Printf("remove %d snapshots:\n", len(remove)) return fmt.Errorf("refusing to delete last snapshot of snapshot group %v", key)
PrintSnapshots(globalOptions.stdout, remove, nil, opts.Compact) }
Printf("\n")
}
fg.Remove = asJSONSnapshots(remove)
fg.Reasons = asJSONKeeps(reasons) if len(keep) != 0 && !gopts.Quiet && !gopts.JSON {
Printf("keep %d snapshots:\n", len(keep))
PrintSnapshots(globalOptions.stdout, keep, reasons, opts.Compact)
Printf("\n")
}
fg.Keep = asJSONSnapshots(keep)
jsonGroups = append(jsonGroups, &fg) if len(remove) != 0 && !gopts.Quiet && !gopts.JSON {
Printf("remove %d snapshots:\n", len(remove))
PrintSnapshots(globalOptions.stdout, remove, nil, opts.Compact)
Printf("\n")
}
fg.Remove = asJSONSnapshots(remove)
for _, sn := range remove { fg.Reasons = asJSONKeeps(reasons)
removeSnIDs.Insert(*sn.ID())
} jsonGroups = append(jsonGroups, &fg)
for _, sn := range remove {
removeSnIDs.Insert(*sn.ID())
} }
} }
} }

View File

@ -24,7 +24,7 @@ type SnapshotFilter struct {
TimestampLimit time.Time TimestampLimit time.Time
} }
func (f *SnapshotFilter) empty() bool { func (f *SnapshotFilter) Empty() bool {
return len(f.Hosts)+len(f.Tags)+len(f.Paths) == 0 return len(f.Hosts)+len(f.Tags)+len(f.Paths) == 0
} }
@ -173,7 +173,7 @@ func (f *SnapshotFilter) FindAll(ctx context.Context, be Lister, loader LoaderUn
} }
// Give the user some indication their filters are not used. // Give the user some indication their filters are not used.
if !usedFilter && !f.empty() { if !usedFilter && !f.Empty() {
return fn("filters", nil, errors.Errorf("explicit snapshot ids are given")) return fn("filters", nil, errors.Errorf("explicit snapshot ids are given"))
} }
return nil return nil

View File

@ -66,6 +66,20 @@ type SnapshotGroupKey struct {
Tags []string `json:"tags"` Tags []string `json:"tags"`
} }
func (s *SnapshotGroupKey) String() string {
var parts []string
if s.Hostname != "" {
parts = append(parts, fmt.Sprintf("host %v", s.Hostname))
}
if len(s.Paths) != 0 {
parts = append(parts, fmt.Sprintf("path %v", s.Paths))
}
if len(s.Tags) != 0 {
parts = append(parts, fmt.Sprintf("tags %v", s.Tags))
}
return strings.Join(parts, ", ")
}
// GroupSnapshots takes a list of snapshots and a grouping criteria and creates // GroupSnapshots takes a list of snapshots and a grouping criteria and creates
// a grouped list of snapshots. // a grouped list of snapshots.
func GroupSnapshots(snapshots Snapshots, groupBy SnapshotGroupByOptions) (map[string]Snapshots, bool, error) { func GroupSnapshots(snapshots Snapshots, groupBy SnapshotGroupByOptions) (map[string]Snapshots, bool, error) {

View File

@ -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
} }