2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-15 17:47:21 +00:00
restic/cmd/restic/cmd_restore.go

161 lines
4.6 KiB
Go
Raw Normal View History

2014-04-27 22:00:15 +00:00
package main
import (
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/filter"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
2016-09-17 10:36:05 +00:00
"github.com/spf13/cobra"
2014-04-27 22:00:15 +00:00
)
2016-09-17 10:36:05 +00:00
var cmdRestore = &cobra.Command{
Use: "restore [flags] snapshotID",
Short: "Extract the data from a snapshot",
2016-09-17 10:36:05 +00:00
Long: `
The "restore" command extracts the data from a snapshot from the repository to
a directory.
The special snapshot "latest" can be used to restore the latest snapshot in the
repository.
`,
DisableAutoGenTag: true,
2016-09-17 10:36:05 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
return runRestore(restoreOptions, globalOptions, args)
},
}
2016-09-17 10:36:05 +00:00
// RestoreOptions collects all options for the restore command.
type RestoreOptions struct {
Exclude []string
Include []string
Target string
Host string
Paths []string
Tags restic.TagLists
}
2014-12-07 15:30:52 +00:00
2016-09-17 10:36:05 +00:00
var restoreOptions RestoreOptions
2014-11-30 21:39:58 +00:00
func init() {
2016-09-17 10:36:05 +00:00
cmdRoot.AddCommand(cmdRestore)
2014-12-07 15:30:52 +00:00
2016-09-17 10:36:05 +00:00
flags := cmdRestore.Flags()
flags.StringArrayVarP(&restoreOptions.Exclude, "exclude", "e", nil, "exclude a `pattern` (can be specified multiple times)")
flags.StringArrayVarP(&restoreOptions.Include, "include", "i", nil, "include a `pattern`, exclude everything else (can be specified multiple times)")
2016-09-17 10:36:05 +00:00
flags.StringVarP(&restoreOptions.Target, "target", "t", "", "directory to extract data to")
2016-09-29 18:37:45 +00:00
flags.StringVarP(&restoreOptions.Host, "host", "H", "", `only consider snapshots for this host when the snapshot ID is "latest"`)
flags.Var(&restoreOptions.Tags, "tag", "only consider snapshots which include this `taglist` for snapshot ID \"latest\"")
flags.StringArrayVar(&restoreOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` for snapshot ID \"latest\"")
2014-11-30 21:39:58 +00:00
}
2016-09-17 10:36:05 +00:00
func runRestore(opts RestoreOptions, gopts GlobalOptions, args []string) error {
2017-06-04 09:16:55 +00:00
ctx := gopts.ctx
switch {
case len(args) == 0:
return errors.Fatal("no snapshot ID specified")
case len(args) > 1:
return errors.Fatalf("more than one snapshot ID specified: %v", args)
2014-12-07 15:30:52 +00:00
}
2016-09-17 10:36:05 +00:00
if opts.Target == "" {
2016-09-01 20:17:37 +00:00
return errors.Fatal("please specify a directory to restore to (--target)")
}
2016-09-17 10:36:05 +00:00
if len(opts.Exclude) > 0 && len(opts.Include) > 0 {
2016-09-01 20:17:37 +00:00
return errors.Fatal("exclude and include patterns are mutually exclusive")
}
snapshotIDString := args[0]
2016-09-27 20:35:08 +00:00
debug.Log("restore %v to %v", snapshotIDString, opts.Target)
2016-09-17 10:36:05 +00:00
repo, err := OpenRepository(gopts)
2014-12-07 15:30:52 +00:00
if err != nil {
return err
2014-04-27 22:00:15 +00:00
}
2016-09-17 10:36:05 +00:00
if !gopts.NoLock {
lock, err := lockRepo(repo)
defer unlockRepo(lock)
if err != nil {
return err
}
2015-06-27 12:40:18 +00:00
}
2017-06-04 09:16:55 +00:00
err = repo.LoadIndex(ctx)
if err != nil {
return err
}
2016-09-01 14:04:29 +00:00
var id restic.ID
if snapshotIDString == "latest" {
id, err = restic.FindLatestSnapshot(ctx, repo, opts.Paths, opts.Tags, opts.Host)
if err != nil {
2016-09-17 10:36:05 +00:00
Exitf(1, "latest snapshot for criteria not found: %v Paths:%v Host:%v", err, opts.Paths, opts.Host)
}
} else {
id, err = restic.FindSnapshot(repo, snapshotIDString)
if err != nil {
2016-09-17 10:36:05 +00:00
Exitf(1, "invalid id %q: %v", snapshotIDString, err)
}
2014-04-27 22:00:15 +00:00
}
res, err := restic.NewRestorer(repo, id)
2014-08-04 20:46:14 +00:00
if err != nil {
2016-09-17 10:36:05 +00:00
Exitf(2, "creating restorer failed: %v\n", err)
2014-08-04 20:46:14 +00:00
}
totalErrors := 0
2014-12-05 20:45:49 +00:00
res.Error = func(dir string, node *restic.Node, err error) error {
Warnf("ignoring error for %s: %s\n", dir, err)
totalErrors++
return nil
2014-04-27 22:00:15 +00:00
}
selectExcludeFilter := func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {
matched, _, err := filter.List(opts.Exclude, item)
if err != nil {
2016-09-17 10:36:05 +00:00
Warnf("error for exclude pattern: %v", err)
}
// An exclude filter is basically a 'wildcard but foo',
// so even if a childMayMatch, other children of a dir may not,
// therefore childMayMatch does not matter, but we should not go down
// unless the dir is selected for restore
selectedForRestore = !matched
childMayBeSelected = selectedForRestore && node.Type == "dir"
return selectedForRestore, childMayBeSelected
}
selectIncludeFilter := func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {
matched, childMayMatch, err := filter.List(opts.Include, item)
if err != nil {
2016-09-17 10:36:05 +00:00
Warnf("error for include pattern: %v", err)
}
selectedForRestore = matched
childMayBeSelected = childMayMatch && node.Type == "dir"
return selectedForRestore, childMayBeSelected
}
2016-09-17 10:36:05 +00:00
if len(opts.Exclude) > 0 {
res.SelectFilter = selectExcludeFilter
2016-09-17 10:36:05 +00:00
} else if len(opts.Include) > 0 {
res.SelectFilter = selectIncludeFilter
}
2016-09-17 10:36:05 +00:00
Verbosef("restoring %s to %s\n", res.Snapshot(), opts.Target)
2014-04-27 22:00:15 +00:00
2017-06-04 09:16:55 +00:00
err = res.RestoreTo(ctx, opts.Target)
if totalErrors > 0 {
Printf("There were %d errors\n", totalErrors)
}
return err
2014-04-27 22:00:15 +00:00
}