mirror of https://github.com/restic/restic.git
Merge 4e17d8cfb7
into 228b35f074
This commit is contained in:
commit
eb4d10ae64
|
@ -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,13 +213,17 @@ 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)
|
||||||
}
|
}
|
||||||
|
@ -240,6 +248,10 @@ func runForget(ctx context.Context, opts ForgetOptions, pruneOptions PruneOption
|
||||||
|
|
||||||
keep, remove, reasons := restic.ApplyPolicy(snapshotGroup, policy)
|
keep, remove, reasons := restic.ApplyPolicy(snapshotGroup, policy)
|
||||||
|
|
||||||
|
if !policy.Empty() && len(keep) == 0 {
|
||||||
|
return fmt.Errorf("refusing to delete last snapshot of snapshot group %v", key)
|
||||||
|
}
|
||||||
|
|
||||||
if len(keep) != 0 && !gopts.Quiet && !gopts.JSON {
|
if len(keep) != 0 && !gopts.Quiet && !gopts.JSON {
|
||||||
Printf("keep %d snapshots:\n", len(keep))
|
Printf("keep %d snapshots:\n", len(keep))
|
||||||
PrintSnapshots(globalOptions.stdout, keep, reasons, opts.Compact)
|
PrintSnapshots(globalOptions.stdout, keep, reasons, opts.Compact)
|
||||||
|
@ -263,7 +275,6 @@ func runForget(ctx context.Context, opts ForgetOptions, pruneOptions PruneOption
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if len(removeSnIDs) > 0 {
|
if len(removeSnIDs) > 0 {
|
||||||
if !opts.DryRun {
|
if !opts.DryRun {
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s == "" {
|
||||||
|
s = "remove"
|
||||||
|
} else {
|
||||||
s = "keep " + s
|
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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue