restic: Add unified method to resolve a single snapshot

This commit is contained in:
Michael Eischer 2022-10-03 14:16:33 +02:00
parent 95a1bb4261
commit a81f0432e9
3 changed files with 22 additions and 24 deletions

View File

@ -139,18 +139,9 @@ func runDump(ctx context.Context, opts DumpOptions, gopts GlobalOptions, args []
}
}
var id restic.ID
if snapshotIDString == "latest" {
id, err = restic.FindLatestSnapshot(ctx, repo.Backend(), repo, opts.Paths, opts.Tags, opts.Hosts, nil)
if err != nil {
Exitf(1, "latest snapshot for criteria not found: %v Paths:%v Hosts:%v", err, opts.Paths, opts.Hosts)
}
} else {
id, err = restic.FindSnapshot(ctx, repo.Backend(), snapshotIDString)
if err != nil {
Exitf(1, "invalid id %q: %v", snapshotIDString, err)
}
id, err := restic.FindFilteredSnapshot(ctx, repo.Backend(), repo, opts.Paths, opts.Tags, opts.Hosts, snapshotIDString)
if err != nil {
Exitf(1, "failed to find snapshot: %v", err)
}
sn, err := restic.LoadSnapshot(ctx, repo, id)

View File

@ -131,18 +131,9 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions, a
}
}
var id restic.ID
if snapshotIDString == "latest" {
id, err = restic.FindLatestSnapshot(ctx, repo.Backend(), repo, opts.Paths, opts.Tags, opts.Hosts, nil)
if err != nil {
Exitf(1, "latest snapshot for criteria not found: %v Paths:%v Hosts:%v", err, opts.Paths, opts.Hosts)
}
} else {
id, err = restic.FindSnapshot(ctx, repo.Backend(), snapshotIDString)
if err != nil {
Exitf(1, "invalid id %q: %v", snapshotIDString, err)
}
id, err := restic.FindFilteredSnapshot(ctx, repo.Backend(), repo, opts.Hosts, opts.Tags, opts.Paths, snapshotIDString)
if err != nil {
Exitf(1, "failed to find snapshot %q: %v", snapshotIDString, err)
}
err = repo.LoadIndex(ctx)

View File

@ -88,6 +88,22 @@ func FindSnapshot(ctx context.Context, be Lister, s string) (ID, error) {
return ParseID(name)
}
func FindFilteredSnapshot(ctx context.Context, be Lister, loader LoaderUnpacked, hosts []string, tags []TagList, paths []string, snapshotID string) (ID, error) {
if snapshotID == "latest" {
id, err := FindLatestSnapshot(ctx, be, loader, paths, tags, hosts, nil)
if err == ErrNoSnapshotFound {
err = errors.Errorf("no snapshot matched given filter (Paths:%v Tags:%v Hosts:%v)", paths, tags, hosts)
}
return id, err
} else {
id, err := FindSnapshot(ctx, be, snapshotID)
if err != nil {
return ID{}, err
}
return id, err
}
}
type SnapshotFindCb func(string, *Snapshot, error) error
// FindFilteredSnapshots yields Snapshots, either given explicitly by `snapshotIDs` or filtered from the list of all snapshots.