restic/cmd/restic/cmd_dump.go

186 lines
4.9 KiB
Go
Raw Normal View History

2017-10-09 20:09:41 +00:00
package main
import (
"context"
"fmt"
"os"
"path"
2017-10-09 20:09:41 +00:00
"path/filepath"
"github.com/restic/restic/internal/debug"
"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: `
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
The special snapshot "latest" can be used to use 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.
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 {
snapshotFilterOptions
2020-11-10 01:24:09 +00:00
Archive 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()
initMultiSnapshotFilterOptions(flags, &dumpOptions.snapshotFilterOptions, true)
2020-11-10 01:24:09 +00:00
flags.StringVarP(&dumpOptions.Archive, "archive", "a", "tar", "set archive `format` as \"tar\" or \"zip\"")
2017-10-09 20:09:41 +00:00
}
func splitPath(p string) []string {
d, f := path.Split(p)
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)
}
func printFromTree(ctx context.Context, tree *restic.Tree, repo restic.Repository, prefix string, pathComponents []string, d *dump.Dumper) error {
// If we print / we need to assume that there are multiple nodes at that
// level in the tree.
if pathComponents[0] == "" {
2020-11-09 22:22:27 +00:00
if err := checkStdoutArchive(); err != nil {
return err
}
return d.DumpTree(ctx, tree, "/")
}
2017-10-09 20:09:41 +00:00
item := filepath.Join(prefix, pathComponents[0])
l := len(pathComponents)
2017-10-09 20:09:41 +00:00
for _, node := range tree.Nodes {
// 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 {
case l == 1 && dump.IsFile(node):
return d.WriteNode(ctx, node)
case l > 1 && dump.IsDir(node):
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)
}
return printFromTree(ctx, subtree, repo, item, pathComponents[1:], d)
case dump.IsDir(node):
2020-11-09 22:22:27 +00:00
if err := checkStdoutArchive(); err != nil {
return err
}
subtree, err := restic.LoadTree(ctx, repo, *node.Subtree)
if err != nil {
return err
}
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)
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)
}
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 {
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
splittedPath := splitPath(path.Clean(pathToPrint))
2017-10-09 20:09:41 +00:00
repo, err := OpenRepository(ctx, gopts)
2017-10-09 20:09:41 +00:00
if err != nil {
return err
}
if !gopts.NoLock {
var lock *restic.Lock
lock, ctx, err = lockRepo(ctx, repo)
2017-10-09 20:09:41 +00:00
defer unlockRepo(lock)
if err != nil {
return err
}
}
var id restic.ID
if snapshotIDString == "latest" {
id, err = restic.FindLatestSnapshot(ctx, repo.Backend(), repo, opts.Paths, opts.Tags, opts.Hosts, nil)
2017-10-09 20:09:41 +00:00
if err != nil {
Exitf(1, "latest snapshot for criteria not found: %v Paths:%v Hosts:%v", err, opts.Paths, opts.Hosts)
2017-10-09 20:09:41 +00:00
}
} else {
id, err = restic.FindSnapshot(ctx, repo.Backend(), snapshotIDString)
2017-10-09 20:09:41 +00:00
if err != nil {
Exitf(1, "invalid id %q: %v", snapshotIDString, err)
}
}
sn, err := restic.LoadSnapshot(ctx, repo, id)
2017-10-09 20:09:41 +00:00
if err != nil {
Exitf(2, "loading snapshot %q failed: %v", snapshotIDString, err)
}
err = repo.LoadIndex(ctx)
if err != nil {
return err
}
tree, err := restic.LoadTree(ctx, repo, *sn.Tree)
2017-10-09 20:09:41 +00:00
if err != nil {
Exitf(2, "loading tree for snapshot %q failed: %v", snapshotIDString, err)
}
d := dump.New(opts.Archive, repo, os.Stdout)
err = printFromTree(ctx, tree, repo, "/", splittedPath, d)
2017-10-09 20:09:41 +00:00
if err != nil {
2017-10-14 09:34:04 +00:00
Exitf(2, "cannot dump file: %v", err)
2017-10-09 20:09:41 +00:00
}
return nil
}
2020-11-09 22:22:27 +00:00
func checkStdoutArchive() error {
if stdoutIsTerminal() {
return fmt.Errorf("stdout is the terminal, please redirect output")
}
return nil
}