2014-08-04 20:55:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-03-08 19:24:58 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2014-08-04 20:55:54 +00:00
|
|
|
"fmt"
|
2017-03-05 04:24:11 +00:00
|
|
|
"io"
|
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-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal"
|
2014-08-04 20:55:54 +00:00
|
|
|
)
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
var cmdSnapshots = &cobra.Command{
|
2017-03-08 19:24:58 +00:00
|
|
|
Use: "snapshots [snapshotID ...]",
|
2016-09-17 10:36:05 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-08 19:09:24 +00:00
|
|
|
// SnapshotOptions bundles all options for the snapshots command.
|
2016-09-17 10:36:05 +00:00
|
|
|
type SnapshotOptions struct {
|
|
|
|
Host string
|
2017-07-09 10:45:49 +00:00
|
|
|
Tags restic.TagLists
|
2016-09-17 10:36:05 +00:00
|
|
|
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()
|
2017-03-08 19:24:58 +00:00
|
|
|
f.StringVarP(&snapshotOptions.Host, "host", "H", "", "only consider snapshots for this `host`")
|
2017-07-09 10:45:49 +00:00
|
|
|
f.Var(&snapshotOptions.Tags, "tag", "only consider snapshots which include this `taglist` (can be specified multiple times)")
|
2017-07-07 01:19:06 +00:00
|
|
|
f.StringArrayVar(&snapshotOptions.Paths, "path", nil, "only consider 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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-08 19:24:58 +00:00
|
|
|
ctx, cancel := context.WithCancel(gopts.ctx)
|
|
|
|
defer cancel()
|
2016-05-10 19:23:18 +00:00
|
|
|
|
2017-03-08 19:24:58 +00:00
|
|
|
var list restic.Snapshots
|
2017-07-09 10:45:49 +00:00
|
|
|
for sn := range FindFilteredSnapshots(ctx, repo, opts.Host, opts.Tags, opts.Paths, args) {
|
2017-03-08 19:24:58 +00:00
|
|
|
list = append(list, sn)
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
2017-03-08 19:24:58 +00:00
|
|
|
sort.Sort(sort.Reverse(list))
|
2014-08-04 20:55:54 +00:00
|
|
|
|
2017-02-12 20:43:39 +00:00
|
|
|
if gopts.JSON {
|
2017-03-05 04:24:11 +00:00
|
|
|
err := printSnapshotsJSON(gopts.stdout, list)
|
2017-02-12 20:43:39 +00:00
|
|
|
if err != nil {
|
2017-03-05 04:24:11 +00:00
|
|
|
Warnf("error printing snapshot: %v\n", err)
|
2017-02-12 20:43:39 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-07 13:19:36 +00:00
|
|
|
PrintSnapshots(gopts.stdout, list)
|
2017-02-12 20:43:39 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-07 13:19:36 +00:00
|
|
|
// PrintSnapshots prints a text table of the snapshots in list to stdout.
|
2017-03-08 19:24:58 +00:00
|
|
|
func PrintSnapshots(stdout io.Writer, list restic.Snapshots) {
|
2017-02-12 20:43:39 +00:00
|
|
|
|
2017-03-05 04:32:01 +00:00
|
|
|
// Determine the max widths for host and tag.
|
|
|
|
maxHost, maxTag := 10, 6
|
|
|
|
for _, sn := range list {
|
|
|
|
if len(sn.Hostname) > maxHost {
|
|
|
|
maxHost = len(sn.Hostname)
|
|
|
|
}
|
|
|
|
for _, tag := range sn.Tags {
|
|
|
|
if len(tag) > maxTag {
|
|
|
|
maxTag = len(tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 20:43:39 +00:00
|
|
|
tab := NewTable()
|
2017-03-05 04:32:01 +00:00
|
|
|
tab.Header = fmt.Sprintf("%-8s %-19s %-*s %-*s %-3s %s", "ID", "Date", -maxHost, "Host", -maxTag, "Tags", "", "Directory")
|
|
|
|
tab.RowFormat = fmt.Sprintf("%%-8s %%-19s %%%ds %%%ds %%-3s %%s", -maxHost, -maxTag)
|
2017-02-12 20:43:39 +00:00
|
|
|
|
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-03-06 01:49:15 +00:00
|
|
|
if rows < len(sn.Tags) {
|
|
|
|
rows = len(sn.Tags)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2017-03-05 04:24:11 +00:00
|
|
|
tab.Write(stdout)
|
2017-02-12 20:43:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 19:24:58 +00:00
|
|
|
// Snapshot helps to print Snaphots as JSON with their ID included.
|
2017-02-12 20:43:39 +00:00
|
|
|
type Snapshot struct {
|
|
|
|
*restic.Snapshot
|
|
|
|
|
2017-03-05 16:50:11 +00:00
|
|
|
ID *restic.ID `json:"id"`
|
2017-02-12 20:43:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// printSnapshotsJSON writes the JSON representation of list to stdout.
|
2017-03-08 19:24:58 +00:00
|
|
|
func printSnapshotsJSON(stdout io.Writer, list restic.Snapshots) error {
|
2017-02-12 20:43:39 +00:00
|
|
|
|
|
|
|
var snapshots []Snapshot
|
|
|
|
|
|
|
|
for _, sn := range list {
|
|
|
|
|
|
|
|
k := Snapshot{
|
|
|
|
Snapshot: sn,
|
2017-03-05 16:50:11 +00:00
|
|
|
ID: sn.ID(),
|
2017-02-12 20:43:39 +00:00
|
|
|
}
|
|
|
|
snapshots = append(snapshots, k)
|
|
|
|
}
|
|
|
|
|
2017-03-05 04:24:11 +00:00
|
|
|
return json.NewEncoder(stdout).Encode(snapshots)
|
2014-08-04 20:55:54 +00:00
|
|
|
}
|