2014-04-27 22:00:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-09-23 20:39:12 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-01-01 15:29:41 +00:00
|
|
|
"path/filepath"
|
2014-04-27 22:00:15 +00:00
|
|
|
|
2014-12-05 20:45:49 +00:00
|
|
|
"github.com/restic/restic"
|
2014-04-27 22:00:15 +00:00
|
|
|
)
|
|
|
|
|
2014-12-07 15:30:52 +00:00
|
|
|
type CmdRestore struct{}
|
|
|
|
|
2014-11-30 21:39:58 +00:00
|
|
|
func init() {
|
2014-12-07 15:30:52 +00:00
|
|
|
_, err := parser.AddCommand("restore",
|
|
|
|
"restore a snapshot",
|
|
|
|
"The restore command restores a snapshot to a directory",
|
|
|
|
&CmdRestore{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd CmdRestore) Usage() string {
|
2015-01-01 15:29:41 +00:00
|
|
|
return "snapshot-ID TARGETDIR [PATTERN]"
|
2014-11-30 21:39:58 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 15:30:52 +00:00
|
|
|
func (cmd CmdRestore) Execute(args []string) error {
|
2015-01-01 15:29:41 +00:00
|
|
|
if len(args) < 2 || len(args) > 3 {
|
2014-12-07 15:30:52 +00:00
|
|
|
return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage())
|
|
|
|
}
|
|
|
|
|
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-04-26 15:44:38 +00:00
|
|
|
err = s.LoadIndex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-17 18:48:59 +00:00
|
|
|
id, err := restic.FindSnapshot(s, args[0])
|
2014-04-27 22:00:15 +00:00
|
|
|
if err != nil {
|
2014-08-03 13:16:56 +00:00
|
|
|
errx(1, "invalid id %q: %v", args[0], err)
|
2014-04-27 22:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
target := args[1]
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
// create restorer
|
2014-12-21 17:10:19 +00:00
|
|
|
res, err := restic.NewRestorer(s, id)
|
2014-08-04 20:46:14 +00:00
|
|
|
if err != nil {
|
2014-09-23 20:39:12 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "creating restorer failed: %v\n", err)
|
|
|
|
os.Exit(2)
|
2014-08-04 20:46:14 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 20:45:49 +00:00
|
|
|
res.Error = func(dir string, node *restic.Node, err error) error {
|
2014-09-23 20:39:12 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "error for %s: %+v\n", dir, err)
|
|
|
|
|
|
|
|
// if node.Type == "dir" {
|
|
|
|
// if e, ok := err.(*os.PathError); ok {
|
|
|
|
// if errn, ok := e.Err.(syscall.Errno); ok {
|
|
|
|
// if errn == syscall.EEXIST {
|
|
|
|
// fmt.Printf("ignoring already existing directory %s\n", dir)
|
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
return err
|
2014-04-27 22:00:15 +00:00
|
|
|
}
|
|
|
|
|
2015-01-01 15:29:41 +00:00
|
|
|
// TODO: a filter against the full path sucks as filepath.Match doesn't match
|
|
|
|
// directory separators on '*'. still, it's better than nothing.
|
|
|
|
if len(args) > 2 {
|
|
|
|
res.Filter = func(item string, dstpath string, node *restic.Node) bool {
|
|
|
|
matched, err := filepath.Match(item, args[2])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return matched
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
fmt.Printf("restoring %s to %s\n", res.Snapshot(), target)
|
|
|
|
|
|
|
|
err = res.RestoreTo(target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-27 22:00:15 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|