2014-08-04 20:55:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-07-25 15:05:45 +00:00
|
|
|
"encoding/hex"
|
2014-08-04 20:55:54 +00:00
|
|
|
"fmt"
|
2014-11-25 21:38:14 +00:00
|
|
|
"io"
|
2014-11-24 20:12:32 +00:00
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2014-08-04 20:55:54 +00:00
|
|
|
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic"
|
2014-08-04 20:55:54 +00:00
|
|
|
)
|
|
|
|
|
2014-11-25 21:38:14 +00:00
|
|
|
type Table struct {
|
|
|
|
Header string
|
|
|
|
Rows [][]interface{}
|
|
|
|
|
|
|
|
RowFormat string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTable() Table {
|
|
|
|
return Table{
|
|
|
|
Rows: [][]interface{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-22 10:21:14 +00:00
|
|
|
func (t Table) Write(w io.Writer) error {
|
2014-11-25 21:38:14 +00:00
|
|
|
_, err := fmt.Fprintln(w, t.Header)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = fmt.Fprintln(w, strings.Repeat("-", 70))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, row := range t.Rows {
|
|
|
|
_, err = fmt.Fprintf(w, t.RowFormat+"\n", row...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-24 20:12:32 +00:00
|
|
|
const TimeFormat = "2006-01-02 15:04:05"
|
|
|
|
|
2015-06-21 11:02:56 +00:00
|
|
|
type CmdSnapshots struct {
|
2016-05-10 20:12:33 +00:00
|
|
|
Host string `short:"h" long:"host" description:"Host Filter"`
|
|
|
|
Paths []string `short:"p" long:"path" description:"Path Filter (absolute path) (can be specified multiple times)"`
|
2016-05-10 19:23:18 +00:00
|
|
|
|
2015-06-21 11:02:56 +00:00
|
|
|
global *GlobalOptions
|
|
|
|
}
|
2014-12-07 15:30:52 +00:00
|
|
|
|
2014-11-30 21:39:58 +00:00
|
|
|
func init() {
|
2014-12-07 15:30:52 +00:00
|
|
|
_, err := parser.AddCommand("snapshots",
|
|
|
|
"show snapshots",
|
|
|
|
"The snapshots command lists all snapshots stored in a repository",
|
2015-06-21 11:02:56 +00:00
|
|
|
&CmdSnapshots{global: &globalOpts})
|
2014-12-07 15:30:52 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd CmdSnapshots) Usage() string {
|
|
|
|
return ""
|
2014-11-30 21:39:58 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 15:30:52 +00:00
|
|
|
func (cmd CmdSnapshots) Execute(args []string) error {
|
2014-08-04 20:55:54 +00:00
|
|
|
if len(args) != 0 {
|
2016-08-28 20:19:48 +00:00
|
|
|
return restic.Fatalf("wrong number of arguments, usage: %s", cmd.Usage())
|
2014-12-07 15:30:52 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 12:36:46 +00:00
|
|
|
repo, err := cmd.global.OpenRepository()
|
2014-12-07 15:30:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-04 20:55:54 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 13:05:20 +00:00
|
|
|
lock, err := lockRepo(repo)
|
|
|
|
defer unlockRepo(lock)
|
2015-06-27 12:40:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-25 21:38:14 +00:00
|
|
|
tab := NewTable()
|
2016-05-10 19:51:56 +00:00
|
|
|
tab.Header = fmt.Sprintf("%-8s %-19s %-10s %s", "ID", "Date", "Host", "Directory")
|
2014-11-25 21:38:14 +00:00
|
|
|
tab.RowFormat = "%-8s %-19s %-10s %s"
|
2014-11-24 20:12:32 +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-05-10 19:51:56 +00:00
|
|
|
if restic.SamePaths(sn.Paths, cmd.Paths) && (cmd.Host == "" || cmd.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
|
|
|
|
2016-09-01 14:04:29 +00:00
|
|
|
plen, err := repo.PrefixLength(restic.SnapshotFile)
|
2014-11-25 21:38:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2015-07-25 15:05:45 +00:00
|
|
|
id := sn.ID()
|
|
|
|
tab.Rows = append(tab.Rows, []interface{}{hex.EncodeToString(id[:plen/2]), sn.Time.Format(TimeFormat), sn.Hostname, sn.Paths[0]})
|
2015-03-02 13:48:47 +00:00
|
|
|
|
|
|
|
if len(sn.Paths) > 1 {
|
2015-07-06 20:26:31 +00:00
|
|
|
for _, path := range sn.Paths[1:] {
|
2015-03-02 13:48:47 +00:00
|
|
|
tab.Rows = append(tab.Rows, []interface{}{"", "", "", path})
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2014-08-04 20:55:54 +00:00
|
|
|
return nil
|
|
|
|
}
|