Show restore progress details

This commit is contained in:
Mikei386
2026-06-21 22:59:56 +02:00
parent c014a5f502
commit 41ac4ba43e
10 changed files with 84 additions and 15 deletions
+8 -3
View File
@@ -280,12 +280,17 @@ func (r *Runner) List(ctx context.Context, repo model.Repository, snapshot, path
return items, scanner.Err()
}
func (r *Runner) Restore(ctx context.Context, repo model.Repository, task model.RestoreTask) error {
args := []string{"restore", task.SnapshotID, "--target", task.Target}
func (r *Runner) Restore(ctx context.Context, repo model.Repository, task model.RestoreTask, progress ProgressCallback) error {
args := []string{"restore", task.SnapshotID, "--target", task.Target, "--json"}
for _, include := range task.Includes {
args = append(args, "--include", include)
}
return r.run(ctx, repo, args, nil, nil)
return r.run(ctx, repo, args, func(line []byte) {
var status Progress
if json.Unmarshal(line, &status) == nil && status.MessageType == "status" && progress != nil {
progress(status)
}
}, nil)
}
func (r *Runner) run(ctx context.Context, repo model.Repository, args []string, onLine func([]byte), capture *[]byte) error {
+26
View File
@@ -164,6 +164,32 @@ func TestBackupImageStreamsRawDeviceWithStableFilename(t *testing.T) {
}
}
func TestRestoreReportsProgress(t *testing.T) {
dir := t.TempDir()
argsPath := filepath.Join(dir, "args")
script := filepath.Join(dir, "restic")
body := fmt.Sprintf("#!/bin/sh\nprintf '%%s\\n' \"$@\" > '%s'\nprintf '%%s\\n' '{\"message_type\":\"status\",\"percent_done\":0.25,\"total_files\":8,\"files_done\":2,\"total_bytes\":4096,\"bytes_done\":1024,\"seconds_remaining\":30,\"current_files\":[\"/restore/file.txt\"]}'\n", argsPath)
if err := os.WriteFile(script, []byte(body), 0700); err != nil {
t.Fatal(err)
}
runner := &Runner{Binary: script, RuntimeDir: dir, Secrets: fakeSecrets{"password": "secret"}}
repo := model.Repository{Type: model.RepositoryLocal, Location: "/repo", PasswordRef: "password"}
task := model.RestoreTask{SnapshotID: "snap123", Target: "/target", Includes: []string{"/source/file.txt"}}
var progress Progress
if err := runner.Restore(context.Background(), repo, task, func(value Progress) { progress = value }); err != nil {
t.Fatal(err)
}
if progress.PercentDone != 0.25 || progress.FilesDone != 2 || progress.BytesDone != 1024 || progress.SecondsRemaining != 30 || len(progress.CurrentFiles) != 1 {
t.Fatalf("unexpected progress: %+v", progress)
}
args, _ := os.ReadFile(argsPath)
for _, expected := range []string{"restore", "snap123", "--target", "/target", "--json", "--include", "/source/file.txt"} {
if !strings.Contains(string(args), expected) {
t.Fatalf("arguments missing %q: %s", expected, args)
}
}
}
func TestStatsUsesRequestedRepositoryCountingMode(t *testing.T) {
dir := t.TempDir()
argsPath := filepath.Join(dir, "args")
+24 -2
View File
@@ -732,13 +732,35 @@ func (s *Service) executeRestore(ctx context.Context, run model.Run) model.Run {
defer s.mounts.Cleanup(context.Background(), mounted)
appendLiveLog(&live, "Restic stellt den Snapshot wieder her")
s.updateActive(live)
if err := s.restic.Restore(ctx, mounted.Repository, task); err != nil {
lastLoggedPercent := -10
started := time.Now()
if err := s.restic.Restore(ctx, mounted.Repository, task, func(progress restic.Progress) {
live.ProgressPercent = progress.PercentDone * 100
live.ProgressBytes, live.ProgressTotal = progress.BytesDone, progress.TotalBytes
live.ProgressFiles, live.ProgressFileTotal = progress.FilesDone, progress.TotalFiles
live.SecondsRemaining = progress.SecondsRemaining
if elapsed := time.Since(started).Seconds(); elapsed > 0 {
live.BytesPerSecond = float64(progress.BytesDone) / elapsed
}
if len(progress.CurrentFiles) > 0 {
live.CurrentFile = progress.CurrentFiles[len(progress.CurrentFiles)-1]
}
percent := int(live.ProgressPercent)
if percent >= lastLoggedPercent+10 {
appendLiveLog(&live, fmt.Sprintf("Restore-Fortschritt: %.1f%%, %d/%d Dateien, %s/%s", live.ProgressPercent, live.ProgressFiles, live.ProgressFileTotal, formatBytes(live.ProgressBytes), formatBytes(live.ProgressTotal)))
lastLoggedPercent = percent - percent%10
}
s.updateActive(live)
}); err != nil {
failedRun := failedError(run, err)
appendLiveLog(&live, "Restore fehlgeschlagen: "+err.Error())
failedRun.LiveLog = live.LiveLog
return failedRun
}
run.Status, run.Message, run.JobID = "success", "restore completed", ""
run.Status, run.Message = "success", "restore completed"
run.ProgressPercent, run.ProgressBytes, run.ProgressTotal = 100, live.ProgressBytes, live.ProgressTotal
run.ProgressFiles, run.ProgressFileTotal = live.ProgressFiles, live.ProgressFileTotal
run.BytesPerSecond, run.SecondsRemaining, run.CurrentFile = live.BytesPerSecond, 0, live.CurrentFile
appendLiveLog(&live, "Restore abgeschlossen")
run.LiveLog = live.LiveLog
return run