2014-04-28 00:00:15 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-09-23 22:39:12 +02:00
|
|
|
"os"
|
2015-01-04 22:39:30 +01:00
|
|
|
"path/filepath"
|
2014-11-16 21:29:11 +01:00
|
|
|
"strings"
|
2014-11-16 22:50:20 +01:00
|
|
|
"time"
|
2014-04-28 00:00:15 +02:00
|
|
|
|
2014-12-05 21:45:49 +01:00
|
|
|
"github.com/restic/restic"
|
|
|
|
"github.com/restic/restic/backend"
|
2014-11-16 21:29:11 +01:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2014-04-28 00:00:15 +02:00
|
|
|
)
|
|
|
|
|
2014-12-07 16:30:52 +01:00
|
|
|
type CmdBackup struct{}
|
|
|
|
|
2014-11-30 22:39:58 +01:00
|
|
|
func init() {
|
2014-12-07 16:30:52 +01:00
|
|
|
_, err := parser.AddCommand("backup",
|
|
|
|
"save file/directory",
|
|
|
|
"The backup command creates a snapshot of a file or directory",
|
|
|
|
&CmdBackup{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-11-30 22:39:58 +01:00
|
|
|
}
|
|
|
|
|
2014-11-16 21:29:11 +01:00
|
|
|
func format_bytes(c uint64) string {
|
|
|
|
b := float64(c)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case c > 1<<40:
|
2014-11-23 14:53:46 +01:00
|
|
|
return fmt.Sprintf("%.3f TiB", b/(1<<40))
|
2014-11-16 21:29:11 +01:00
|
|
|
case c > 1<<30:
|
2014-11-23 14:53:46 +01:00
|
|
|
return fmt.Sprintf("%.3f GiB", b/(1<<30))
|
2014-11-16 21:29:11 +01:00
|
|
|
case c > 1<<20:
|
2014-11-23 14:53:46 +01:00
|
|
|
return fmt.Sprintf("%.3f MiB", b/(1<<20))
|
2014-11-16 21:29:11 +01:00
|
|
|
case c > 1<<10:
|
2014-11-23 14:53:46 +01:00
|
|
|
return fmt.Sprintf("%.3f KiB", b/(1<<10))
|
2014-11-16 21:29:11 +01:00
|
|
|
default:
|
2014-11-23 12:05:43 +01:00
|
|
|
return fmt.Sprintf("%dB", c)
|
2014-11-16 21:29:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-04 18:23:00 +01:00
|
|
|
func format_seconds(sec uint64) string {
|
2014-11-23 12:05:43 +01:00
|
|
|
hours := sec / 3600
|
|
|
|
sec -= hours * 3600
|
|
|
|
min := sec / 60
|
|
|
|
sec -= min * 60
|
|
|
|
if hours > 0 {
|
|
|
|
return fmt.Sprintf("%d:%02d:%02d", hours, min, sec)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%d:%02d", min, sec)
|
|
|
|
}
|
|
|
|
|
2015-01-04 18:23:00 +01:00
|
|
|
func format_duration(d time.Duration) string {
|
|
|
|
sec := uint64(d / time.Second)
|
|
|
|
return format_seconds(sec)
|
|
|
|
}
|
|
|
|
|
2014-12-05 21:45:49 +01:00
|
|
|
func print_tree2(indent int, t *restic.Tree) {
|
2014-11-16 21:29:11 +01:00
|
|
|
for _, node := range *t {
|
2015-01-04 22:39:30 +01:00
|
|
|
if node.Tree() != nil {
|
2014-11-16 21:29:11 +01:00
|
|
|
fmt.Printf("%s%s/\n", strings.Repeat(" ", indent), node.Name)
|
2015-01-04 22:39:30 +01:00
|
|
|
print_tree2(indent+1, node.Tree())
|
2014-11-16 21:29:11 +01:00
|
|
|
} else {
|
|
|
|
fmt.Printf("%s%s\n", strings.Repeat(" ", indent), node.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-07 16:30:52 +01:00
|
|
|
func (cmd CmdBackup) Usage() string {
|
|
|
|
return "DIR/FILE [snapshot-ID]"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd CmdBackup) Execute(args []string) error {
|
|
|
|
if len(args) == 0 || len(args) > 2 {
|
|
|
|
return fmt.Errorf("wrong number of parameters, Usage: %s", cmd.Usage())
|
|
|
|
}
|
|
|
|
|
2014-12-21 18:10:19 +01:00
|
|
|
s, err := OpenRepo()
|
2014-12-07 16:30:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-04-28 00:00:15 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 22:34:21 +01:00
|
|
|
var parentSnapshotID backend.ID
|
|
|
|
|
2014-04-28 00:00:15 +02:00
|
|
|
target := args[0]
|
2014-11-30 22:34:21 +01:00
|
|
|
if len(args) > 1 {
|
2014-12-21 18:10:19 +01:00
|
|
|
parentSnapshotID, err = s.FindSnapshot(args[1])
|
2014-11-30 22:34:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid id %q: %v", args[1], err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("found parent snapshot %v\n", parentSnapshotID)
|
|
|
|
}
|
2014-04-28 00:00:15 +02:00
|
|
|
|
2015-01-04 22:39:30 +01:00
|
|
|
fmt.Printf("scan %s\n", target)
|
2014-11-16 21:29:11 +01:00
|
|
|
|
2015-01-04 18:23:00 +01:00
|
|
|
scanProgress := restic.NewProgress(time.Second)
|
2014-11-16 21:29:11 +01:00
|
|
|
if terminal.IsTerminal(int(os.Stdout.Fd())) {
|
2015-01-04 18:23:00 +01:00
|
|
|
scanProgress.F = func(s restic.Stat, d time.Duration, ticker bool) {
|
|
|
|
fmt.Printf("\x1b[2K\r[%s] %d directories, %d files, %s", format_duration(d), s.Dirs, s.Files, format_bytes(s.Bytes))
|
|
|
|
}
|
|
|
|
scanProgress.D = func(s restic.Stat, d time.Duration, ticker bool) {
|
|
|
|
fmt.Printf("\nDone in %s\n", format_duration(d))
|
|
|
|
}
|
2014-11-16 21:29:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add filter
|
|
|
|
// arch.Filter = func(dir string, fi os.FileInfo) bool {
|
|
|
|
// return true
|
|
|
|
// }
|
|
|
|
|
2015-01-04 18:23:00 +01:00
|
|
|
sc := restic.NewScanner(scanProgress)
|
|
|
|
|
2015-01-04 22:39:30 +01:00
|
|
|
newTree, err := sc.Scan(target)
|
2014-08-04 22:46:14 +02:00
|
|
|
if err != nil {
|
2014-11-16 21:29:11 +01:00
|
|
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
2014-09-23 22:39:12 +02:00
|
|
|
return err
|
2014-08-04 22:46:14 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 22:39:30 +01:00
|
|
|
if parentSnapshotID != nil {
|
|
|
|
fmt.Printf("load old snapshot\n")
|
|
|
|
ch := restic.NewContentHandler(s)
|
|
|
|
sn, err := ch.LoadSnapshot(parentSnapshotID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
oldTree, err := restic.LoadTreeRecursive(filepath.Dir(sn.Dir), ch, sn.Tree)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newTree.CopyFrom(oldTree)
|
|
|
|
}
|
|
|
|
|
2015-01-04 18:23:00 +01:00
|
|
|
archiveProgress := restic.NewProgress(time.Second)
|
2015-01-04 22:39:30 +01:00
|
|
|
targetStat := newTree.StatTodo()
|
2014-11-16 21:29:11 +01:00
|
|
|
|
|
|
|
if terminal.IsTerminal(int(os.Stdout.Fd())) {
|
2015-01-04 18:23:00 +01:00
|
|
|
var bps, eta uint64
|
2015-01-04 22:39:30 +01:00
|
|
|
itemsTodo := targetStat.Files + targetStat.Dirs
|
|
|
|
|
2015-01-04 18:23:00 +01:00
|
|
|
archiveProgress.F = func(s restic.Stat, d time.Duration, ticker bool) {
|
|
|
|
sec := uint64(d / time.Second)
|
2015-01-04 22:39:30 +01:00
|
|
|
if targetStat.Bytes > 0 && sec > 0 && ticker {
|
2015-01-04 18:23:00 +01:00
|
|
|
bps = s.Bytes / sec
|
2015-01-04 22:39:30 +01:00
|
|
|
if bps > 0 {
|
|
|
|
eta = (targetStat.Bytes - s.Bytes) / bps
|
|
|
|
}
|
2015-01-04 18:23:00 +01:00
|
|
|
}
|
2014-11-23 09:22:18 +01:00
|
|
|
|
2015-01-04 22:39:30 +01:00
|
|
|
itemsDone := s.Files + s.Dirs
|
|
|
|
fmt.Printf("\x1b[2K\r[%s] %3.2f%% %s/s %s / %s %d / %d items ETA %s",
|
2015-01-04 18:23:00 +01:00
|
|
|
format_duration(d),
|
|
|
|
float64(s.Bytes)/float64(targetStat.Bytes)*100,
|
|
|
|
format_bytes(bps),
|
|
|
|
format_bytes(s.Bytes), format_bytes(targetStat.Bytes),
|
2015-01-04 22:39:30 +01:00
|
|
|
itemsDone, itemsTodo,
|
2015-01-04 18:23:00 +01:00
|
|
|
format_seconds(eta))
|
|
|
|
}
|
2014-11-23 12:05:43 +01:00
|
|
|
|
2015-01-04 18:23:00 +01:00
|
|
|
archiveProgress.D = func(s restic.Stat, d time.Duration, ticker bool) {
|
|
|
|
sec := uint64(d / time.Second)
|
|
|
|
fmt.Printf("\nduration: %s, %.2fMiB/s\n",
|
|
|
|
format_duration(d),
|
|
|
|
float64(targetStat.Bytes)/float64(sec)/(1<<20))
|
|
|
|
}
|
|
|
|
}
|
2014-11-23 09:22:18 +01:00
|
|
|
|
2015-01-04 18:23:00 +01:00
|
|
|
arch, err := restic.NewArchiver(s, archiveProgress)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "err: %v\n", err)
|
|
|
|
}
|
2014-11-23 12:05:43 +01:00
|
|
|
|
2015-01-04 18:23:00 +01:00
|
|
|
arch.Error = func(dir string, fi os.FileInfo, err error) error {
|
|
|
|
// TODO: make ignoring errors configurable
|
2015-01-04 22:39:30 +01:00
|
|
|
fmt.Fprintf(os.Stderr, "\nerror for %s: %v\n", dir, err)
|
2015-01-04 18:23:00 +01:00
|
|
|
return nil
|
2014-11-16 21:29:11 +01:00
|
|
|
}
|
|
|
|
|
2015-01-04 22:39:30 +01:00
|
|
|
_, id, err := arch.Snapshot(target, newTree, parentSnapshotID)
|
2014-11-16 21:29:11 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2014-12-21 18:10:19 +01:00
|
|
|
plen, err := s.PrefixLength(backend.Snapshot)
|
2014-12-07 14:20:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-04 18:23:00 +01:00
|
|
|
fmt.Printf("snapshot %s saved\n", id[:plen])
|
2014-04-28 00:00:15 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|