Skip to content

Commit

Permalink
[FIX] Fix restore single file snapshot (when using backupcomamnd anno…
Browse files Browse the repository at this point in the history
…tation). Ref: #803
  • Loading branch information
poyaz committed Nov 25, 2023
1 parent a31a1c3 commit 3d3c017
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion restic/cli/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,13 @@ func (r *Restic) getLatestSnapshot(snapshotID string, log logr.Logger) (dto.Snap

func (r *Restic) folderRestore(restoreDir string, snapshot dto.Snapshot, restoreFilter string, verify bool, log logr.Logger) error {
var linkedDir string
if cfg.Config.RestoreTrimPath {

singleFile, err := r.isRestoreSingleFile(log, snapshot)
if err != nil {
return err
}

if !singleFile && cfg.Config.RestoreTrimPath {
restoreRoot, err := r.linkRestorePaths(snapshot, restoreDir)
if err != nil {
return err
Expand Down Expand Up @@ -221,6 +227,47 @@ func (r *Restic) linkRestorePaths(snapshot dto.Snapshot, restoreDir string) (str
return restoreRoot, nil
}

func (r *Restic) isRestoreSingleFile(log logr.Logger, snapshot dto.Snapshot) (bool, error) {
buf := bytes.Buffer{}

opts := CommandOptions{
Path: r.resticPath,
Args: r.globalFlags.ApplyToCommand("ls", "--json", snapshot.ID),
StdOut: &buf,
}

cmd := NewCommand(r.ctx, log, opts)
cmd.Run()
capturedStdOut := buf.String()

stdOutLines := strings.Split(capturedStdOut, "\n")

count := 0
for _, fileJSON := range stdOutLines {
node := &fileNode{}
err := json.Unmarshal([]byte(fileJSON), node)
if err != nil {
continue
}
if node.Type == "file" {
count++
}
if node.Type == "dir" {
count = 0
break
}
if count >= 2 {
break
}
}

if count == 1 {
return true, nil
}

return false, nil
}

func (r *Restic) parsePath(paths []string) string {
return path.Base(paths[len(paths)-1])
}
Expand Down

0 comments on commit 3d3c017

Please sign in to comment.