Skip to content

Commit

Permalink
Apply trivial review suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
depau committed Jan 6, 2022
1 parent efa82e6 commit 2a98115
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
4 changes: 2 additions & 2 deletions changelog/unreleased/issue-14
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Change: Implement rewrite command
Enhancement: Implement rewrite command

TODO: write here
We've added a new command which allows to rewrite existing snapshots removing unwanted contents.
34 changes: 15 additions & 19 deletions cmd/restic/cmd_rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ import (

var cmdRewrite = &cobra.Command{
Use: "rewrite [f] [all|snapshotID ...]",
Short: "Modify existing snapshots by deleting files",
Short: "Rewrite existing snapshots excluding specified files",
Long: `
The "rewrite" command excludes files from existing snapshots.
By default 'rewrite' will create new snapshot that will contains same data as
source snapshot except excluded data. All metadata (time, host, tags) will be preserved.
Special tag 'rewrite' will be added to new snapshot to distinguish it from source
(unless --inplace is used)
By default 'rewrite' will create new snapshot that will contain the same data as the
source snapshot, except excluded data. All metadata (time, host, tags) will be preserved.
The special tag 'rewrite' will be added to new snapshot to distinguish them from originals
(unless --forget is used)
If --inplace option is used, old snapshot will be removed from repository.
If --forget option is used, old snapshot will be removed from repository.
Snapshots to rewrite are specified using --host, --tag, --path or by providing list of snapshotID.
Alternatively it's possible to use special 'all' snapshot that will match all snapshots
Expand All @@ -45,11 +45,11 @@ Exit status is 0 if the command was successful, and non-zero if there was any er
// RewriteOptions collects all options for the ls command.
type RewriteOptions struct {
// TagOptions bundles all options for the 'tag' command.
Hosts []string
Paths []string
Tags restic.TagLists
Inplace bool
DryRun bool
Hosts []string
Paths []string
Tags restic.TagLists
Forget bool
DryRun bool

// Exclude options
Excludes []string
Expand All @@ -66,7 +66,7 @@ func init() {
f.StringArrayVarP(&rewriteOptions.Hosts, "host", "H", nil, "only consider snapshots for this `host`, when no snapshot ID is given (can be specified multiple times)")
f.Var(&rewriteOptions.Tags, "tag", "only consider snapshots which include this `taglist`, when no snapshot-ID is given")
f.StringArrayVar(&rewriteOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when no snapshot-ID is given")
f.BoolVarP(&rewriteOptions.Inplace, "inplace", "", false, "replace existing snapshots")
f.BoolVarP(&rewriteOptions.Forget, "forget", "", false, "Forget sources of rewritten snapshots")
f.BoolVarP(&rewriteOptions.DryRun, "dry-run", "n", false, "do not do anything, just print what would be done")

// Excludes
Expand Down Expand Up @@ -197,18 +197,14 @@ func rewriteSnapshot(ctx context.Context, repo *repository.Repository, sn *resti
if err != nil {
return false, err
}
err = repo.SaveIndex(ctx)
if err != nil {
return false, err
}

// Retain the original snapshot id over all tag changes.
if sn.Original == nil {
sn.Original = sn.ID()
}
*sn.Tree = filteredTree

if !opts.Inplace {
if !opts.Forget {
sn.AddTags([]string{"rewrite"})
}

Expand All @@ -222,13 +218,13 @@ func rewriteSnapshot(ctx context.Context, repo *repository.Repository, sn *resti
return true, err
}

if opts.Inplace {
if opts.Forget {
h := restic.Handle{Type: restic.SnapshotFile, Name: sn.ID().String()}
if err = repo.Backend().Remove(ctx, h); err != nil {
return false, err
}

debug.Log("old snapshot %v removed", sn.ID())
debug.Log("old snapshot %v forgotten", sn.ID())
}
Printf("new snapshot saved as %v\n", id)
return true, nil
Expand Down

0 comments on commit 2a98115

Please sign in to comment.