2014-04-28 00:00:15 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-10-31 23:08:13 +01:00
|
|
|
"context"
|
2020-02-20 21:54:31 +01:00
|
|
|
"time"
|
2020-02-26 22:17:59 +01:00
|
|
|
|
2017-07-23 14:21:03 +02:00
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-24 17:42:25 +02:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2018-05-10 22:56:10 -04:00
|
|
|
"github.com/restic/restic/internal/restorer"
|
2022-10-28 17:44:34 +02:00
|
|
|
"github.com/restic/restic/internal/ui"
|
|
|
|
restoreui "github.com/restic/restic/internal/ui/restore"
|
|
|
|
"github.com/restic/restic/internal/ui/termstatus"
|
2016-09-17 12:36:05 +02:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2014-04-28 00:00:15 +02:00
|
|
|
)
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
var cmdRestore = &cobra.Command{
|
|
|
|
Use: "restore [flags] snapshotID",
|
2017-09-11 09:32:44 -07:00
|
|
|
Short: "Extract the data from a snapshot",
|
2016-09-17 12:36:05 +02:00
|
|
|
Long: `
|
|
|
|
The "restore" command extracts the data from a snapshot from the repository to
|
|
|
|
a directory.
|
|
|
|
|
2023-08-05 12:35:45 +02:00
|
|
|
The special snapshotID "latest" can be used to restore the latest snapshot in the
|
2016-09-17 12:36:05 +02:00
|
|
|
repository.
|
2019-11-04 22:03:38 -08:00
|
|
|
|
2024-07-03 20:39:59 +02:00
|
|
|
To only restore a specific subfolder, you can use the "snapshotID:subfolder"
|
2023-08-05 12:35:08 +02:00
|
|
|
syntax, where "subfolder" is a path within the snapshot.
|
|
|
|
|
2019-11-04 22:03:38 -08:00
|
|
|
EXIT STATUS
|
|
|
|
===========
|
|
|
|
|
|
|
|
Exit status is 0 if the command was successful, and non-zero if there was any error.
|
2016-09-17 12:36:05 +02:00
|
|
|
`,
|
2017-08-06 21:02:16 +02:00
|
|
|
DisableAutoGenTag: true,
|
2016-09-17 12:36:05 +02:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2024-01-20 18:12:36 +01:00
|
|
|
term, cancel := setupTermstatus()
|
2024-01-20 18:10:11 +01:00
|
|
|
defer cancel()
|
|
|
|
return runRestore(cmd.Context(), restoreOptions, globalOptions, term, args)
|
2016-09-17 12:36:05 +02:00
|
|
|
},
|
|
|
|
}
|
2015-07-20 00:38:44 +02:00
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
// RestoreOptions collects all options for the restore command.
|
|
|
|
type RestoreOptions struct {
|
2024-06-01 17:49:15 +05:30
|
|
|
excludePatternOptions
|
|
|
|
includePatternOptions
|
|
|
|
Target string
|
2023-02-17 16:13:46 +01:00
|
|
|
restic.SnapshotFilter
|
2024-05-31 11:43:42 +02:00
|
|
|
Sparse bool
|
|
|
|
Verify bool
|
|
|
|
Overwrite restorer.OverwriteBehavior
|
2015-06-21 13:02:56 +02:00
|
|
|
}
|
2014-12-07 16:30:52 +01:00
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
var restoreOptions RestoreOptions
|
|
|
|
|
2014-11-30 22:39:58 +01:00
|
|
|
func init() {
|
2016-09-17 12:36:05 +02:00
|
|
|
cmdRoot.AddCommand(cmdRestore)
|
2014-12-07 16:30:52 +01:00
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
flags := cmdRestore.Flags()
|
|
|
|
flags.StringVarP(&restoreOptions.Target, "target", "t", "", "directory to extract data to")
|
2024-06-01 17:49:15 +05:30
|
|
|
|
|
|
|
initExcludePatternOptions(flags, &restoreOptions.excludePatternOptions)
|
|
|
|
initIncludePatternOptions(flags, &restoreOptions.includePatternOptions)
|
2016-09-17 12:36:05 +02:00
|
|
|
|
2023-02-17 16:13:46 +01:00
|
|
|
initSingleSnapshotFilter(flags, &restoreOptions.SnapshotFilter)
|
2022-09-04 11:23:31 +02:00
|
|
|
flags.BoolVar(&restoreOptions.Sparse, "sparse", false, "restore files as sparse")
|
2018-04-13 10:02:09 -04:00
|
|
|
flags.BoolVar(&restoreOptions.Verify, "verify", false, "verify restored files content")
|
2024-05-31 17:34:48 +02:00
|
|
|
flags.Var(&restoreOptions.Overwrite, "overwrite", "overwrite behavior, one of (always|if-changed|if-newer|never) (default: always)")
|
2014-11-30 22:39:58 +01:00
|
|
|
}
|
|
|
|
|
2022-10-28 17:44:34 +02:00
|
|
|
func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
|
|
|
|
term *termstatus.Terminal, args []string) error {
|
|
|
|
|
2024-06-01 19:46:12 +05:30
|
|
|
excludePatternFns, err := opts.excludePatternOptions.CollectPatterns()
|
2024-06-01 18:04:14 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-01 19:46:12 +05:30
|
|
|
includePatternFns, err := opts.includePatternOptions.CollectPatterns()
|
2024-06-01 18:04:14 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-09 19:07:23 +05:30
|
|
|
hasExcludes := len(excludePatternFns) > 0
|
|
|
|
hasIncludes := len(includePatternFns) > 0
|
|
|
|
|
2018-01-06 22:44:18 +01:00
|
|
|
switch {
|
|
|
|
case len(args) == 0:
|
2017-02-10 19:39:49 +01:00
|
|
|
return errors.Fatal("no snapshot ID specified")
|
2018-01-06 22:44:18 +01:00
|
|
|
case len(args) > 1:
|
|
|
|
return errors.Fatalf("more than one snapshot ID specified: %v", args)
|
2014-12-07 16:30:52 +01:00
|
|
|
}
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
if opts.Target == "" {
|
2016-09-01 22:17:37 +02:00
|
|
|
return errors.Fatal("please specify a directory to restore to (--target)")
|
2015-07-20 00:38:44 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 22:39:55 +02:00
|
|
|
if hasExcludes && hasIncludes {
|
2016-09-01 22:17:37 +02:00
|
|
|
return errors.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]
|
|
|
|
|
2016-09-27 22:35:08 +02:00
|
|
|
debug.Log("restore %v to %v", snapshotIDString, opts.Target)
|
2015-07-20 00:38:44 +02:00
|
|
|
|
2024-02-24 15:19:02 +01:00
|
|
|
ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock)
|
2014-12-07 16:30:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-04-28 00:00:15 +02:00
|
|
|
}
|
2024-02-24 15:19:02 +01:00
|
|
|
defer unlock()
|
2015-06-27 14:40:18 +02:00
|
|
|
|
2023-07-16 16:50:54 +02:00
|
|
|
sn, subfolder, err := (&restic.SnapshotFilter{
|
2023-02-17 16:13:46 +01:00
|
|
|
Hosts: opts.Hosts,
|
|
|
|
Paths: opts.Paths,
|
|
|
|
Tags: opts.Tags,
|
2023-10-01 13:05:56 +02:00
|
|
|
}).FindLatest(ctx, repo, repo, snapshotIDString)
|
2022-10-03 14:16:33 +02:00
|
|
|
if err != nil {
|
2022-12-28 21:42:38 +01:00
|
|
|
return errors.Fatalf("failed to find snapshot: %v", err)
|
2014-04-28 00:00:15 +02:00
|
|
|
}
|
|
|
|
|
2023-10-01 19:38:09 +02:00
|
|
|
bar := newIndexTerminalProgress(gopts.Quiet, gopts.JSON, term)
|
2023-07-15 22:48:30 -04:00
|
|
|
err = repo.LoadIndex(ctx, bar)
|
2021-11-06 00:32:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-16 16:50:54 +02:00
|
|
|
sn.Tree, err = restic.FindTreeDirectory(ctx, repo, sn.Tree, subfolder)
|
2023-05-18 23:15:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-13 23:23:39 +02:00
|
|
|
msg := ui.NewMessage(term, gopts.verbosity)
|
2023-05-01 12:01:03 +02:00
|
|
|
var printer restoreui.ProgressPrinter
|
|
|
|
if gopts.JSON {
|
|
|
|
printer = restoreui.NewJSONProgress(term)
|
|
|
|
} else {
|
|
|
|
printer = restoreui.NewTextProgress(term)
|
2022-10-28 17:44:34 +02:00
|
|
|
}
|
|
|
|
|
2023-05-01 12:01:03 +02:00
|
|
|
progress := restoreui.NewProgress(printer, calculateProgressInterval(!gopts.Quiet, gopts.JSON))
|
2024-05-31 11:42:25 +02:00
|
|
|
res := restorer.NewRestorer(repo, sn, restorer.Options{
|
|
|
|
Sparse: opts.Sparse,
|
|
|
|
Progress: progress,
|
2024-05-31 11:43:42 +02:00
|
|
|
Overwrite: opts.Overwrite,
|
2024-05-31 11:42:25 +02:00
|
|
|
})
|
2014-08-04 22:46:14 +02:00
|
|
|
|
2017-03-12 16:39:37 +01:00
|
|
|
totalErrors := 0
|
2018-09-14 20:55:30 -04:00
|
|
|
res.Error = func(location string, err error) error {
|
2023-05-13 23:23:39 +02:00
|
|
|
msg.E("ignoring error for %s: %s\n", location, err)
|
2017-03-12 16:39:37 +01:00
|
|
|
totalErrors++
|
2015-07-25 12:58:55 +02:00
|
|
|
return nil
|
2014-04-28 00:00:15 +02:00
|
|
|
}
|
2024-02-22 17:52:26 -07:00
|
|
|
res.Warn = func(message string) {
|
|
|
|
msg.E("Warning: %s\n", message)
|
|
|
|
}
|
2014-04-28 00:00:15 +02:00
|
|
|
|
2024-02-10 22:58:10 +01:00
|
|
|
selectExcludeFilter := func(item string, _ string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {
|
2024-06-01 20:37:52 +05:30
|
|
|
matched := false
|
2024-06-01 19:46:12 +05:30
|
|
|
for _, rejectFn := range excludePatternFns {
|
2024-06-01 20:37:52 +05:30
|
|
|
matched = matched || rejectFn(item)
|
2024-06-09 19:07:23 +05:30
|
|
|
|
|
|
|
// implementing a short-circuit here to improve the performance
|
|
|
|
// to prevent additional pattern matching once the first pattern
|
|
|
|
// matches.
|
|
|
|
if matched {
|
|
|
|
break
|
|
|
|
}
|
2024-05-19 23:30:14 +05:30
|
|
|
}
|
2024-06-01 20:37:52 +05:30
|
|
|
// 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"
|
|
|
|
|
2024-06-01 17:49:15 +05:30
|
|
|
return selectedForRestore, childMayBeSelected
|
2024-05-19 23:30:14 +05:30
|
|
|
}
|
|
|
|
|
2024-02-10 22:58:10 +01:00
|
|
|
selectIncludeFilter := func(item string, _ string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {
|
2024-06-01 20:37:52 +05:30
|
|
|
selectedForRestore = false
|
|
|
|
childMayBeSelected = false
|
2024-06-01 19:46:12 +05:30
|
|
|
for _, includeFn := range includePatternFns {
|
2024-06-01 20:37:52 +05:30
|
|
|
matched, childMayMatch := includeFn(item)
|
|
|
|
selectedForRestore = selectedForRestore || matched
|
|
|
|
childMayBeSelected = childMayBeSelected || childMayMatch
|
2018-10-16 22:39:55 +02:00
|
|
|
|
2024-06-10 01:55:39 +05:30
|
|
|
if selectedForRestore && childMayBeSelected {
|
2024-06-09 19:07:23 +05:30
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2024-06-10 01:55:39 +05:30
|
|
|
childMayBeSelected = childMayBeSelected && node.Type == "dir"
|
2017-07-07 11:54:10 +02:00
|
|
|
|
|
|
|
return selectedForRestore, childMayBeSelected
|
2015-07-20 19:20:20 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 22:39:55 +02:00
|
|
|
if hasExcludes {
|
2015-07-20 19:20:20 +02:00
|
|
|
res.SelectFilter = selectExcludeFilter
|
2018-10-16 22:39:55 +02:00
|
|
|
} else if hasIncludes {
|
2015-07-20 19:20:20 +02:00
|
|
|
res.SelectFilter = selectIncludeFilter
|
2015-01-01 16:29:41 +01:00
|
|
|
}
|
|
|
|
|
2023-05-01 12:01:03 +02:00
|
|
|
if !gopts.JSON {
|
2023-05-13 23:23:39 +02:00
|
|
|
msg.P("restoring %s to %s\n", res.Snapshot(), opts.Target)
|
2023-05-01 12:01:03 +02:00
|
|
|
}
|
2014-04-28 00:00:15 +02:00
|
|
|
|
2018-05-11 00:45:14 -04:00
|
|
|
err = res.RestoreTo(ctx, opts.Target)
|
2021-01-05 09:13:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-01 12:01:03 +02:00
|
|
|
progress.Finish()
|
2022-10-28 17:44:34 +02:00
|
|
|
|
2021-01-05 09:13:15 +01:00
|
|
|
if totalErrors > 0 {
|
|
|
|
return errors.Fatalf("There were %d errors\n", totalErrors)
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Verify {
|
2023-05-01 12:01:03 +02:00
|
|
|
if !gopts.JSON {
|
2023-05-13 23:23:39 +02:00
|
|
|
msg.P("verifying files in %s\n", opts.Target)
|
2023-05-01 12:01:03 +02:00
|
|
|
}
|
2018-04-13 10:02:09 -04:00
|
|
|
var count int
|
2020-02-20 21:54:31 +01:00
|
|
|
t0 := time.Now()
|
2018-04-13 10:02:09 -04:00
|
|
|
count, err = res.VerifyFiles(ctx, opts.Target)
|
2021-01-05 09:13:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if totalErrors > 0 {
|
|
|
|
return errors.Fatalf("There were %d errors\n", totalErrors)
|
|
|
|
}
|
2023-05-01 12:01:03 +02:00
|
|
|
|
|
|
|
if !gopts.JSON {
|
2023-05-13 23:23:39 +02:00
|
|
|
msg.P("finished verifying %d files in %s (took %s)\n", count, opts.Target,
|
2023-05-01 12:01:03 +02:00
|
|
|
time.Since(t0).Round(time.Millisecond))
|
|
|
|
}
|
2018-04-13 10:02:09 -04:00
|
|
|
}
|
2021-01-05 09:13:15 +01:00
|
|
|
|
|
|
|
return nil
|
2014-04-28 00:00:15 +02:00
|
|
|
}
|