2017-10-09 20:09:41 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-11-10 11:43:11 +00:00
|
|
|
"path"
|
2017-10-09 20:09:41 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/debug"
|
2019-05-14 18:07:29 +00:00
|
|
|
"github.com/restic/restic/internal/dump"
|
2017-10-09 20:09:41 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2017-10-14 09:34:04 +00:00
|
|
|
var cmdDump = &cobra.Command{
|
|
|
|
Use: "dump [flags] snapshotID file",
|
2017-10-09 20:09:41 +00:00
|
|
|
Short: "Print a backed-up file to stdout",
|
|
|
|
Long: `
|
2020-08-31 19:40:47 +00:00
|
|
|
The "dump" command extracts files from a snapshot from the repository. If a
|
|
|
|
single file is selected, it prints its contents to stdout. Folders are output
|
2020-11-10 01:24:09 +00:00
|
|
|
as a tar (default) or zip file containing the contents of the specified folder.
|
|
|
|
Pass "/" as file name to dump the whole snapshot as an archive file.
|
2017-10-09 20:09:41 +00:00
|
|
|
|
2023-08-05 10:35:45 +00:00
|
|
|
The special snapshotID "latest" can be used to use the latest snapshot in the
|
2017-10-09 20:09:41 +00:00
|
|
|
repository.
|
2019-11-05 06:03:38 +00:00
|
|
|
|
2023-08-05 10:35:08 +00:00
|
|
|
To include the folder content at the root of the archive, you can use the
|
2024-07-03 18:39:59 +00:00
|
|
|
"snapshotID:subfolder" syntax, where "subfolder" is a path within the
|
2023-08-05 10:35:08 +00:00
|
|
|
snapshot.
|
|
|
|
|
2019-11-05 06:03:38 +00:00
|
|
|
EXIT STATUS
|
|
|
|
===========
|
|
|
|
|
|
|
|
Exit status is 0 if the command was successful, and non-zero if there was any error.
|
2017-10-09 20:09:41 +00:00
|
|
|
`,
|
|
|
|
DisableAutoGenTag: true,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-10-02 21:24:37 +00:00
|
|
|
return runDump(cmd.Context(), dumpOptions, globalOptions, args)
|
2017-10-09 20:09:41 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-10-14 09:34:04 +00:00
|
|
|
// DumpOptions collects all options for the dump command.
|
|
|
|
type DumpOptions struct {
|
2023-02-17 15:13:46 +00:00
|
|
|
restic.SnapshotFilter
|
2020-11-10 01:24:09 +00:00
|
|
|
Archive string
|
2024-02-04 08:45:14 +00:00
|
|
|
Target string
|
2017-10-09 20:09:41 +00:00
|
|
|
}
|
|
|
|
|
2017-10-14 09:34:04 +00:00
|
|
|
var dumpOptions DumpOptions
|
2017-10-09 20:09:41 +00:00
|
|
|
|
|
|
|
func init() {
|
2017-10-14 09:34:04 +00:00
|
|
|
cmdRoot.AddCommand(cmdDump)
|
2017-10-09 20:09:41 +00:00
|
|
|
|
2017-10-14 09:34:04 +00:00
|
|
|
flags := cmdDump.Flags()
|
2023-02-17 15:13:46 +00:00
|
|
|
initSingleSnapshotFilter(flags, &dumpOptions.SnapshotFilter)
|
2020-11-10 01:24:09 +00:00
|
|
|
flags.StringVarP(&dumpOptions.Archive, "archive", "a", "tar", "set archive `format` as \"tar\" or \"zip\"")
|
2024-02-05 19:09:58 +00:00
|
|
|
flags.StringVarP(&dumpOptions.Target, "target", "t", "", "write the output to target `path`")
|
2017-10-09 20:09:41 +00:00
|
|
|
}
|
|
|
|
|
2018-11-10 11:43:11 +00:00
|
|
|
func splitPath(p string) []string {
|
|
|
|
d, f := path.Split(p)
|
2019-04-25 12:08:36 +00:00
|
|
|
if d == "" || d == "/" {
|
2017-10-09 20:09:41 +00:00
|
|
|
return []string{f}
|
|
|
|
}
|
2020-08-29 17:36:13 +00:00
|
|
|
s := splitPath(path.Join("/", d))
|
2017-10-09 20:09:41 +00:00
|
|
|
return append(s, f)
|
|
|
|
}
|
|
|
|
|
2024-02-05 19:09:58 +00:00
|
|
|
func printFromTree(ctx context.Context, tree *restic.Tree, repo restic.BlobLoader, prefix string, pathComponents []string, d *dump.Dumper, canWriteArchiveFunc func() error) error {
|
2019-04-25 12:08:36 +00:00
|
|
|
// If we print / we need to assume that there are multiple nodes at that
|
|
|
|
// level in the tree.
|
2020-08-30 16:14:54 +00:00
|
|
|
if pathComponents[0] == "" {
|
2024-02-05 19:09:58 +00:00
|
|
|
if err := canWriteArchiveFunc(); err != nil {
|
2019-05-14 18:07:29 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-09-24 21:51:51 +00:00
|
|
|
return d.DumpTree(ctx, tree, "/")
|
2019-04-25 12:08:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-09 20:09:41 +00:00
|
|
|
item := filepath.Join(prefix, pathComponents[0])
|
2021-09-24 21:51:51 +00:00
|
|
|
l := len(pathComponents)
|
2017-10-09 20:09:41 +00:00
|
|
|
for _, node := range tree.Nodes {
|
2019-04-25 12:08:36 +00:00
|
|
|
// If dumping something in the highest level it will just take the
|
|
|
|
// first item it finds and dump that according to the switch case below.
|
|
|
|
if node.Name == pathComponents[0] {
|
2017-10-09 20:09:41 +00:00
|
|
|
switch {
|
2019-05-14 18:07:29 +00:00
|
|
|
case l == 1 && dump.IsFile(node):
|
2021-09-24 21:51:51 +00:00
|
|
|
return d.WriteNode(ctx, node)
|
2019-05-14 18:07:29 +00:00
|
|
|
case l > 1 && dump.IsDir(node):
|
2022-06-12 12:38:19 +00:00
|
|
|
subtree, err := restic.LoadTree(ctx, repo, *node.Subtree)
|
2017-10-09 20:09:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "cannot load subtree for %q", item)
|
|
|
|
}
|
2024-02-05 19:09:58 +00:00
|
|
|
return printFromTree(ctx, subtree, repo, item, pathComponents[1:], d, canWriteArchiveFunc)
|
2019-05-14 18:07:29 +00:00
|
|
|
case dump.IsDir(node):
|
2024-02-05 19:09:58 +00:00
|
|
|
if err := canWriteArchiveFunc(); err != nil {
|
2019-05-14 18:07:29 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-06-12 12:38:19 +00:00
|
|
|
subtree, err := restic.LoadTree(ctx, repo, *node.Subtree)
|
2019-04-25 12:08:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-24 21:51:51 +00:00
|
|
|
return d.DumpTree(ctx, subtree, item)
|
2017-10-09 20:09:41 +00:00
|
|
|
case l > 1:
|
2019-05-02 09:51:35 +00:00
|
|
|
return fmt.Errorf("%q should be a dir, but is a %q", item, node.Type)
|
2019-05-14 18:07:29 +00:00
|
|
|
case !dump.IsFile(node):
|
2017-10-09 20:09:41 +00:00
|
|
|
return fmt.Errorf("%q should be a file, but is a %q", item, node.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("path %q not found in snapshot", item)
|
|
|
|
}
|
|
|
|
|
2021-10-31 22:08:13 +00:00
|
|
|
func runDump(ctx context.Context, opts DumpOptions, gopts GlobalOptions, args []string) error {
|
2017-10-09 20:09:41 +00:00
|
|
|
if len(args) != 2 {
|
|
|
|
return errors.Fatal("no file and no snapshot ID specified")
|
|
|
|
}
|
|
|
|
|
2020-11-10 01:24:09 +00:00
|
|
|
switch opts.Archive {
|
2021-09-24 21:51:51 +00:00
|
|
|
case "tar", "zip":
|
2020-11-10 01:24:09 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown archive format %q", opts.Archive)
|
|
|
|
}
|
|
|
|
|
2017-10-14 09:34:04 +00:00
|
|
|
snapshotIDString := args[0]
|
|
|
|
pathToPrint := args[1]
|
2017-10-09 20:09:41 +00:00
|
|
|
|
2017-10-14 09:34:04 +00:00
|
|
|
debug.Log("dump file %q from %q", pathToPrint, snapshotIDString)
|
2017-10-09 20:09:41 +00:00
|
|
|
|
2018-12-20 15:39:48 +00:00
|
|
|
splittedPath := splitPath(path.Clean(pathToPrint))
|
2017-10-09 20:09:41 +00:00
|
|
|
|
2024-02-24 14:19:02 +00:00
|
|
|
ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock)
|
2017-10-09 20:09:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-24 14:19:02 +00:00
|
|
|
defer unlock()
|
2017-10-09 20:09:41 +00:00
|
|
|
|
2023-07-16 14:50:54 +00:00
|
|
|
sn, subfolder, err := (&restic.SnapshotFilter{
|
2023-02-17 15:13:46 +00:00
|
|
|
Hosts: opts.Hosts,
|
|
|
|
Paths: opts.Paths,
|
|
|
|
Tags: opts.Tags,
|
2023-10-01 11:05:56 +00:00
|
|
|
}).FindLatest(ctx, repo, repo, snapshotIDString)
|
2022-10-03 12:16:33 +00:00
|
|
|
if err != nil {
|
2022-12-28 20:42:38 +00:00
|
|
|
return errors.Fatalf("failed to find snapshot: %v", err)
|
2017-10-09 20:09:41 +00:00
|
|
|
}
|
|
|
|
|
2023-10-01 17:38:09 +00:00
|
|
|
bar := newIndexProgress(gopts.Quiet, gopts.JSON)
|
2023-07-16 02:48:30 +00:00
|
|
|
err = repo.LoadIndex(ctx, bar)
|
2021-11-05 23:32:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-16 14:50:54 +00:00
|
|
|
sn.Tree, err = restic.FindTreeDirectory(ctx, repo, sn.Tree, subfolder)
|
2023-05-18 21:15:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
tree, err := restic.LoadTree(ctx, repo, *sn.Tree)
|
2017-10-09 20:09:41 +00:00
|
|
|
if err != nil {
|
2022-12-28 20:42:38 +00:00
|
|
|
return errors.Fatalf("loading tree for snapshot %q failed: %v", snapshotIDString, err)
|
2017-10-09 20:09:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 19:09:58 +00:00
|
|
|
outputFileWriter := os.Stdout
|
|
|
|
canWriteArchiveFunc := checkStdoutArchive
|
|
|
|
|
2024-02-04 08:45:14 +00:00
|
|
|
if opts.Target != "" {
|
2024-02-08 06:11:57 +00:00
|
|
|
file, err := os.Create(opts.Target)
|
2024-02-04 08:45:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot dump to file: %w", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = file.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
outputFileWriter = file
|
2024-02-05 19:09:58 +00:00
|
|
|
canWriteArchiveFunc = func() error { return nil }
|
2024-02-04 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d := dump.New(opts.Archive, repo, outputFileWriter)
|
2024-02-05 19:09:58 +00:00
|
|
|
err = printFromTree(ctx, tree, repo, "/", splittedPath, d, canWriteArchiveFunc)
|
2017-10-09 20:09:41 +00:00
|
|
|
if err != nil {
|
2022-12-28 20:42:38 +00:00
|
|
|
return errors.Fatalf("cannot dump file: %v", err)
|
2017-10-09 20:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-12-20 15:39:48 +00:00
|
|
|
|
2020-11-09 22:22:27 +00:00
|
|
|
func checkStdoutArchive() error {
|
2019-04-25 12:08:36 +00:00
|
|
|
if stdoutIsTerminal() {
|
|
|
|
return fmt.Errorf("stdout is the terminal, please redirect output")
|
|
|
|
}
|
2018-12-20 15:39:48 +00:00
|
|
|
return nil
|
|
|
|
}
|