From 43ff971dfd94ee31b62a936d6101404d8128a315 Mon Sep 17 00:00:00 2001 From: Tobias Klein Date: Tue, 29 Aug 2017 18:29:46 +0200 Subject: [PATCH] new sub-option for backup: time New option to specify the timestamp for a backup --- cmd/restic/cmd_backup.go | 12 +++++++++++- internal/archiver/archive_reader.go | 2 +- internal/archiver/archiver.go | 4 ++-- internal/archiver/testing.go | 3 ++- internal/restic/snapshot.go | 4 ++-- internal/restic/testing.go | 2 +- 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index 09a4ba2f5..d0150da24 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -66,6 +66,7 @@ type BackupOptions struct { Tags []string Hostname string FilesFrom string + TimeStamp string } var backupOptions BackupOptions @@ -86,6 +87,7 @@ func init() { f.StringArrayVar(&backupOptions.Tags, "tag", nil, "add a `tag` for the new snapshot (can be specified multiple times)") f.StringVar(&backupOptions.Hostname, "hostname", "", "set the `hostname` for the snapshot manually") f.StringVar(&backupOptions.FilesFrom, "files-from", "", "read the files to backup from file (can be combined with file args)") + f.StringVar(&backupOptions.TimeStamp, "time", "", "time of the backup (ex. '2012-11-01 22:08:41') (default: now)") } func newScanProgress(gopts GlobalOptions) *restic.Progress { @@ -493,7 +495,15 @@ func runBackup(opts BackupOptions, gopts GlobalOptions, args []string) error { Warnf("%s\rwarning for %s: %v\n", ClearLine(), dir, err) } - _, id, err := arch.Snapshot(context.TODO(), newArchiveProgress(gopts, stat), target, opts.Tags, opts.Hostname, parentSnapshotID) + timeStamp := time.Now() + if opts.TimeStamp != "" { + timeStamp, err = time.Parse(TimeFormat, opts.TimeStamp) + if err != nil { + return errors.Fatalf("error in time option: %v\n", err) + } + } + + _, id, err := arch.Snapshot(context.TODO(), newArchiveProgress(gopts, stat), target, opts.Tags, opts.Hostname, parentSnapshotID, timeStamp) if err != nil { return err } diff --git a/internal/archiver/archive_reader.go b/internal/archiver/archive_reader.go index eaf422aab..b6dab993b 100644 --- a/internal/archiver/archive_reader.go +++ b/internal/archiver/archive_reader.go @@ -28,7 +28,7 @@ func (r *Reader) Archive(ctx context.Context, name string, rd io.Reader, p *rest } debug.Log("start archiving %s", name) - sn, err := restic.NewSnapshot([]string{name}, r.Tags, r.Hostname) + sn, err := restic.NewSnapshot([]string{name}, r.Tags, r.Hostname, time.Now()) if err != nil { return nil, restic.ID{}, err } diff --git a/internal/archiver/archiver.go b/internal/archiver/archiver.go index 3117ec509..9e7ee307d 100644 --- a/internal/archiver/archiver.go +++ b/internal/archiver/archiver.go @@ -651,7 +651,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(ctx context.Context, p *restic.Progress, paths, tags []string, hostname string, parentID *restic.ID) (*restic.Snapshot, restic.ID, error) { +func (arch *Archiver) Snapshot(ctx context.Context, p *restic.Progress, paths, tags []string, hostname string, parentID *restic.ID, time time.Time) (*restic.Snapshot, restic.ID, error) { paths = unique(paths) sort.Sort(baseNameSlice(paths)) @@ -666,7 +666,7 @@ func (arch *Archiver) Snapshot(ctx context.Context, p *restic.Progress, paths, t defer p.Done() // create new snapshot - sn, err := restic.NewSnapshot(paths, tags, hostname) + sn, err := restic.NewSnapshot(paths, tags, hostname, time) if err != nil { return nil, restic.ID{}, err } diff --git a/internal/archiver/testing.go b/internal/archiver/testing.go index 37abea83f..d700135b4 100644 --- a/internal/archiver/testing.go +++ b/internal/archiver/testing.go @@ -3,6 +3,7 @@ package archiver import ( "context" "testing" + "time" "github.com/restic/restic/internal/restic" ) @@ -10,7 +11,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(context.TODO(), nil, []string{path}, []string{"test"}, "localhost", parent) + sn, _, err := arch.Snapshot(context.TODO(), nil, []string{path}, []string{"test"}, "localhost", parent, time.Now()) if err != nil { t.Fatal(err) } diff --git a/internal/restic/snapshot.go b/internal/restic/snapshot.go index c6e0dad50..f73be6937 100644 --- a/internal/restic/snapshot.go +++ b/internal/restic/snapshot.go @@ -29,7 +29,7 @@ type Snapshot struct { // NewSnapshot returns an initialized snapshot struct for the current user and // time. -func NewSnapshot(paths []string, tags []string, hostname string) (*Snapshot, error) { +func NewSnapshot(paths []string, tags []string, hostname string, time time.Time) (*Snapshot, error) { for i, path := range paths { if p, err := filepath.Abs(path); err != nil { paths[i] = p @@ -38,7 +38,7 @@ func NewSnapshot(paths []string, tags []string, hostname string) (*Snapshot, err sn := &Snapshot{ Paths: paths, - Time: time.Now(), + Time: time, Tags: tags, Hostname: hostname, } diff --git a/internal/restic/testing.go b/internal/restic/testing.go index 7ddba731a..5e1f3372b 100644 --- a/internal/restic/testing.go +++ b/internal/restic/testing.go @@ -164,7 +164,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}, []string{"test"}, "foo") + snapshot, err := NewSnapshot([]string{fakedir}, []string{"test"}, "foo", time.Now()) if err != nil { t.Fatal(err) }