2014-04-27 22:00:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-09-23 20:39:12 +00:00
|
|
|
"os"
|
2014-11-16 20:29:11 +00:00
|
|
|
"strings"
|
2014-11-16 21:50:20 +00:00
|
|
|
"time"
|
2014-04-27 22:00:15 +00:00
|
|
|
|
2014-07-28 18:20:32 +00:00
|
|
|
"github.com/fd0/khepri"
|
2014-09-23 20:39:12 +00:00
|
|
|
"github.com/fd0/khepri/backend"
|
2014-11-16 20:29:11 +00:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2014-04-27 22:00:15 +00:00
|
|
|
)
|
|
|
|
|
2014-11-16 20:29:11 +00:00
|
|
|
func format_bytes(c uint64) string {
|
|
|
|
b := float64(c)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case c > 1<<40:
|
|
|
|
return fmt.Sprintf("%.3f TiB", b/(1<<40))
|
|
|
|
case c > 1<<30:
|
|
|
|
return fmt.Sprintf("%.3f GiB", b/(1<<30))
|
|
|
|
case c > 1<<20:
|
|
|
|
return fmt.Sprintf("%.3f MiB", b/(1<<20))
|
|
|
|
case c > 1<<10:
|
|
|
|
return fmt.Sprintf("%.3f KiB", b/(1<<10))
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%d B", c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func print_tree2(indent int, t *khepri.Tree) {
|
|
|
|
for _, node := range *t {
|
|
|
|
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-09-23 20:39:12 +00:00
|
|
|
func commandBackup(be backend.Server, key *khepri.Key, args []string) error {
|
2014-04-27 22:00:15 +00:00
|
|
|
if len(args) != 1 {
|
2014-09-23 20:39:12 +00:00
|
|
|
return errors.New("usage: backup [dir|file]")
|
2014-04-27 22:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
target := args[0]
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
arch, err := khepri.NewArchiver(be, key)
|
2014-08-11 20:47:24 +00:00
|
|
|
if err != nil {
|
2014-09-23 20:39:12 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "err: %v\n", err)
|
2014-08-11 20:47:24 +00:00
|
|
|
}
|
2014-09-23 20:39:12 +00:00
|
|
|
arch.Error = func(dir string, fi os.FileInfo, err error) error {
|
2014-11-23 11:14:56 +00:00
|
|
|
// TODO: make ignoring errors configurable
|
|
|
|
fmt.Fprintf(os.Stderr, "\nerror for %s: %v\n%v\n", dir, err, fi)
|
|
|
|
return nil
|
2014-04-27 22:00:15 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 20:29:11 +00:00
|
|
|
fmt.Printf("scanning %s\n", target)
|
|
|
|
|
|
|
|
if terminal.IsTerminal(int(os.Stdout.Fd())) {
|
2014-11-23 08:22:18 +00:00
|
|
|
ch := make(chan khepri.Stats, 5)
|
|
|
|
arch.ScannerStats = ch
|
|
|
|
|
|
|
|
go func(ch <-chan khepri.Stats) {
|
|
|
|
for stats := range ch {
|
|
|
|
fmt.Printf("\r%6d directories, %6d files, %14s", stats.Directories, stats.Files, format_bytes(stats.Bytes))
|
|
|
|
}
|
|
|
|
}(ch)
|
2014-11-16 20:29:11 +00:00
|
|
|
}
|
|
|
|
|
2014-11-23 08:22:18 +00:00
|
|
|
fmt.Printf("done\n")
|
|
|
|
|
2014-11-16 20:29:11 +00:00
|
|
|
// TODO: add filter
|
|
|
|
// arch.Filter = func(dir string, fi os.FileInfo) bool {
|
|
|
|
// return true
|
|
|
|
// }
|
|
|
|
|
|
|
|
t, err := arch.LoadTree(target)
|
2014-08-04 20:46:14 +00:00
|
|
|
if err != nil {
|
2014-11-16 20:29:11 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
2014-09-23 20:39:12 +00:00
|
|
|
return err
|
2014-08-04 20:46:14 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 20:29:11 +00:00
|
|
|
fmt.Printf("\r%6d directories, %6d files, %14s\n", arch.Stats.Directories, arch.Stats.Files, format_bytes(arch.Stats.Bytes))
|
|
|
|
|
|
|
|
stats := khepri.Stats{}
|
|
|
|
if terminal.IsTerminal(int(os.Stdout.Fd())) {
|
2014-11-23 08:22:18 +00:00
|
|
|
ch := make(chan khepri.Stats, 5)
|
|
|
|
arch.SaveStats = ch
|
|
|
|
|
|
|
|
go func(ch <-chan khepri.Stats) {
|
|
|
|
for s := range ch {
|
|
|
|
stats.Files += s.Files
|
|
|
|
stats.Directories += s.Directories
|
|
|
|
stats.Other += s.Other
|
|
|
|
stats.Bytes += s.Bytes
|
|
|
|
|
|
|
|
fmt.Printf("\r%3.2f%% %d/%d directories, %d/%d files, %s/%s",
|
|
|
|
float64(stats.Bytes)/float64(arch.Stats.Bytes)*100,
|
|
|
|
stats.Directories, arch.Stats.Directories,
|
|
|
|
stats.Files, arch.Stats.Files,
|
|
|
|
format_bytes(stats.Bytes), format_bytes(arch.Stats.Bytes))
|
|
|
|
}
|
|
|
|
}(ch)
|
2014-11-16 20:29:11 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 21:50:20 +00:00
|
|
|
start := time.Now()
|
2014-11-16 20:29:11 +00:00
|
|
|
sn, id, err := arch.Snapshot(target, t)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2014-11-23 08:22:18 +00:00
|
|
|
if terminal.IsTerminal(int(os.Stdout.Fd())) {
|
|
|
|
// close channels so that the goroutines terminate
|
|
|
|
close(arch.SaveStats)
|
|
|
|
close(arch.ScannerStats)
|
|
|
|
}
|
|
|
|
|
2014-11-16 20:29:11 +00:00
|
|
|
fmt.Printf("\nsnapshot %s saved: %v\n", id, sn)
|
2014-11-16 21:50:20 +00:00
|
|
|
duration := time.Now().Sub(start)
|
|
|
|
fmt.Printf("duration: %s, %.2fMiB/s\n", duration, float64(arch.Stats.Bytes)/float64(duration/time.Second)/(1<<20))
|
2014-04-27 22:00:15 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|