Add tags to snapshots and filter

This commit is contained in:
Alexander Neumann 2016-09-13 20:12:55 +02:00
parent 2d7e1b5804
commit 1f83635267
18 changed files with 403 additions and 68 deletions

View file

@ -13,9 +13,9 @@ import (
// ArchiveReader reads from the reader and archives the data. Returned is the
// resulting snapshot and its ID.
func ArchiveReader(repo restic.Repository, p *restic.Progress, rd io.Reader, name string) (*restic.Snapshot, restic.ID, error) {
func ArchiveReader(repo restic.Repository, p *restic.Progress, rd io.Reader, name string, tags []string) (*restic.Snapshot, restic.ID, error) {
debug.Log("ArchiveReader", "start archiving %s", name)
sn, err := restic.NewSnapshot([]string{name})
sn, err := restic.NewSnapshot([]string{name}, tags)
if err != nil {
return nil, restic.ID{}, err
}

View file

@ -633,7 +633,7 @@ func (p baseNameSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Snapshot creates a snapshot of the given paths. If parentrestic.ID is set, this is
// used to compare the files to the ones archived at the time this snapshot was
// taken.
func (arch *Archiver) Snapshot(p *restic.Progress, paths []string, parentID *restic.ID) (*restic.Snapshot, restic.ID, error) {
func (arch *Archiver) Snapshot(p *restic.Progress, paths, tags []string, parentID *restic.ID) (*restic.Snapshot, restic.ID, error) {
paths = unique(paths)
sort.Sort(baseNameSlice(paths))
@ -649,7 +649,7 @@ func (arch *Archiver) Snapshot(p *restic.Progress, paths []string, parentID *res
defer p.Done()
// create new snapshot
sn, err := restic.NewSnapshot(paths)
sn, err := restic.NewSnapshot(paths, tags)
if err != nil {
return nil, restic.ID{}, err
}

View file

@ -8,7 +8,7 @@ import (
// TestSnapshot creates a new snapshot of path.
func TestSnapshot(t testing.TB, repo restic.Repository, path string, parent *restic.ID) *restic.Snapshot {
arch := New(repo)
sn, _, err := arch.Snapshot(nil, []string{path}, parent)
sn, _, err := arch.Snapshot(nil, []string{path}, []string{"test"}, parent)
if err != nil {
t.Fatal(err)
}

View file

@ -21,13 +21,14 @@ type Snapshot struct {
UID uint32 `json:"uid,omitempty"`
GID uint32 `json:"gid,omitempty"`
Excludes []string `json:"excludes,omitempty"`
Tags []string `json:"tags,omitempty"`
id *ID // plaintext ID, used during restore
}
// NewSnapshot returns an initialized snapshot struct for the current user and
// time.
func NewSnapshot(paths []string) (*Snapshot, error) {
func NewSnapshot(paths []string, tags []string) (*Snapshot, error) {
for i, path := range paths {
if p, err := filepath.Abs(path); err != nil {
paths[i] = p
@ -37,6 +38,7 @@ func NewSnapshot(paths []string) (*Snapshot, error) {
sn := &Snapshot{
Paths: paths,
Time: time.Now(),
Tags: tags,
}
hn, err := os.Hostname()

View file

@ -31,6 +31,22 @@ type SnapshotFilter struct {
Hostname string
Username string
Paths []string
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.
@ -48,6 +64,10 @@ func FilterSnapshots(s Snapshots, f SnapshotFilter) (result Snapshots) {
continue
}
if !hasTags(snap.Tags, f.Tags) {
continue
}
result = append(result, snap)
}

View file

@ -22,25 +22,25 @@ func parseTimeUTC(s string) time.Time {
}
var testFilterSnapshots = restic.Snapshots{
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-01 01:02:03"), Paths: []string{"/usr", "/bin"}},
{Hostname: "bar", Username: "testuser", Time: parseTimeUTC("2016-01-01 01:03:03"), Paths: []string{"/usr", "/bin"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-03 07:02:03"), Paths: []string{"/usr", "/bin"}},
{Hostname: "bar", Username: "testuser", Time: parseTimeUTC("2016-01-01 07:08:03"), Paths: []string{"/usr", "/bin"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-04 10:23:03"), Paths: []string{"/usr", "/bin"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-04 11:23:03"), Paths: []string{"/usr", "/bin"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-04 12:23:03"), Paths: []string{"/usr", "/bin"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-04 12:24:03"), Paths: []string{"/usr", "/bin"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-04 12:28:03"), Paths: []string{"/usr", "/bin"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-04 12:30:03"), Paths: []string{"/usr", "/bin"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-04 16:23:03"), Paths: []string{"/usr", "/bin"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-05 09:02:03"), Paths: []string{"/usr", "/bin"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-06 08:02:03"), Paths: []string{"/usr", "/bin"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-07 10:02:03"), Paths: []string{"/usr", "/bin"}},
{Hostname: "foo", Username: "root", Time: parseTimeUTC("2016-01-08 20:02:03"), Paths: []string{"/usr", "/sbin"}},
{Hostname: "foo", Username: "root", Time: parseTimeUTC("2016-01-09 21:02:03"), Paths: []string{"/usr", "/sbin"}},
{Hostname: "bar", Username: "root", Time: parseTimeUTC("2016-01-12 21:02:03"), Paths: []string{"/usr", "/sbin"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-12 21:08:03"), Paths: []string{"/usr", "/bin"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-18 12:02:03"), Paths: []string{"/usr", "/bin"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-01 01:02:03"), Paths: []string{"/usr", "/bin"}, Tags: []string{"foo"}},
{Hostname: "bar", Username: "testuser", Time: parseTimeUTC("2016-01-01 01:03:03"), Paths: []string{"/usr", "/bin"}, Tags: []string{"foo"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-03 07:02:03"), Paths: []string{"/usr", "/bin"}, Tags: []string{"foo"}},
{Hostname: "bar", Username: "testuser", Time: parseTimeUTC("2016-01-01 07:08:03"), Paths: []string{"/usr", "/bin"}, Tags: []string{"foo"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-04 10:23:03"), Paths: []string{"/usr", "/bin"}, Tags: []string{"foo"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-04 11:23:03"), Paths: []string{"/usr", "/bin"}, Tags: []string{"foo"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-04 12:23:03"), Paths: []string{"/usr", "/bin"}, Tags: []string{"foo"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-04 12:24:03"), Paths: []string{"/usr", "/bin"}, Tags: []string{"test"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-04 12:28:03"), Paths: []string{"/usr", "/bin"}, Tags: []string{"test"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-04 12:30:03"), Paths: []string{"/usr", "/bin"}, Tags: []string{"test", "foo", "bar"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-04 16:23:03"), Paths: []string{"/usr", "/bin"}, Tags: []string{"test", "test2"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-05 09:02:03"), Paths: []string{"/usr", "/bin"}, Tags: []string{"foo"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-06 08:02:03"), Paths: []string{"/usr", "/bin"}, Tags: []string{"fox"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-07 10:02:03"), Paths: []string{"/usr", "/bin"}, Tags: []string{"fox"}},
{Hostname: "foo", Username: "root", Time: parseTimeUTC("2016-01-08 20:02:03"), Paths: []string{"/usr", "/sbin"}, Tags: []string{"foo"}},
{Hostname: "foo", Username: "root", Time: parseTimeUTC("2016-01-09 21:02:03"), Paths: []string{"/usr", "/sbin"}, Tags: []string{"fox"}},
{Hostname: "bar", Username: "root", Time: parseTimeUTC("2016-01-12 21:02:03"), Paths: []string{"/usr", "/sbin"}, Tags: []string{"foo"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-12 21:08:03"), Paths: []string{"/usr", "/bin"}, Tags: []string{"bar"}},
{Hostname: "foo", Username: "testuser", Time: parseTimeUTC("2016-01-18 12:02:03"), Paths: []string{"/usr", "/bin"}, Tags: []string{"bar"}},
}
var filterTests = []restic.SnapshotFilter{
@ -50,6 +50,10 @@ var filterTests = []restic.SnapshotFilter{
{Paths: []string{"/usr", "/bin"}},
{Hostname: "bar", Paths: []string{"/usr", "/bin"}},
{Hostname: "foo", Username: "root", Paths: []string{"/usr", "/sbin"}},
{Tags: []string{"foo"}},
{Tags: []string{"fox"}, Username: "root"},
{Tags: []string{"foo", "test"}},
{Tags: []string{"foo", "test2"}},
}
func TestFilterSnapshots(t *testing.T) {

View file

@ -10,6 +10,6 @@ import (
func TestNewSnapshot(t *testing.T) {
paths := []string{"/home/foobar"}
_, err := restic.NewSnapshot(paths)
_, err := restic.NewSnapshot(paths, nil)
OK(t, err)
}

View file

@ -7,7 +7,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"bar"
]
},
{
"time": "2016-01-12T21:08:03Z",
@ -17,7 +20,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"bar"
]
},
{
"time": "2016-01-09T21:02:03Z",
@ -27,7 +33,10 @@
"/sbin"
],
"hostname": "foo",
"username": "root"
"username": "root",
"tags": [
"fox"
]
},
{
"time": "2016-01-08T20:02:03Z",
@ -37,7 +46,10 @@
"/sbin"
],
"hostname": "foo",
"username": "root"
"username": "root",
"tags": [
"foo"
]
},
{
"time": "2016-01-07T10:02:03Z",
@ -47,7 +59,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"fox"
]
},
{
"time": "2016-01-06T08:02:03Z",
@ -57,7 +72,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"fox"
]
},
{
"time": "2016-01-05T09:02:03Z",
@ -67,7 +85,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-04T16:23:03Z",
@ -77,7 +98,11 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"test",
"test2"
]
},
{
"time": "2016-01-04T12:30:03Z",
@ -87,7 +112,12 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"test",
"foo",
"bar"
]
},
{
"time": "2016-01-04T12:28:03Z",
@ -97,7 +127,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"test"
]
},
{
"time": "2016-01-04T12:24:03Z",
@ -107,7 +140,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"test"
]
},
{
"time": "2016-01-04T12:23:03Z",
@ -117,7 +153,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-04T11:23:03Z",
@ -127,7 +166,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-04T10:23:03Z",
@ -137,7 +179,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-03T07:02:03Z",
@ -147,7 +192,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-01T01:02:03Z",
@ -157,6 +205,9 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"foo"
]
}
]

View file

@ -7,7 +7,10 @@
"/sbin"
],
"hostname": "bar",
"username": "root"
"username": "root",
"tags": [
"foo"
]
},
{
"time": "2016-01-09T21:02:03Z",
@ -17,7 +20,10 @@
"/sbin"
],
"hostname": "foo",
"username": "root"
"username": "root",
"tags": [
"fox"
]
},
{
"time": "2016-01-08T20:02:03Z",
@ -27,6 +33,9 @@
"/sbin"
],
"hostname": "foo",
"username": "root"
"username": "root",
"tags": [
"foo"
]
}
]

View file

@ -7,7 +7,10 @@
"/sbin"
],
"hostname": "foo",
"username": "root"
"username": "root",
"tags": [
"fox"
]
},
{
"time": "2016-01-08T20:02:03Z",
@ -17,6 +20,9 @@
"/sbin"
],
"hostname": "foo",
"username": "root"
"username": "root",
"tags": [
"foo"
]
}
]

View file

@ -7,7 +7,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"bar"
]
},
{
"time": "2016-01-12T21:08:03Z",
@ -17,7 +20,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"bar"
]
},
{
"time": "2016-01-07T10:02:03Z",
@ -27,7 +33,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"fox"
]
},
{
"time": "2016-01-06T08:02:03Z",
@ -37,7 +46,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"fox"
]
},
{
"time": "2016-01-05T09:02:03Z",
@ -47,7 +59,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-04T16:23:03Z",
@ -57,7 +72,11 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"test",
"test2"
]
},
{
"time": "2016-01-04T12:30:03Z",
@ -67,7 +86,12 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"test",
"foo",
"bar"
]
},
{
"time": "2016-01-04T12:28:03Z",
@ -77,7 +101,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"test"
]
},
{
"time": "2016-01-04T12:24:03Z",
@ -87,7 +114,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"test"
]
},
{
"time": "2016-01-04T12:23:03Z",
@ -97,7 +127,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-04T11:23:03Z",
@ -107,7 +140,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-04T10:23:03Z",
@ -117,7 +153,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-03T07:02:03Z",
@ -127,7 +166,10 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-01T07:08:03Z",
@ -137,7 +179,10 @@
"/bin"
],
"hostname": "bar",
"username": "testuser"
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-01T01:03:03Z",
@ -147,7 +192,10 @@
"/bin"
],
"hostname": "bar",
"username": "testuser"
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-01T01:02:03Z",
@ -157,6 +205,9 @@
"/bin"
],
"hostname": "foo",
"username": "testuser"
"username": "testuser",
"tags": [
"foo"
]
}
]

View file

@ -7,7 +7,10 @@
"/bin"
],
"hostname": "bar",
"username": "testuser"
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-01T01:03:03Z",
@ -17,6 +20,9 @@
"/bin"
],
"hostname": "bar",
"username": "testuser"
"username": "testuser",
"tags": [
"foo"
]
}
]

View file

@ -7,7 +7,10 @@
"/sbin"
],
"hostname": "foo",
"username": "root"
"username": "root",
"tags": [
"fox"
]
},
{
"time": "2016-01-08T20:02:03Z",
@ -17,6 +20,9 @@
"/sbin"
],
"hostname": "foo",
"username": "root"
"username": "root",
"tags": [
"foo"
]
}
]

147
src/restic/testdata/filter_snapshots_6 vendored Normal file
View file

@ -0,0 +1,147 @@
[
{
"time": "2016-01-12T21:02:03Z",
"tree": null,
"paths": [
"/usr",
"/sbin"
],
"hostname": "bar",
"username": "root",
"tags": [
"foo"
]
},
{
"time": "2016-01-08T20:02:03Z",
"tree": null,
"paths": [
"/usr",
"/sbin"
],
"hostname": "foo",
"username": "root",
"tags": [
"foo"
]
},
{
"time": "2016-01-05T09:02:03Z",
"tree": null,
"paths": [
"/usr",
"/bin"
],
"hostname": "foo",
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-04T12:30:03Z",
"tree": null,
"paths": [
"/usr",
"/bin"
],
"hostname": "foo",
"username": "testuser",
"tags": [
"test",
"foo",
"bar"
]
},
{
"time": "2016-01-04T12:23:03Z",
"tree": null,
"paths": [
"/usr",
"/bin"
],
"hostname": "foo",
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-04T11:23:03Z",
"tree": null,
"paths": [
"/usr",
"/bin"
],
"hostname": "foo",
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-04T10:23:03Z",
"tree": null,
"paths": [
"/usr",
"/bin"
],
"hostname": "foo",
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-03T07:02:03Z",
"tree": null,
"paths": [
"/usr",
"/bin"
],
"hostname": "foo",
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-01T07:08:03Z",
"tree": null,
"paths": [
"/usr",
"/bin"
],
"hostname": "bar",
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-01T01:03:03Z",
"tree": null,
"paths": [
"/usr",
"/bin"
],
"hostname": "bar",
"username": "testuser",
"tags": [
"foo"
]
},
{
"time": "2016-01-01T01:02:03Z",
"tree": null,
"paths": [
"/usr",
"/bin"
],
"hostname": "foo",
"username": "testuser",
"tags": [
"foo"
]
}
]

15
src/restic/testdata/filter_snapshots_7 vendored Normal file
View file

@ -0,0 +1,15 @@
[
{
"time": "2016-01-09T21:02:03Z",
"tree": null,
"paths": [
"/usr",
"/sbin"
],
"hostname": "foo",
"username": "root",
"tags": [
"fox"
]
}
]

17
src/restic/testdata/filter_snapshots_8 vendored Normal file
View file

@ -0,0 +1,17 @@
[
{
"time": "2016-01-04T12:30:03Z",
"tree": null,
"paths": [
"/usr",
"/bin"
],
"hostname": "foo",
"username": "testuser",
"tags": [
"test",
"foo",
"bar"
]
}
]

View file

@ -0,0 +1 @@
null

View file

@ -154,7 +154,7 @@ func TestCreateSnapshot(t testing.TB, repo Repository, at time.Time, depth int,
t.Logf("create fake snapshot at %s with seed %d", at, seed)
fakedir := fmt.Sprintf("fakedir-at-%v", at.Format("2006-01-02 15:04:05"))
snapshot, err := NewSnapshot([]string{fakedir})
snapshot, err := NewSnapshot([]string{fakedir}, []string{"test"})
if err != nil {
t.Fatal(err)
}