backup: use unified FindFilteredSnapshot

This commit is contained in:
Michael Eischer 2022-10-03 14:27:11 +02:00
parent 0aa73bbd39
commit fcad5e6f5d
5 changed files with 18 additions and 24 deletions

View file

@ -505,27 +505,20 @@ func collectTargets(opts BackupOptions, args []string) (targets []string, err er
// parent returns the ID of the parent snapshot. If there is none, nil is // parent returns the ID of the parent snapshot. If there is none, nil is
// returned. // returned.
func findParentSnapshot(ctx context.Context, repo restic.Repository, opts BackupOptions, targets []string, timeStampLimit time.Time) (parentID *restic.ID, err error) { func findParentSnapshot(ctx context.Context, repo restic.Repository, opts BackupOptions, targets []string, timeStampLimit time.Time) (parentID *restic.ID, err error) {
// Force using a parent if opts.Force {
if !opts.Force && opts.Parent != "" { return nil, nil
id, err := restic.FindSnapshot(ctx, repo.Backend(), opts.Parent)
if err != nil {
return nil, errors.Fatalf("invalid id %q: %v", opts.Parent, err)
}
parentID = &id
} }
// Find last snapshot to set it as parent, if not already set snName := opts.Parent
if !opts.Force && parentID == nil { if snName == "" {
id, err := restic.FindLatestSnapshot(ctx, repo.Backend(), repo, targets, []restic.TagList{}, []string{opts.Host}, &timeStampLimit) snName = "latest"
if err == nil {
parentID = &id
} else if err != restic.ErrNoSnapshotFound {
return nil, err
}
} }
id, err := restic.FindFilteredSnapshot(ctx, repo.Backend(), repo, []string{opts.Host}, []restic.TagList{}, targets, &timeStampLimit, snName)
return parentID, nil // Snapshot not found is ok if no explicit parent was set
if opts.Parent == "" && errors.Is(err, restic.ErrNoSnapshotFound) {
err = nil
}
return &id, err
} }
func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, term *termstatus.Terminal, args []string) error { func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, term *termstatus.Terminal, args []string) error {

View file

@ -139,7 +139,7 @@ func runDump(ctx context.Context, opts DumpOptions, gopts GlobalOptions, args []
} }
} }
id, err := restic.FindFilteredSnapshot(ctx, repo.Backend(), repo, opts.Paths, opts.Tags, opts.Hosts, snapshotIDString) id, err := restic.FindFilteredSnapshot(ctx, repo.Backend(), repo, opts.Paths, opts.Tags, opts.Hosts, nil, snapshotIDString)
if err != nil { if err != nil {
Exitf(1, "failed to find snapshot: %v", err) Exitf(1, "failed to find snapshot: %v", err)
} }

View file

@ -210,7 +210,7 @@ func runLs(ctx context.Context, opts LsOptions, gopts GlobalOptions, args []stri
} }
} }
id, err := restic.FindFilteredSnapshot(ctx, snapshotLister, repo, opts.Hosts, opts.Tags, opts.Paths, args[0]) id, err := restic.FindFilteredSnapshot(ctx, snapshotLister, repo, opts.Hosts, opts.Tags, opts.Paths, nil, args[0])
if err != nil { if err != nil {
return err return err
} }

View file

@ -131,7 +131,7 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions, a
} }
} }
id, err := restic.FindFilteredSnapshot(ctx, repo.Backend(), repo, opts.Hosts, opts.Tags, opts.Paths, snapshotIDString) id, err := restic.FindFilteredSnapshot(ctx, repo.Backend(), repo, opts.Hosts, opts.Tags, opts.Paths, nil, snapshotIDString)
if err != nil { if err != nil {
Exitf(1, "failed to find snapshot %q: %v", snapshotIDString, err) Exitf(1, "failed to find snapshot %q: %v", snapshotIDString, err)
} }

View file

@ -2,6 +2,7 @@ package restic
import ( import (
"context" "context"
"fmt"
"path/filepath" "path/filepath"
"time" "time"
@ -88,11 +89,11 @@ func FindSnapshot(ctx context.Context, be Lister, s string) (ID, error) {
return ParseID(name) return ParseID(name)
} }
func FindFilteredSnapshot(ctx context.Context, be Lister, loader LoaderUnpacked, hosts []string, tags []TagList, paths []string, snapshotID string) (ID, error) { func FindFilteredSnapshot(ctx context.Context, be Lister, loader LoaderUnpacked, hosts []string, tags []TagList, paths []string, timeStampLimit *time.Time, snapshotID string) (ID, error) {
if snapshotID == "latest" { if snapshotID == "latest" {
id, err := FindLatestSnapshot(ctx, be, loader, paths, tags, hosts, nil) id, err := FindLatestSnapshot(ctx, be, loader, paths, tags, hosts, timeStampLimit)
if err == ErrNoSnapshotFound { if err == ErrNoSnapshotFound {
err = errors.Errorf("no snapshot matched given filter (Paths:%v Tags:%v Hosts:%v)", paths, tags, hosts) err = fmt.Errorf("snapshot filter (Paths:%v Tags:%v Hosts:%v): %w", paths, tags, hosts, err)
} }
return id, err return id, err
} else { } else {