restic/cmd/restic/cmd_restore.go

229 lines
6.9 KiB
Go
Raw Normal View History

2014-04-27 22:00:15 +00:00
package main
import (
"context"
"strings"
"time"
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"
"github.com/restic/restic/internal/restorer"
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.
EXIT STATUS
===========
Exit status is 0 if the command was successful, and non-zero if there was any error.
2016-09-17 10:36:05 +00:00
`,
DisableAutoGenTag: true,
2016-09-17 10:36:05 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
2022-10-02 21:24:37 +00:00
return runRestore(cmd.Context(), restoreOptions, globalOptions, args)
2016-09-17 10:36:05 +00:00
},
}
2016-09-17 10:36:05 +00:00
// RestoreOptions collects all options for the restore command.
type RestoreOptions struct {
Exclude []string
InsensitiveExclude []string
Include []string
InsensitiveInclude []string
Target string
snapshotFilterOptions
Sparse bool
Verify bool
}
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.StringArrayVar(&restoreOptions.InsensitiveExclude, "iexclude", nil, "same as `--exclude` but ignores the casing of filenames")
flags.StringArrayVarP(&restoreOptions.Include, "include", "i", nil, "include a `pattern`, exclude everything else (can be specified multiple times)")
flags.StringArrayVar(&restoreOptions.InsensitiveInclude, "iinclude", nil, "same as `--include` but ignores the casing of filenames")
2016-09-17 10:36:05 +00:00
flags.StringVarP(&restoreOptions.Target, "target", "t", "", "directory to extract data to")
initSingleSnapshotFilterOptions(flags, &restoreOptions.snapshotFilterOptions)
flags.BoolVar(&restoreOptions.Sparse, "sparse", false, "restore files as sparse")
flags.BoolVar(&restoreOptions.Verify, "verify", false, "verify restored files content")
2014-11-30 21:39:58 +00:00
}
func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions, args []string) error {
hasExcludes := len(opts.Exclude) > 0 || len(opts.InsensitiveExclude) > 0
hasIncludes := len(opts.Include) > 0 || len(opts.InsensitiveInclude) > 0
2017-06-04 09:16:55 +00:00
2022-05-20 14:06:25 +00:00
// Validate provided patterns
if len(opts.Exclude) > 0 {
if err := filter.ValidatePatterns(opts.Exclude); err != nil {
return errors.Fatalf("--exclude: %s", err)
2022-05-20 14:06:25 +00:00
}
}
if len(opts.InsensitiveExclude) > 0 {
if err := filter.ValidatePatterns(opts.InsensitiveExclude); err != nil {
return errors.Fatalf("--iexclude: %s", err)
2022-05-20 14:06:25 +00:00
}
}
if len(opts.Include) > 0 {
if err := filter.ValidatePatterns(opts.Include); err != nil {
return errors.Fatalf("--include: %s", err)
2022-05-20 14:06:25 +00:00
}
}
if len(opts.InsensitiveInclude) > 0 {
if err := filter.ValidatePatterns(opts.InsensitiveInclude); err != nil {
return errors.Fatalf("--iinclude: %s", err)
2022-05-20 14:06:25 +00:00
}
}
for i, str := range opts.InsensitiveExclude {
opts.InsensitiveExclude[i] = strings.ToLower(str)
}
for i, str := range opts.InsensitiveInclude {
opts.InsensitiveInclude[i] = strings.ToLower(str)
}
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)")
}
if hasExcludes && hasIncludes {
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)
repo, err := OpenRepository(ctx, 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 {
var lock *restic.Lock
lock, ctx, err = lockRepo(ctx, repo)
defer unlockRepo(lock)
if err != nil {
return err
}
2015-06-27 12:40:18 +00:00
}
sn, err := restic.FindFilteredSnapshot(ctx, repo.Backend(), repo, opts.Hosts, opts.Tags, opts.Paths, nil, snapshotIDString)
if err != nil {
Exitf(1, "failed to find snapshot: %v", err)
2014-04-27 22:00:15 +00:00
}
err = repo.LoadIndex(ctx)
if err != nil {
return err
}
res := restorer.NewRestorer(ctx, repo, sn, opts.Sparse)
2014-08-04 20:46:14 +00:00
totalErrors := 0
res.Error = func(location string, err error) error {
Warnf("ignoring error for %s: %s\n", location, err)
totalErrors++
return nil
2014-04-27 22:00:15 +00:00
}
excludePatterns := filter.ParsePatterns(opts.Exclude)
insensitiveExcludePatterns := filter.ParsePatterns(opts.InsensitiveExclude)
selectExcludeFilter := func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {
matched, err := filter.List(excludePatterns, item)
if err != nil {
2016-09-17 10:36:05 +00:00
Warnf("error for exclude pattern: %v", err)
}
matchedInsensitive, err := filter.List(insensitiveExcludePatterns, strings.ToLower(item))
if err != nil {
Warnf("error for iexclude 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 && !matchedInsensitive
childMayBeSelected = selectedForRestore && node.Type == "dir"
return selectedForRestore, childMayBeSelected
}
includePatterns := filter.ParsePatterns(opts.Include)
insensitiveIncludePatterns := filter.ParsePatterns(opts.InsensitiveInclude)
selectIncludeFilter := func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {
matched, childMayMatch, err := filter.ListWithChild(includePatterns, item)
if err != nil {
2016-09-17 10:36:05 +00:00
Warnf("error for include pattern: %v", err)
}
matchedInsensitive, childMayMatchInsensitive, err := filter.ListWithChild(insensitiveIncludePatterns, strings.ToLower(item))
if err != nil {
Warnf("error for iexclude pattern: %v", err)
}
selectedForRestore = matched || matchedInsensitive
childMayBeSelected = (childMayMatch || childMayMatchInsensitive) && node.Type == "dir"
return selectedForRestore, childMayBeSelected
}
if hasExcludes {
res.SelectFilter = selectExcludeFilter
} else if hasIncludes {
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
err = res.RestoreTo(ctx, opts.Target)
2021-01-05 08:13:15 +00:00
if err != nil {
return err
}
if totalErrors > 0 {
return errors.Fatalf("There were %d errors\n", totalErrors)
}
if opts.Verify {
Verbosef("verifying files in %s\n", opts.Target)
var count int
t0 := time.Now()
count, err = res.VerifyFiles(ctx, opts.Target)
2021-01-05 08:13:15 +00:00
if err != nil {
return err
}
if totalErrors > 0 {
return errors.Fatalf("There were %d errors\n", totalErrors)
}
Verbosef("finished verifying %d files in %s (took %s)\n", count, opts.Target,
time.Since(t0).Round(time.Millisecond))
}
2021-01-05 08:13:15 +00:00
return nil
2014-04-27 22:00:15 +00:00
}