2014-04-27 22:00:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-07-19 22:38:44 +00:00
|
|
|
"errors"
|
2014-09-23 20:39:12 +00:00
|
|
|
"fmt"
|
2014-04-27 22:00:15 +00:00
|
|
|
|
2014-12-05 20:45:49 +00:00
|
|
|
"github.com/restic/restic"
|
2015-07-06 21:59:28 +00:00
|
|
|
"github.com/restic/restic/debug"
|
2015-07-19 22:38:44 +00:00
|
|
|
"github.com/restic/restic/filter"
|
2014-04-27 22:00:15 +00:00
|
|
|
)
|
|
|
|
|
2015-06-21 11:02:56 +00:00
|
|
|
type CmdRestore struct {
|
2015-07-19 22:38:44 +00:00
|
|
|
Exclude []string `short:"e" long:"exclude" description:"Exclude a pattern (can be specified multiple times)"`
|
2015-07-20 17:20:20 +00:00
|
|
|
Include []string `short:"i" long:"include" description:"Include a pattern, exclude everything else (can be specified multiple times)"`
|
2015-07-19 22:38:44 +00:00
|
|
|
Target string `short:"t" long:"target" description:"Directory to restore to"`
|
|
|
|
|
2015-06-21 11:02:56 +00:00
|
|
|
global *GlobalOptions
|
|
|
|
}
|
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("restore",
|
|
|
|
"restore a snapshot",
|
|
|
|
"The restore command restores a snapshot to a directory",
|
2015-06-21 11:02:56 +00:00
|
|
|
&CmdRestore{global: &globalOpts})
|
2014-12-07 15:30:52 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd CmdRestore) Usage() string {
|
2015-07-19 22:38:44 +00:00
|
|
|
return "snapshot-ID"
|
2014-11-30 21:39:58 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 15:30:52 +00:00
|
|
|
func (cmd CmdRestore) Execute(args []string) error {
|
2015-07-19 22:38:44 +00:00
|
|
|
if len(args) != 1 {
|
2014-12-07 15:30:52 +00:00
|
|
|
return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage())
|
|
|
|
}
|
|
|
|
|
2015-07-19 22:38:44 +00:00
|
|
|
if cmd.Target == "" {
|
|
|
|
return errors.New("please specify a directory to restore to (--target)")
|
|
|
|
}
|
|
|
|
|
2015-07-20 17:20:20 +00:00
|
|
|
if len(cmd.Exclude) > 0 && len(cmd.Include) > 0 {
|
|
|
|
return errors.New("exclude and include patterns are mutually exclusive")
|
|
|
|
}
|
|
|
|
|
2015-07-19 22:38:44 +00:00
|
|
|
snapshotIDString := args[0]
|
|
|
|
|
|
|
|
debug.Log("restore", "restore %v to %v", snapshotIDString, cmd.Target)
|
|
|
|
|
2015-06-27 12:36:46 +00:00
|
|
|
repo, err := cmd.global.OpenRepository()
|
2014-12-07 15:30:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-04-27 22:00:15 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 13:05:20 +00:00
|
|
|
lock, err := lockRepo(repo)
|
|
|
|
defer unlockRepo(lock)
|
2015-06-27 12:40:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-27 12:36:46 +00:00
|
|
|
err = repo.LoadIndex()
|
2015-04-26 15:44:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-19 22:38:44 +00:00
|
|
|
id, err := restic.FindSnapshot(repo, snapshotIDString)
|
2014-04-27 22:00:15 +00:00
|
|
|
if err != nil {
|
2015-07-19 22:38:44 +00:00
|
|
|
cmd.global.Exitf(1, "invalid id %q: %v", snapshotIDString, err)
|
2014-04-27 22:00:15 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 12:36:46 +00:00
|
|
|
res, err := restic.NewRestorer(repo, id)
|
2014-08-04 20:46:14 +00:00
|
|
|
if err != nil {
|
2015-06-21 13:20:54 +00:00
|
|
|
cmd.global.Exitf(2, "creating restorer failed: %v\n", err)
|
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 {
|
2015-06-21 13:20:54 +00:00
|
|
|
cmd.global.Warnf("error for %s: %+v\n", dir, err)
|
2014-09-23 20:39:12 +00:00
|
|
|
return err
|
2014-04-27 22:00:15 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 17:20:20 +00:00
|
|
|
selectExcludeFilter := func(item string, dstpath string, node *restic.Node) bool {
|
2015-07-19 22:38:44 +00:00
|
|
|
matched, err := filter.List(cmd.Exclude, item)
|
|
|
|
if err != nil {
|
|
|
|
cmd.global.Warnf("error for exclude pattern: %v", err)
|
2015-01-01 15:29:41 +00:00
|
|
|
}
|
2015-07-19 22:38:44 +00:00
|
|
|
|
|
|
|
return !matched
|
|
|
|
}
|
|
|
|
|
2015-07-20 17:20:20 +00:00
|
|
|
selectIncludeFilter := func(item string, dstpath string, node *restic.Node) bool {
|
|
|
|
matched, err := filter.List(cmd.Include, item)
|
|
|
|
if err != nil {
|
|
|
|
cmd.global.Warnf("error for include pattern: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return matched
|
|
|
|
}
|
|
|
|
|
2015-07-19 22:38:44 +00:00
|
|
|
if len(cmd.Exclude) > 0 {
|
2015-07-20 17:20:20 +00:00
|
|
|
res.SelectFilter = selectExcludeFilter
|
|
|
|
} else if len(cmd.Include) > 0 {
|
|
|
|
res.SelectFilter = selectIncludeFilter
|
2015-01-01 15:29:41 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 22:38:44 +00:00
|
|
|
cmd.global.Verbosef("restoring %s to %s\n", res.Snapshot(), cmd.Target)
|
2014-09-23 20:39:12 +00:00
|
|
|
|
2015-07-19 22:38:44 +00:00
|
|
|
err = res.RestoreTo(cmd.Target)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-27 22:00:15 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|