restic/cmd/restic/cmd_backup.go

252 lines
5.6 KiB
Go
Raw Normal View History

2014-04-27 22:00:15 +00:00
package main
import (
"fmt"
2014-09-23 20:39:12 +00:00
"os"
2015-03-02 13:48:47 +00:00
"path/filepath"
"strings"
2014-11-16 21:50:20 +00:00
"time"
2014-04-27 22:00:15 +00:00
2014-12-05 20:45:49 +00:00
"github.com/restic/restic"
"github.com/restic/restic/backend"
"golang.org/x/crypto/ssh/terminal"
2014-04-27 22:00:15 +00:00
)
2015-03-02 13:48:47 +00:00
type CmdBackup struct {
Parent string `short:"p" long:"parent" description:"use this parent snapshot (default: not set)"`
}
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("backup",
"save file/directory",
"The backup command creates a snapshot of a file or directory",
&CmdBackup{})
if err != nil {
panic(err)
}
2014-11-30 21:39:58 +00:00
}
func format_bytes(c uint64) string {
b := float64(c)
switch {
case c > 1<<40:
2014-11-23 13:53:46 +00:00
return fmt.Sprintf("%.3f TiB", b/(1<<40))
case c > 1<<30:
2014-11-23 13:53:46 +00:00
return fmt.Sprintf("%.3f GiB", b/(1<<30))
case c > 1<<20:
2014-11-23 13:53:46 +00:00
return fmt.Sprintf("%.3f MiB", b/(1<<20))
case c > 1<<10:
2014-11-23 13:53:46 +00:00
return fmt.Sprintf("%.3f KiB", b/(1<<10))
default:
2014-11-23 11:05:43 +00:00
return fmt.Sprintf("%dB", c)
}
}
func format_seconds(sec uint64) string {
2014-11-23 11:05:43 +00: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)
}
func format_duration(d time.Duration) string {
sec := uint64(d / time.Second)
return format_seconds(sec)
}
2014-12-05 20:45:49 +00:00
func print_tree2(indent int, t *restic.Tree) {
for _, node := range t.Nodes {
if node.Tree() != nil {
fmt.Printf("%s%s/\n", strings.Repeat(" ", indent), node.Name)
print_tree2(indent+1, node.Tree())
} else {
fmt.Printf("%s%s\n", strings.Repeat(" ", indent), node.Name)
}
}
}
2014-12-07 15:30:52 +00:00
func (cmd CmdBackup) Usage() string {
return "DIR/FILE [snapshot-ID]"
}
func newCacheRefreshProgress() *restic.Progress {
p := restic.NewProgress(time.Second)
p.OnStart = func() {
fmt.Printf("refreshing cache\n")
}
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
return p
}
p.OnUpdate = func(s restic.Stat, d time.Duration, ticker bool) {
fmt.Printf("\x1b[2K[%s] %d trees loaded\r", format_duration(d), s.Trees)
}
p.OnDone = func(s restic.Stat, d time.Duration, ticker bool) {
fmt.Printf("\x1b[2Krefreshed cache in %s\n", format_duration(d))
}
return p
}
2015-02-21 13:23:49 +00:00
func newScanProgress() *restic.Progress {
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
return nil
}
2015-02-21 14:32:48 +00:00
p := restic.NewProgress(time.Second)
p.OnUpdate = func(s restic.Stat, d time.Duration, ticker bool) {
fmt.Printf("\x1b[2K[%s] %d directories, %d files, %s\r", format_duration(d), s.Dirs, s.Files, format_bytes(s.Bytes))
2015-02-21 14:32:48 +00:00
}
p.OnDone = func(s restic.Stat, d time.Duration, ticker bool) {
fmt.Printf("\x1b[2Kscanned %d directories, %d files in %s\n", s.Dirs, s.Files, format_duration(d))
2015-02-21 14:32:48 +00:00
}
return p
}
2015-02-21 13:23:49 +00:00
func newArchiveProgress(todo restic.Stat) *restic.Progress {
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
return nil
}
archiveProgress := restic.NewProgress(time.Second)
var bps, eta uint64
itemsTodo := todo.Files + todo.Dirs
archiveProgress.OnUpdate = func(s restic.Stat, d time.Duration, ticker bool) {
sec := uint64(d / time.Second)
if todo.Bytes > 0 && sec > 0 && ticker {
bps = s.Bytes / sec
2015-03-16 19:20:53 +00:00
if s.Bytes >= todo.Bytes {
eta = 0
} else if bps > 0 {
2015-02-21 13:23:49 +00:00
eta = (todo.Bytes - s.Bytes) / bps
}
}
itemsDone := s.Files + s.Dirs
2015-03-16 19:20:53 +00:00
percent := float64(s.Bytes) / float64(todo.Bytes) * 100
if percent > 100 {
percent = 100
}
status1 := fmt.Sprintf("[%s] %3.2f%% %s/s %s / %s %d / %d items ",
2015-02-21 13:23:49 +00:00
format_duration(d),
2015-03-16 19:20:53 +00:00
percent,
2015-02-21 13:23:49 +00:00
format_bytes(bps),
format_bytes(s.Bytes), format_bytes(todo.Bytes),
itemsDone, itemsTodo)
status2 := fmt.Sprintf("ETA %s ", format_seconds(eta))
w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if err == nil {
if len(status1)+len(status2) > w {
max := w - len(status2) - 4
status1 = status1[:max] + "... "
}
}
fmt.Printf("\x1b[2K%s%s\r", status1, status2)
2015-02-21 13:23:49 +00:00
}
archiveProgress.OnDone = 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(todo.Bytes)/float64(sec)/(1<<20))
}
return archiveProgress
2015-02-16 22:44:26 +00:00
}
2014-12-07 15:30:52 +00:00
func (cmd CmdBackup) Execute(args []string) error {
2015-03-02 13:48:47 +00:00
if len(args) == 0 {
2014-12-07 15:30:52 +00:00
return fmt.Errorf("wrong number of parameters, Usage: %s", cmd.Usage())
}
2015-03-02 13:48:47 +00:00
target := make([]string, 0, len(args))
for _, d := range args {
if a, err := filepath.Abs(d); err == nil {
d = a
}
target = append(target, d)
}
2014-12-21 17:10:19 +00:00
s, err := OpenRepo()
2014-12-07 15:30:52 +00:00
if err != nil {
return err
2014-04-27 22:00:15 +00:00
}
2015-03-28 10:50:23 +00:00
var (
parentSnapshot string
parentSnapshotID backend.ID
)
2014-11-30 21:34:21 +00:00
2015-03-02 13:48:47 +00:00
if cmd.Parent != "" {
2015-03-28 10:50:23 +00:00
parentSnapshot, err = s.FindSnapshot(cmd.Parent)
2014-11-30 21:34:21 +00:00
if err != nil {
2015-03-02 13:48:47 +00:00
return fmt.Errorf("invalid id %q: %v", cmd.Parent, err)
2014-11-30 21:34:21 +00:00
}
2015-03-28 10:50:23 +00:00
parentSnapshotID, err = backend.ParseID(parentSnapshot)
if err != nil {
return fmt.Errorf("invalid parent snapshot id %v", parentSnapshot)
}
2014-11-30 21:34:21 +00:00
fmt.Printf("found parent snapshot %v\n", parentSnapshotID)
}
2014-04-27 22:00:15 +00:00
2015-03-02 13:48:47 +00:00
fmt.Printf("scan %v\n", target)
2015-02-21 14:32:48 +00:00
stat, err := restic.Scan(target, newScanProgress())
// TODO: add filter
// arch.Filter = func(dir string, fi os.FileInfo) bool {
// return true
// }
2015-02-21 13:23:49 +00:00
arch, err := restic.NewArchiver(s)
if err != nil {
fmt.Fprintf(os.Stderr, "err: %v\n", err)
}
2014-11-23 11:05:43 +00:00
arch.Error = func(dir string, fi os.FileInfo, err error) error {
// TODO: make ignoring errors configurable
fmt.Fprintf(os.Stderr, "\x1b[2K\rerror for %s: %v\n", dir, err)
return nil
}
err = arch.Cache().RefreshSnapshots(s, newCacheRefreshProgress())
if err != nil {
return err
}
2015-03-07 11:05:33 +00:00
fmt.Printf("loading blobs\n")
2015-03-09 21:26:39 +00:00
err = arch.Preload()
2015-03-07 11:05:33 +00:00
if err != nil {
return err
}
2015-02-17 21:39:44 +00:00
2015-02-21 14:32:48 +00:00
_, id, err := arch.Snapshot(newArchiveProgress(stat), target, parentSnapshotID)
if err != nil {
2015-02-03 21:05:46 +00:00
return err
}
2014-12-21 17:10:19 +00:00
plen, err := s.PrefixLength(backend.Snapshot)
2014-12-07 13:20:17 +00:00
if err != nil {
return err
}
2015-03-28 15:15:48 +00:00
fmt.Printf("snapshot %s saved\n", id[:plen/2])
2014-04-27 22:00:15 +00:00
return nil
}