restic/cmd/restic/cmd_restore.go

106 lines
2.2 KiB
Go
Raw Normal View History

2014-04-28 00:00:15 +02:00
package main
import (
2014-09-23 22:39:12 +02:00
"fmt"
"path/filepath"
2014-04-28 00:00:15 +02:00
2014-12-05 21:45:49 +01:00
"github.com/restic/restic"
"github.com/restic/restic/debug"
2014-04-28 00:00:15 +02:00
)
type CmdRestore struct {
global *GlobalOptions
}
2014-12-07 16:30:52 +01:00
2014-11-30 22:39:58 +01:00
func init() {
2014-12-07 16:30:52 +01:00
_, err := parser.AddCommand("restore",
"restore a snapshot",
"The restore command restores a snapshot to a directory",
&CmdRestore{global: &globalOpts})
2014-12-07 16:30:52 +01:00
if err != nil {
panic(err)
}
}
func (cmd CmdRestore) Usage() string {
return "snapshot-ID TARGETDIR [PATTERN]"
2014-11-30 22:39:58 +01:00
}
2014-12-07 16:30:52 +01:00
func (cmd CmdRestore) Execute(args []string) error {
if len(args) < 2 || len(args) > 3 {
2014-12-07 16:30:52 +01:00
return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage())
}
repo, err := cmd.global.OpenRepository()
2014-12-07 16:30:52 +01:00
if err != nil {
return err
2014-04-28 00:00:15 +02:00
}
lock, err := lockRepo(repo)
defer unlockRepo(lock)
2015-06-27 14:40:18 +02:00
if err != nil {
return err
}
err = repo.LoadIndex()
if err != nil {
return err
}
id, err := restic.FindSnapshot(repo, args[0])
2014-04-28 00:00:15 +02:00
if err != nil {
cmd.global.Exitf(1, "invalid id %q: %v", args[0], err)
2014-04-28 00:00:15 +02:00
}
target := args[1]
2014-09-23 22:39:12 +02:00
// create restorer
res, err := restic.NewRestorer(repo, id)
2014-08-04 22:46:14 +02:00
if err != nil {
cmd.global.Exitf(2, "creating restorer failed: %v\n", err)
2014-08-04 22:46:14 +02:00
}
2014-12-05 21:45:49 +01:00
res.Error = func(dir string, node *restic.Node, err error) error {
cmd.global.Warnf("error for %s: %+v\n", dir, err)
2014-09-23 22:39:12 +02:00
// 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-28 00:00:15 +02: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 {
pattern := args[2]
cmd.global.Verbosef("filter pattern %q\n", pattern)
res.SelectForRestore = func(item string, dstpath string, node *restic.Node) bool {
matched, err := filepath.Match(pattern, node.Name)
if err != nil {
panic(err)
}
if !matched {
debug.Log("restic.restore", "item %v doesn't match pattern %q", item, pattern)
}
return matched
}
}
2015-06-21 13:25:26 +02:00
cmd.global.Verbosef("restoring %s to %s\n", res.Snapshot(), target)
2014-09-23 22:39:12 +02:00
err = res.RestoreTo(target)
if err != nil {
return err
}
2014-04-28 00:00:15 +02:00
return nil
}