Add tags to 'backup' and 'snapshots' commands

This commit is contained in:
Alexander Neumann 2016-09-13 20:13:04 +02:00
parent 1f83635267
commit 673bce936e
8 changed files with 50 additions and 36 deletions

View file

@ -25,6 +25,7 @@ type CmdBackup struct {
ExcludeFile string `long:"exclude-file" description:"Read exclude-patterns from file"`
Stdin bool `long:"stdin" description:"read backup data from stdin"`
StdinFilename string `long:"stdin-filename" default:"stdin" description:"file name to use when reading from stdin"`
Tags []string `long:"tag" description:"Add a tag (can be specified multiple times)"`
global *GlobalOptions
}
@ -259,7 +260,7 @@ func (cmd CmdBackup) readFromStdin(args []string) error {
return err
}
_, id, err := archiver.ArchiveReader(repo, cmd.newArchiveStdinProgress(), os.Stdin, cmd.StdinFilename)
_, id, err := archiver.ArchiveReader(repo, cmd.newArchiveStdinProgress(), os.Stdin, cmd.StdinFilename, cmd.Tags)
if err != nil {
return err
}
@ -380,7 +381,7 @@ func (cmd CmdBackup) Execute(args []string) error {
return nil
}
_, id, err := arch.Snapshot(cmd.newArchiveProgress(stat), target, parentSnapshotID)
_, id, err := arch.Snapshot(cmd.newArchiveProgress(stat), target, cmd.Tags, parentSnapshotID)
if err != nil {
return err
}

View file

@ -1,7 +1,6 @@
package main
import (
"encoding/hex"
"fmt"
"io"
"os"
@ -85,8 +84,8 @@ func (cmd CmdSnapshots) Execute(args []string) error {
}
tab := NewTable()
tab.Header = fmt.Sprintf("%-8s %-19s %-10s %s", "ID", "Date", "Host", "Directory")
tab.RowFormat = "%-8s %-19s %-10s %s"
tab.Header = fmt.Sprintf("%-8s %-19s %-10s %-10s %s", "ID", "Date", "Host", "Tags", "Directory")
tab.RowFormat = "%-8s %-19s %-10s %-10s %s"
done := make(chan struct{})
defer close(done)
@ -115,22 +114,35 @@ func (cmd CmdSnapshots) Execute(args []string) error {
}
plen, err := repo.PrefixLength(restic.SnapshotFile)
if err != nil {
return err
}
for _, sn := range list {
if len(sn.Paths) == 0 {
continue
}
id := sn.ID()
tab.Rows = append(tab.Rows, []interface{}{hex.EncodeToString(id[:plen/2]), sn.Time.Format(TimeFormat), sn.Hostname, sn.Paths[0]})
if len(sn.Paths) > 1 {
for _, path := range sn.Paths[1:] {
tab.Rows = append(tab.Rows, []interface{}{"", "", "", path})
firstTag := ""
if len(sn.Tags) > 0 {
firstTag = sn.Tags[0]
}
tab.Rows = append(tab.Rows, []interface{}{sn.ID().Str(), sn.Time.Format(TimeFormat), sn.Hostname, firstTag, sn.Paths[0]})
rows := len(sn.Paths)
if len(sn.Tags) > rows {
rows = len(sn.Tags)
}
for i := 1; i < rows; i++ {
path := ""
if len(sn.Paths) > i {
path = sn.Paths[i]
}
tag := ""
if len(sn.Tags) > i {
tag = sn.Tags[i]
}
tab.Rows = append(tab.Rows, []interface{}{"", "", "", tag, path})
}
}

View file

@ -77,7 +77,7 @@ func TestArchiveReader(t *testing.T) {
f := fakeFile(t, seed, size)
sn, id, err := ArchiveReader(repo, nil, f, "fakefile")
sn, id, err := ArchiveReader(repo, nil, f, "fakefile", []string{"test"})
if err != nil {
t.Fatalf("ArchiveReader() returned error %v", err)
}
@ -107,7 +107,7 @@ func BenchmarkArchiveReader(t *testing.B) {
t.ResetTimer()
for i := 0; i < t.N; i++ {
_, _, err := ArchiveReader(repo, nil, bytes.NewReader(buf), "fakefile")
_, _, err := ArchiveReader(repo, nil, bytes.NewReader(buf), "fakefile", []string{"test"})
if err != nil {
t.Fatal(err)
}

View file

@ -104,7 +104,7 @@ func archiveDirectory(b testing.TB) {
arch := archiver.New(repo)
_, id, err := arch.Snapshot(nil, []string{BenchArchiveDirectory}, nil)
_, id, err := arch.Snapshot(nil, []string{BenchArchiveDirectory}, nil, nil)
OK(b, err)
b.Logf("snapshot archived as %v", id)

View file

@ -247,7 +247,7 @@ func TestCheckerModifiedData(t *testing.T) {
test.OK(t, repo.Init(test.TestPassword))
arch := archiver.New(repo)
_, id, err := arch.Snapshot(nil, []string{"."}, nil)
_, id, err := arch.Snapshot(nil, []string{"."}, nil, nil)
test.OK(t, err)
t.Logf("archived as %v", id.Str())

View file

@ -104,6 +104,22 @@ func (sn *Snapshot) fillUserInfo() error {
return err
}
// HasTags returns true if the snapshot has all the tags.
func (sn *Snapshot) HasTags(tags []string) bool {
nextTag:
for _, tag := range tags {
for _, snTag := range sn.Tags {
if tag == snTag {
continue nextTag
}
}
return false
}
return true
}
// SamePaths compares the Snapshot's paths and provided paths are exactly the same
func SamePaths(expected, actual []string) bool {
if expected == nil || actual == nil {

View file

@ -34,21 +34,6 @@ type SnapshotFilter struct {
Tags []string
}
func hasTags(have, want []string) bool {
nextTag:
for _, tag := range want {
for _, snTag := range have {
if tag == snTag {
continue nextTag
}
}
return false
}
return true
}
// FilterSnapshots returns the snapshots from s which match the filter f.
func FilterSnapshots(s Snapshots, f SnapshotFilter) (result Snapshots) {
for _, snap := range s {
@ -64,7 +49,7 @@ func FilterSnapshots(s Snapshots, f SnapshotFilter) (result Snapshots) {
continue
}
if !hasTags(snap.Tags, f.Tags) {
if !snap.HasTags(f.Tags) {
continue
}

View file

@ -24,7 +24,7 @@ func TestWalkTree(t *testing.T) {
// archive a few files
arch := archiver.New(repo)
sn, _, err := arch.Snapshot(nil, dirs, nil)
sn, _, err := arch.Snapshot(nil, dirs, nil, nil)
OK(t, err)
// flush repo, write all packs