Add --prune switch to forget

This commit is contained in:
Alexander Neumann
2017-02-21 10:58:30 +01:00
parent 4e2f8145f5
commit 685f5ebbd1
3 changed files with 55 additions and 3 deletions

View File

@@ -38,6 +38,7 @@ type ForgetOptions struct {
Tags []string
DryRun bool
Prune bool
}
var forgetOptions ForgetOptions
@@ -58,6 +59,7 @@ func init() {
f.StringSliceVar(&forgetOptions.Tags, "tag", []string{}, "only forget snapshots with the `tag` (can be specified multiple times)")
f.BoolVarP(&forgetOptions.DryRun, "dry-run", "n", false, "do not delete anything, just print what would be done")
f.BoolVar(&forgetOptions.Prune, "prune", false, "automatically run the 'prune' command if snapshots have been removed")
}
func printSnapshots(w io.Writer, snapshots restic.Snapshots) {
@@ -188,6 +190,7 @@ func runForget(opts ForgetOptions, gopts GlobalOptions, args []string) error {
snapshotGroups[k] = list
}
removeSnapshots := 0
for key, snapshotGroup := range snapshotGroups {
Printf("snapshots for host %v, directories %v:\n\n", key.Hostname, key.Dirs)
keep, remove := restic.ApplyPolicy(snapshotGroup, policy)
@@ -200,6 +203,8 @@ func runForget(opts ForgetOptions, gopts GlobalOptions, args []string) error {
printSnapshots(globalOptions.stdout, remove)
Printf("\n")
removeSnapshots += len(remove)
if !opts.DryRun {
for _, sn := range remove {
h := restic.Handle{Type: restic.SnapshotFile, Name: sn.ID().String()}
@@ -211,5 +216,12 @@ func runForget(opts ForgetOptions, gopts GlobalOptions, args []string) error {
}
}
if removeSnapshots > 0 && opts.Prune {
Printf("%d snapshots have been removed, running prune\n", removeSnapshots)
if !opts.DryRun {
return pruneRepository(gopts, repo)
}
}
return nil
}

View File

@@ -75,7 +75,11 @@ func runPrune(gopts GlobalOptions) error {
return err
}
err = repo.LoadIndex()
return pruneRepository(gopts, repo)
}
func pruneRepository(gopts GlobalOptions, repo restic.Repository) error {
err := repo.LoadIndex()
if err != nil {
return err
}