2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-21 20:35:12 +00:00

dump: minor cleanups

This commit is contained in:
Michael Eischer 2024-02-05 20:09:58 +01:00
parent 175c14b5c9
commit f5ffa40652
3 changed files with 13 additions and 12 deletions

View File

@ -1,7 +1,7 @@
Enhancement: Add --target flag to the dump command Enhancement: Add --target flag to the dump command
Restic `dump` always printed to the standard output. It now permits to select a Restic `dump` always printed to the standard output. It now permits to select a
`--target` file to print the output. `--target` file to write the output to.
https://github.com/restic/restic/issues/4678 https://github.com/restic/restic/issues/4678
https://github.com/restic/restic/pull/4682 https://github.com/restic/restic/pull/4682

View File

@ -57,7 +57,7 @@ func init() {
flags := cmdDump.Flags() flags := cmdDump.Flags()
initSingleSnapshotFilter(flags, &dumpOptions.SnapshotFilter) initSingleSnapshotFilter(flags, &dumpOptions.SnapshotFilter)
flags.StringVarP(&dumpOptions.Archive, "archive", "a", "tar", "set archive `format` as \"tar\" or \"zip\"") flags.StringVarP(&dumpOptions.Archive, "archive", "a", "tar", "set archive `format` as \"tar\" or \"zip\"")
flags.StringVarP(&dumpOptions.Target, "target", "t", "", "set the target path to dump the archive file") flags.StringVarP(&dumpOptions.Target, "target", "t", "", "write the output to target `path`")
} }
func splitPath(p string) []string { func splitPath(p string) []string {
@ -69,11 +69,11 @@ func splitPath(p string) []string {
return append(s, f) return append(s, f)
} }
func printFromTree(ctx context.Context, tree *restic.Tree, repo restic.BlobLoader, prefix string, pathComponents []string, d *dump.Dumper, checkStdoutArchiveFunc func() error) error { func printFromTree(ctx context.Context, tree *restic.Tree, repo restic.BlobLoader, prefix string, pathComponents []string, d *dump.Dumper, canWriteArchiveFunc func() error) error {
// If we print / we need to assume that there are multiple nodes at that // If we print / we need to assume that there are multiple nodes at that
// level in the tree. // level in the tree.
if pathComponents[0] == "" { if pathComponents[0] == "" {
if err := checkStdoutArchiveFunc(); err != nil { if err := canWriteArchiveFunc(); err != nil {
return err return err
} }
return d.DumpTree(ctx, tree, "/") return d.DumpTree(ctx, tree, "/")
@ -93,9 +93,9 @@ func printFromTree(ctx context.Context, tree *restic.Tree, repo restic.BlobLoade
if err != nil { if err != nil {
return errors.Wrapf(err, "cannot load subtree for %q", item) return errors.Wrapf(err, "cannot load subtree for %q", item)
} }
return printFromTree(ctx, subtree, repo, item, pathComponents[1:], d, checkStdoutArchiveFunc) return printFromTree(ctx, subtree, repo, item, pathComponents[1:], d, canWriteArchiveFunc)
case dump.IsDir(node): case dump.IsDir(node):
if err := checkStdoutArchiveFunc(); err != nil { if err := canWriteArchiveFunc(); err != nil {
return err return err
} }
subtree, err := restic.LoadTree(ctx, repo, *node.Subtree) subtree, err := restic.LoadTree(ctx, repo, *node.Subtree)
@ -170,8 +170,9 @@ func runDump(ctx context.Context, opts DumpOptions, gopts GlobalOptions, args []
return errors.Fatalf("loading tree for snapshot %q failed: %v", snapshotIDString, err) return errors.Fatalf("loading tree for snapshot %q failed: %v", snapshotIDString, err)
} }
var outputFileWriter = os.Stdout outputFileWriter := os.Stdout
checkStdoutArchiveFunc := checkStdoutArchive canWriteArchiveFunc := checkStdoutArchive
if opts.Target != "" { if opts.Target != "" {
file, err := os.OpenFile(opts.Target, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o666) file, err := os.OpenFile(opts.Target, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o666)
if err != nil { if err != nil {
@ -182,11 +183,11 @@ func runDump(ctx context.Context, opts DumpOptions, gopts GlobalOptions, args []
}() }()
outputFileWriter = file outputFileWriter = file
checkStdoutArchiveFunc = func() error { return nil } canWriteArchiveFunc = func() error { return nil }
} }
d := dump.New(opts.Archive, repo, outputFileWriter) d := dump.New(opts.Archive, repo, outputFileWriter)
err = printFromTree(ctx, tree, repo, "/", splittedPath, d, checkStdoutArchiveFunc) err = printFromTree(ctx, tree, repo, "/", splittedPath, d, canWriteArchiveFunc)
if err != nil { if err != nil {
return errors.Fatalf("cannot dump file: %v", err) return errors.Fatalf("cannot dump file: %v", err)
} }

View File

@ -176,8 +176,8 @@ To include the folder content at the root of the archive, you can use the ``<sna
$ restic -r /srv/restic-repo dump latest:/home/other/work / > restore.tar $ restic -r /srv/restic-repo dump latest:/home/other/work / > restore.tar
It is also possible to ``dump`` the contents of a selected snapshot and folder It is also possible to ``dump`` the contents of a selected snapshot and folder
structure to a file using the ``--target`` flag. The ``dump`` will fail if the structure to a file using the ``--target`` flag. The ``dump`` command will fail
file exists. if the already file exists.
.. code-block:: console .. code-block:: console
$ restic -r /srv/restic-repo dump latest / --target /home/linux.user/output.tar -a tar $ restic -r /srv/restic-repo dump latest / --target /home/linux.user/output.tar -a tar