2017-03-05 05:20:32 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-03-08 19:28:44 +00:00
|
|
|
"context"
|
|
|
|
|
2017-03-05 05:20:32 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-03-05 05:20:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var cmdTag = &cobra.Command{
|
|
|
|
Use: "tag [flags] [snapshot-ID ...]",
|
2017-09-11 16:32:44 +00:00
|
|
|
Short: "Modify tags on snapshots",
|
2017-03-05 05:20:32 +00:00
|
|
|
Long: `
|
|
|
|
The "tag" command allows you to modify tags on exiting snapshots.
|
|
|
|
|
|
|
|
You can either set/replace the entire set of tags on a snapshot, or
|
|
|
|
add tags to/remove tags from the existing set.
|
|
|
|
|
|
|
|
When no snapshot-ID is given, all snapshots matching the host, tag and path filter criteria are modified.
|
|
|
|
`,
|
2017-08-06 19:02:16 +00:00
|
|
|
DisableAutoGenTag: true,
|
2017-03-05 05:20:32 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runTag(tagOptions, globalOptions, args)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// TagOptions bundles all options for the 'tag' command.
|
|
|
|
type TagOptions struct {
|
|
|
|
Host string
|
|
|
|
Paths []string
|
2017-07-09 10:45:49 +00:00
|
|
|
Tags restic.TagLists
|
2017-03-05 05:20:32 +00:00
|
|
|
SetTags []string
|
|
|
|
AddTags []string
|
|
|
|
RemoveTags []string
|
|
|
|
}
|
|
|
|
|
|
|
|
var tagOptions TagOptions
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmdRoot.AddCommand(cmdTag)
|
|
|
|
|
|
|
|
tagFlags := cmdTag.Flags()
|
|
|
|
tagFlags.StringSliceVar(&tagOptions.SetTags, "set", nil, "`tag` which will replace the existing tags (can be given multiple times)")
|
|
|
|
tagFlags.StringSliceVar(&tagOptions.AddTags, "add", nil, "`tag` which will be added to the existing tags (can be given multiple times)")
|
|
|
|
tagFlags.StringSliceVar(&tagOptions.RemoveTags, "remove", nil, "`tag` which will be removed from the existing tags (can be given multiple times)")
|
|
|
|
|
2017-03-08 19:28:44 +00:00
|
|
|
tagFlags.StringVarP(&tagOptions.Host, "host", "H", "", "only consider snapshots for this `host`, when no snapshot ID is given")
|
2017-07-09 10:45:49 +00:00
|
|
|
tagFlags.Var(&tagOptions.Tags, "tag", "only consider snapshots which include this `taglist`, when no snapshot-ID is given")
|
2017-07-07 01:19:06 +00:00
|
|
|
tagFlags.StringArrayVar(&tagOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when no snapshot-ID is given")
|
2017-03-05 05:20:32 +00:00
|
|
|
}
|
|
|
|
|
use global context for check, debug, dump, find, forget, init, key,
list, mount, tag, unlock commands
gh-1434
2017-12-06 12:02:55 +00:00
|
|
|
func changeTags(ctx context.Context, repo *repository.Repository, sn *restic.Snapshot, setTags, addTags, removeTags []string) (bool, error) {
|
2017-03-05 05:20:32 +00:00
|
|
|
var changed bool
|
|
|
|
|
|
|
|
if len(setTags) != 0 {
|
2017-03-05 16:43:18 +00:00
|
|
|
// Setting the tag to an empty string really means no tags.
|
2017-03-05 05:20:32 +00:00
|
|
|
if len(setTags) == 1 && setTags[0] == "" {
|
|
|
|
setTags = nil
|
|
|
|
}
|
|
|
|
sn.Tags = setTags
|
|
|
|
changed = true
|
|
|
|
} else {
|
2017-03-05 16:43:18 +00:00
|
|
|
changed = sn.AddTags(addTags)
|
|
|
|
if sn.RemoveTags(removeTags) {
|
|
|
|
changed = true
|
2017-03-05 05:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if changed {
|
2017-03-05 16:51:57 +00:00
|
|
|
// Retain the original snapshot id over all tag changes.
|
|
|
|
if sn.Original == nil {
|
|
|
|
sn.Original = sn.ID()
|
|
|
|
}
|
|
|
|
|
2017-03-05 05:20:32 +00:00
|
|
|
// Save the new snapshot.
|
use global context for check, debug, dump, find, forget, init, key,
list, mount, tag, unlock commands
gh-1434
2017-12-06 12:02:55 +00:00
|
|
|
id, err := repo.SaveJSONUnpacked(ctx, restic.SnapshotFile, sn)
|
2017-03-05 05:20:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("new snapshot saved as %v", id)
|
2017-03-05 05:20:32 +00:00
|
|
|
|
use global context for check, debug, dump, find, forget, init, key,
list, mount, tag, unlock commands
gh-1434
2017-12-06 12:02:55 +00:00
|
|
|
if err = repo.Flush(ctx); err != nil {
|
2017-03-05 05:20:32 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the old snapshot.
|
|
|
|
h := restic.Handle{Type: restic.SnapshotFile, Name: sn.ID().String()}
|
use global context for check, debug, dump, find, forget, init, key,
list, mount, tag, unlock commands
gh-1434
2017-12-06 12:02:55 +00:00
|
|
|
if err = repo.Backend().Remove(ctx, h); err != nil {
|
2017-03-05 05:20:32 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("old snapshot %v removed", sn.ID())
|
|
|
|
}
|
|
|
|
return changed, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runTag(opts TagOptions, gopts GlobalOptions, args []string) error {
|
|
|
|
if len(opts.SetTags) == 0 && len(opts.AddTags) == 0 && len(opts.RemoveTags) == 0 {
|
|
|
|
return errors.Fatal("nothing to do!")
|
|
|
|
}
|
|
|
|
if len(opts.SetTags) != 0 && (len(opts.AddTags) != 0 || len(opts.RemoveTags) != 0) {
|
|
|
|
return errors.Fatal("--set and --add/--remove cannot be given at the same time")
|
|
|
|
}
|
|
|
|
|
|
|
|
repo, err := OpenRepository(gopts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !gopts.NoLock {
|
2017-10-27 19:06:34 +00:00
|
|
|
Verbosef("create exclusive lock for repository\n")
|
2017-03-05 05:20:32 +00:00
|
|
|
lock, err := lockRepoExclusive(repo)
|
|
|
|
defer unlockRepo(lock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
changeCnt := 0
|
2017-03-08 19:28:44 +00:00
|
|
|
ctx, cancel := context.WithCancel(gopts.ctx)
|
|
|
|
defer cancel()
|
2017-07-09 10:45:49 +00:00
|
|
|
for sn := range FindFilteredSnapshots(ctx, repo, opts.Host, opts.Tags, opts.Paths, args) {
|
use global context for check, debug, dump, find, forget, init, key,
list, mount, tag, unlock commands
gh-1434
2017-12-06 12:02:55 +00:00
|
|
|
changed, err := changeTags(ctx, repo, sn, opts.SetTags, opts.AddTags, opts.RemoveTags)
|
2017-03-05 05:20:32 +00:00
|
|
|
if err != nil {
|
2017-03-08 19:28:44 +00:00
|
|
|
Warnf("unable to modify the tags for snapshot ID %q, ignoring: %v\n", sn.ID(), err)
|
2017-03-05 05:20:32 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if changed {
|
|
|
|
changeCnt++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if changeCnt == 0 {
|
2017-10-27 19:06:34 +00:00
|
|
|
Verbosef("no snapshots were modified\n")
|
2017-03-05 05:20:32 +00:00
|
|
|
} else {
|
2017-10-27 19:06:34 +00:00
|
|
|
Verbosef("modified tags on %v snapshots\n", changeCnt)
|
2017-03-05 05:20:32 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|