2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-24 21:57:41 +00:00

Merge pull request #1518 from restic/fix-1317-again

forget: Also run prune when only IDs are forgotten
This commit is contained in:
Alexander Neumann 2018-01-02 20:39:12 +01:00
commit 2bc4d200d4

View File

@ -162,9 +162,6 @@ func runForget(opts ForgetOptions, gopts GlobalOptions, args []string) error {
snapshotGroups[string(k)] = append(snapshotGroups[string(k)], sn) snapshotGroups[string(k)] = append(snapshotGroups[string(k)], sn)
} }
} }
if len(args) > 0 {
return nil
}
policy := restic.ExpirePolicy{ policy := restic.ExpirePolicy{
Last: opts.Last, Last: opts.Last,
@ -176,56 +173,57 @@ func runForget(opts ForgetOptions, gopts GlobalOptions, args []string) error {
Tags: opts.KeepTags, Tags: opts.KeepTags,
} }
if policy.Empty() { if policy.Empty() && len(args) == 0 {
Verbosef("no policy was specified, no snapshots will be removed\n") Verbosef("no policy was specified, no snapshots will be removed\n")
return nil
} }
for k, snapshotGroup := range snapshotGroups { if !policy.Empty() {
var key key for k, snapshotGroup := range snapshotGroups {
if json.Unmarshal([]byte(k), &key) != nil { var key key
return err if json.Unmarshal([]byte(k), &key) != nil {
} return err
}
// Info // Info
Verbosef("snapshots") Verbosef("snapshots")
var infoStrings []string var infoStrings []string
if GroupByTag { if GroupByTag {
infoStrings = append(infoStrings, "tags ["+strings.Join(key.Tags, ", ")+"]") infoStrings = append(infoStrings, "tags ["+strings.Join(key.Tags, ", ")+"]")
} }
if GroupByHost { if GroupByHost {
infoStrings = append(infoStrings, "host ["+key.Hostname+"]") infoStrings = append(infoStrings, "host ["+key.Hostname+"]")
} }
if GroupByPath { if GroupByPath {
infoStrings = append(infoStrings, "paths ["+strings.Join(key.Paths, ", ")+"]") infoStrings = append(infoStrings, "paths ["+strings.Join(key.Paths, ", ")+"]")
} }
if infoStrings != nil { if infoStrings != nil {
Verbosef(" for (" + strings.Join(infoStrings, ", ") + ")") Verbosef(" for (" + strings.Join(infoStrings, ", ") + ")")
} }
Verbosef(":\n\n") Verbosef(":\n\n")
keep, remove := restic.ApplyPolicy(snapshotGroup, policy) keep, remove := restic.ApplyPolicy(snapshotGroup, policy)
if len(keep) != 0 && !gopts.Quiet { if len(keep) != 0 && !gopts.Quiet {
Printf("keep %d snapshots:\n", len(keep)) Printf("keep %d snapshots:\n", len(keep))
PrintSnapshots(globalOptions.stdout, keep, opts.Compact) PrintSnapshots(globalOptions.stdout, keep, opts.Compact)
Printf("\n") Printf("\n")
} }
if len(remove) != 0 && !gopts.Quiet { if len(remove) != 0 && !gopts.Quiet {
Printf("remove %d snapshots:\n", len(remove)) Printf("remove %d snapshots:\n", len(remove))
PrintSnapshots(globalOptions.stdout, remove, opts.Compact) PrintSnapshots(globalOptions.stdout, remove, opts.Compact)
Printf("\n") Printf("\n")
} }
removeSnapshots += len(remove) removeSnapshots += len(remove)
if !opts.DryRun { if !opts.DryRun {
for _, sn := range remove { for _, sn := range remove {
h := restic.Handle{Type: restic.SnapshotFile, Name: sn.ID().String()} h := restic.Handle{Type: restic.SnapshotFile, Name: sn.ID().String()}
err = repo.Backend().Remove(gopts.ctx, h) err = repo.Backend().Remove(gopts.ctx, h)
if err != nil { if err != nil {
return err return err
}
} }
} }
} }