2014-08-04 20:55:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-11-24 20:12:32 +00:00
|
|
|
"os"
|
2016-09-01 20:17:37 +00:00
|
|
|
"restic/errors"
|
2014-11-24 20:12:32 +00:00
|
|
|
"sort"
|
2016-09-17 10:36:05 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2014-08-04 20:55:54 +00:00
|
|
|
|
2017-02-12 20:43:39 +00:00
|
|
|
"encoding/json"
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic"
|
2014-08-04 20:55:54 +00:00
|
|
|
)
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
var cmdSnapshots = &cobra.Command{
|
|
|
|
Use: "snapshots",
|
|
|
|
Short: "list all snapshots",
|
|
|
|
Long: `
|
2017-02-13 15:05:25 +00:00
|
|
|
The "snapshots" command lists all snapshots stored in the repository.
|
2016-09-17 10:36:05 +00:00
|
|
|
`,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runSnapshots(snapshotOptions, globalOptions, args)
|
|
|
|
},
|
2014-11-25 21:38:14 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
// SnapshotOptions bundle all options for the snapshots command.
|
|
|
|
type SnapshotOptions struct {
|
|
|
|
Host string
|
|
|
|
Paths []string
|
2014-11-25 21:38:14 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
var snapshotOptions SnapshotOptions
|
2014-12-07 15:30:52 +00:00
|
|
|
|
2014-11-30 21:39:58 +00:00
|
|
|
func init() {
|
2016-09-17 10:36:05 +00:00
|
|
|
cmdRoot.AddCommand(cmdSnapshots)
|
2014-12-07 15:30:52 +00:00
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
f := cmdSnapshots.Flags()
|
|
|
|
f.StringVar(&snapshotOptions.Host, "host", "", "only print snapshots for this host")
|
2016-09-29 18:39:55 +00:00
|
|
|
f.StringSliceVar(&snapshotOptions.Paths, "path", []string{}, "only print snapshots for this `path` (can be specified multiple times)")
|
2014-11-30 21:39:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
func runSnapshots(opts SnapshotOptions, gopts GlobalOptions, args []string) error {
|
2014-08-04 20:55:54 +00:00
|
|
|
if len(args) != 0 {
|
2017-02-10 18:39:49 +00:00
|
|
|
return errors.Fatal("wrong number of arguments")
|
2014-12-07 15:30:52 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
repo, err := OpenRepository(gopts)
|
2014-12-07 15:30:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-04 20:55:54 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
if !gopts.NoLock {
|
|
|
|
lock, err := lockRepo(repo)
|
|
|
|
defer unlockRepo(lock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-27 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
|
2014-12-05 20:45:49 +00:00
|
|
|
list := []*restic.Snapshot{}
|
2016-09-01 14:04:29 +00:00
|
|
|
for id := range repo.List(restic.SnapshotFile, done) {
|
2015-06-27 12:36:46 +00:00
|
|
|
sn, err := restic.LoadSnapshot(repo, id)
|
2014-11-24 20:12:32 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error loading snapshot %s: %v\n", id, err)
|
2015-03-28 10:50:23 +00:00
|
|
|
continue
|
2014-11-24 20:12:32 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
if restic.SamePaths(sn.Paths, opts.Paths) && (opts.Host == "" || opts.Host == sn.Hostname) {
|
2016-05-10 19:23:18 +00:00
|
|
|
pos := sort.Search(len(list), func(i int) bool {
|
|
|
|
return list[i].Time.After(sn.Time)
|
|
|
|
})
|
|
|
|
|
|
|
|
if pos < len(list) {
|
|
|
|
list = append(list, nil)
|
|
|
|
copy(list[pos+1:], list[pos:])
|
|
|
|
list[pos] = sn
|
|
|
|
} else {
|
|
|
|
list = append(list, sn)
|
|
|
|
}
|
2014-11-24 20:12:32 +00:00
|
|
|
}
|
2016-05-10 19:23:18 +00:00
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
2014-08-04 20:55:54 +00:00
|
|
|
|
2017-02-12 20:43:39 +00:00
|
|
|
if gopts.JSON {
|
|
|
|
err := printSnapshotsJSON(list)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error printing snapshot: %v\n", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
printSnapshotsReadable(list)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// printSnapshotsReadable prints a text table of the snapshots in list to stdout.
|
|
|
|
func printSnapshotsReadable(list []*restic.Snapshot) {
|
|
|
|
|
|
|
|
tab := NewTable()
|
|
|
|
tab.Header = fmt.Sprintf("%-8s %-19s %-10s %-10s %-3s %s", "ID", "Date", "Host", "Tags", "", "Directory")
|
|
|
|
tab.RowFormat = "%-8s %-19s %-10s %-10s %-3s %s"
|
|
|
|
|
2014-11-24 20:12:32 +00:00
|
|
|
for _, sn := range list {
|
2015-03-02 13:48:47 +00:00
|
|
|
if len(sn.Paths) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-09-13 18:13:04 +00:00
|
|
|
firstTag := ""
|
|
|
|
if len(sn.Tags) > 0 {
|
|
|
|
firstTag = sn.Tags[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
rows := len(sn.Paths)
|
2017-01-18 19:46:00 +00:00
|
|
|
|
|
|
|
treeElement := " "
|
|
|
|
if rows != 1 {
|
|
|
|
treeElement = "┌──"
|
|
|
|
}
|
|
|
|
|
|
|
|
tab.Rows = append(tab.Rows, []interface{}{sn.ID().Str(), sn.Time.Format(TimeFormat), sn.Hostname, firstTag, treeElement, sn.Paths[0]})
|
|
|
|
|
2016-09-13 18:13:04 +00:00
|
|
|
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]
|
2015-03-02 13:48:47 +00:00
|
|
|
}
|
2016-09-13 18:13:04 +00:00
|
|
|
|
|
|
|
tag := ""
|
|
|
|
if len(sn.Tags) > i {
|
|
|
|
tag = sn.Tags[i]
|
|
|
|
}
|
|
|
|
|
2017-01-18 19:46:00 +00:00
|
|
|
treeElement := "│"
|
|
|
|
if i == (rows - 1) {
|
|
|
|
treeElement = "└──"
|
|
|
|
}
|
|
|
|
|
|
|
|
tab.Rows = append(tab.Rows, []interface{}{"", "", "", tag, treeElement, path})
|
2015-03-02 13:48:47 +00:00
|
|
|
}
|
2014-11-24 20:12:32 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 10:21:14 +00:00
|
|
|
tab.Write(os.Stdout)
|
2014-11-25 21:38:14 +00:00
|
|
|
|
2017-02-12 20:43:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshot helps to print Snaphots as JSON
|
|
|
|
type Snapshot struct {
|
|
|
|
*restic.Snapshot
|
|
|
|
|
|
|
|
ID string `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// printSnapshotsJSON writes the JSON representation of list to stdout.
|
|
|
|
func printSnapshotsJSON(list []*restic.Snapshot) error {
|
|
|
|
|
|
|
|
var snapshots []Snapshot
|
|
|
|
|
|
|
|
for _, sn := range list {
|
|
|
|
|
|
|
|
k := Snapshot{
|
|
|
|
Snapshot: sn,
|
|
|
|
ID: sn.ID().String(),
|
|
|
|
}
|
|
|
|
snapshots = append(snapshots, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.NewEncoder(os.Stdout).Encode(snapshots)
|
|
|
|
|
2014-08-04 20:55:54 +00:00
|
|
|
}
|