2014-04-28 00:00:15 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-02-14 15:29:28 +01:00
|
|
|
"restic"
|
|
|
|
"restic/debug"
|
|
|
|
"restic/filter"
|
2014-04-28 00:00:15 +02:00
|
|
|
)
|
|
|
|
|
2015-06-21 13:02:56 +02:00
|
|
|
type CmdRestore struct {
|
2015-07-20 00:38:44 +02:00
|
|
|
Exclude []string `short:"e" long:"exclude" description:"Exclude a pattern (can be specified multiple times)"`
|
2015-07-20 19:20:20 +02:00
|
|
|
Include []string `short:"i" long:"include" description:"Include a pattern, exclude everything else (can be specified multiple times)"`
|
2015-07-20 00:38:44 +02:00
|
|
|
Target string `short:"t" long:"target" description:"Directory to restore to"`
|
2016-05-10 22:12:33 +02:00
|
|
|
Host string `short:"h" long:"host" description:"Source Filter (for id=latest)"`
|
2016-05-10 21:23:18 +02:00
|
|
|
Paths []string `short:"p" long:"path" description:"Path Filter (absolute path;for id=latest) (can be specified multiple times)"`
|
2015-07-20 00:38:44 +02:00
|
|
|
|
2015-06-21 13:02:56 +02:00
|
|
|
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",
|
2015-06-21 13:02:56 +02:00
|
|
|
&CmdRestore{global: &globalOpts})
|
2014-12-07 16:30:52 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd CmdRestore) Usage() string {
|
2015-07-20 00:38:44 +02:00
|
|
|
return "snapshot-ID"
|
2014-11-30 22:39:58 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 16:30:52 +01:00
|
|
|
func (cmd CmdRestore) Execute(args []string) error {
|
2015-07-20 00:38:44 +02:00
|
|
|
if len(args) != 1 {
|
2016-08-28 22:19:48 +02:00
|
|
|
return restic.Fatalf("wrong number of arguments, Usage: %s", cmd.Usage())
|
2014-12-07 16:30:52 +01:00
|
|
|
}
|
|
|
|
|
2015-07-20 00:38:44 +02:00
|
|
|
if cmd.Target == "" {
|
2016-08-28 22:19:48 +02:00
|
|
|
return restic.Fatal("please specify a directory to restore to (--target)")
|
2015-07-20 00:38:44 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:20:20 +02:00
|
|
|
if len(cmd.Exclude) > 0 && len(cmd.Include) > 0 {
|
2016-08-28 22:19:48 +02:00
|
|
|
return restic.Fatal("exclude and include patterns are mutually exclusive")
|
2015-07-20 19:20:20 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 00:38:44 +02:00
|
|
|
snapshotIDString := args[0]
|
|
|
|
|
|
|
|
debug.Log("restore", "restore %v to %v", snapshotIDString, cmd.Target)
|
|
|
|
|
2015-06-27 14:36:46 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-11-13 12:33:59 +01:00
|
|
|
if !cmd.global.NoLock {
|
2015-11-10 22:13:53 +01:00
|
|
|
lock, err := lockRepo(repo)
|
|
|
|
defer unlockRepo(lock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-27 14:40:18 +02:00
|
|
|
}
|
|
|
|
|
2015-06-27 14:36:46 +02:00
|
|
|
err = repo.LoadIndex()
|
2015-04-26 17:44:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-01 16:04:29 +02:00
|
|
|
var id restic.ID
|
2016-05-10 21:23:18 +02:00
|
|
|
|
|
|
|
if snapshotIDString == "latest" {
|
2016-05-10 21:51:56 +02:00
|
|
|
id, err = restic.FindLatestSnapshot(repo, cmd.Paths, cmd.Host)
|
2016-05-10 21:23:18 +02:00
|
|
|
if err != nil {
|
2016-05-10 21:51:56 +02:00
|
|
|
cmd.global.Exitf(1, "latest snapshot for criteria not found: %v Paths:%v Host:%v", err, cmd.Paths, cmd.Host)
|
2016-05-10 21:23:18 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
id, err = restic.FindSnapshot(repo, snapshotIDString)
|
|
|
|
if err != nil {
|
|
|
|
cmd.global.Exitf(1, "invalid id %q: %v", snapshotIDString, err)
|
|
|
|
}
|
2014-04-28 00:00:15 +02:00
|
|
|
}
|
|
|
|
|
2015-06-27 14:36:46 +02:00
|
|
|
res, err := restic.NewRestorer(repo, id)
|
2014-08-04 22:46:14 +02:00
|
|
|
if err != nil {
|
2015-06-21 15:20:54 +02:00
|
|
|
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 {
|
2015-06-21 15:20:54 +02:00
|
|
|
cmd.global.Warnf("error for %s: %+v\n", dir, err)
|
2015-07-25 12:58:55 +02:00
|
|
|
return nil
|
2014-04-28 00:00:15 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:20:20 +02:00
|
|
|
selectExcludeFilter := func(item string, dstpath string, node *restic.Node) bool {
|
2015-07-20 00:38:44 +02:00
|
|
|
matched, err := filter.List(cmd.Exclude, item)
|
|
|
|
if err != nil {
|
|
|
|
cmd.global.Warnf("error for exclude pattern: %v", err)
|
2015-01-01 16:29:41 +01:00
|
|
|
}
|
2015-07-20 00:38:44 +02:00
|
|
|
|
|
|
|
return !matched
|
|
|
|
}
|
|
|
|
|
2015-07-20 19:20:20 +02: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-20 00:38:44 +02:00
|
|
|
if len(cmd.Exclude) > 0 {
|
2015-07-20 19:20:20 +02:00
|
|
|
res.SelectFilter = selectExcludeFilter
|
|
|
|
} else if len(cmd.Include) > 0 {
|
|
|
|
res.SelectFilter = selectIncludeFilter
|
2015-01-01 16:29:41 +01:00
|
|
|
}
|
|
|
|
|
2015-07-20 00:38:44 +02:00
|
|
|
cmd.global.Verbosef("restoring %s to %s\n", res.Snapshot(), cmd.Target)
|
2014-09-23 22:39:12 +02:00
|
|
|
|
2015-07-20 00:38:44 +02:00
|
|
|
err = res.RestoreTo(cmd.Target)
|
2014-09-23 22:39:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-28 00:00:15 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|