mirror of
https://github.com/restic/restic.git
synced 2025-08-23 09:47:42 +00:00
recover: refactor to use termstatus
This commit is contained in:
@@ -6,7 +6,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/restic/restic/internal/errors"
|
"github.com/restic/restic/internal/errors"
|
||||||
|
"github.com/restic/restic/internal/repository"
|
||||||
"github.com/restic/restic/internal/restic"
|
"github.com/restic/restic/internal/restic"
|
||||||
|
"github.com/restic/restic/internal/ui/progress"
|
||||||
|
"github.com/restic/restic/internal/ui/termstatus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
@@ -32,13 +35,15 @@ Exit status is 12 if the password is incorrect.
|
|||||||
GroupID: cmdGroupDefault,
|
GroupID: cmdGroupDefault,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
return runRecover(cmd.Context(), globalOptions)
|
term, cancel := setupTermstatus()
|
||||||
|
defer cancel()
|
||||||
|
return runRecover(cmd.Context(), globalOptions, term)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func runRecover(ctx context.Context, gopts GlobalOptions) error {
|
func runRecover(ctx context.Context, gopts GlobalOptions, term *termstatus.Terminal) error {
|
||||||
hostname, err := os.Hostname()
|
hostname, err := os.Hostname()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -50,13 +55,15 @@ func runRecover(ctx context.Context, gopts GlobalOptions) error {
|
|||||||
}
|
}
|
||||||
defer unlock()
|
defer unlock()
|
||||||
|
|
||||||
|
printer := newTerminalProgressPrinter(gopts.verbosity, term)
|
||||||
|
|
||||||
snapshotLister, err := restic.MemorizeList(ctx, repo, restic.SnapshotFile)
|
snapshotLister, err := restic.MemorizeList(ctx, repo, restic.SnapshotFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
Verbosef("load index files\n")
|
printer.P("load index files\n")
|
||||||
bar := newIndexProgress(gopts.Quiet, gopts.JSON)
|
bar := newIndexTerminalProgress(gopts.Quiet, gopts.JSON, term)
|
||||||
if err = repo.LoadIndex(ctx, bar); err != nil {
|
if err = repo.LoadIndex(ctx, bar); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -74,15 +81,15 @@ func runRecover(ctx context.Context, gopts GlobalOptions) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
Verbosef("load %d trees\n", len(trees))
|
printer.P("load %d trees\n", len(trees))
|
||||||
bar = newProgressMax(!gopts.Quiet, uint64(len(trees)), "trees loaded")
|
bar = newTerminalProgressMax(!gopts.Quiet, uint64(len(trees)), "trees loaded", term)
|
||||||
for id := range trees {
|
for id := range trees {
|
||||||
tree, err := restic.LoadTree(ctx, repo, id)
|
tree, err := restic.LoadTree(ctx, repo, id)
|
||||||
if ctx.Err() != nil {
|
if ctx.Err() != nil {
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Warnf("unable to load tree %v: %v\n", id.Str(), err)
|
printer.E("unable to load tree %v: %v\n", id.Str(), err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +102,7 @@ func runRecover(ctx context.Context, gopts GlobalOptions) error {
|
|||||||
}
|
}
|
||||||
bar.Done()
|
bar.Done()
|
||||||
|
|
||||||
Verbosef("load snapshots\n")
|
printer.P("load snapshots\n")
|
||||||
err = restic.ForAllSnapshots(ctx, snapshotLister, repo, nil, func(_ restic.ID, sn *restic.Snapshot, _ error) error {
|
err = restic.ForAllSnapshots(ctx, snapshotLister, repo, nil, func(_ restic.ID, sn *restic.Snapshot, _ error) error {
|
||||||
trees[*sn.Tree] = true
|
trees[*sn.Tree] = true
|
||||||
return nil
|
return nil
|
||||||
@@ -103,19 +110,19 @@ func runRecover(ctx context.Context, gopts GlobalOptions) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
Verbosef("done\n")
|
printer.P("done\n")
|
||||||
|
|
||||||
roots := restic.NewIDSet()
|
roots := restic.NewIDSet()
|
||||||
for id, seen := range trees {
|
for id, seen := range trees {
|
||||||
if !seen {
|
if !seen {
|
||||||
Verboseff("found root tree %v\n", id.Str())
|
printer.V("found root tree %v\n", id.Str())
|
||||||
roots.Insert(id)
|
roots.Insert(id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Printf("\nfound %d unreferenced roots\n", len(roots))
|
printer.S("\nfound %d unreferenced roots\n", len(roots))
|
||||||
|
|
||||||
if len(roots) == 0 {
|
if len(roots) == 0 {
|
||||||
Verbosef("no snapshot to write.\n")
|
printer.P("no snapshot to write.\n")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,11 +170,11 @@ func runRecover(ctx context.Context, gopts GlobalOptions) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return createSnapshot(ctx, "/recover", hostname, []string{"recovered"}, repo, &treeID)
|
return createSnapshot(ctx, printer, "/recover", hostname, []string{"recovered"}, repo, &treeID)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func createSnapshot(ctx context.Context, name, hostname string, tags []string, repo restic.SaverUnpacked[restic.WriteableFileType], tree *restic.ID) error {
|
func createSnapshot(ctx context.Context, printer progress.Printer, name, hostname string, tags []string, repo restic.SaverUnpacked[restic.WriteableFileType], tree *restic.ID) error {
|
||||||
sn, err := restic.NewSnapshot([]string{name}, tags, hostname, time.Now())
|
sn, err := restic.NewSnapshot([]string{name}, tags, hostname, time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Fatalf("unable to save snapshot: %v", err)
|
return errors.Fatalf("unable to save snapshot: %v", err)
|
||||||
@@ -180,6 +187,6 @@ func createSnapshot(ctx context.Context, name, hostname string, tags []string, r
|
|||||||
return errors.Fatalf("unable to save snapshot: %v", err)
|
return errors.Fatalf("unable to save snapshot: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
Printf("saved new snapshot %v\n", id.Str())
|
printer.S("saved new snapshot %v\n", id.Str())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user