mirror of https://github.com/restic/restic.git
Move snapshot finding functions to new file
This commit is contained in:
parent
5a34799554
commit
77037e33c9
|
@ -6,8 +6,6 @@ import (
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"restic/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Snapshot is the state of a resource at one point in time.
|
// Snapshot is the state of a resource at one point in time.
|
||||||
|
@ -189,46 +187,3 @@ func (sn Snapshots) Less(i, j int) bool {
|
||||||
func (sn Snapshots) Swap(i, j int) {
|
func (sn Snapshots) Swap(i, j int) {
|
||||||
sn[i], sn[j] = sn[j], sn[i]
|
sn[i], sn[j] = sn[j], sn[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrNoSnapshotFound is returned when no snapshot for the given criteria could be found.
|
|
||||||
var ErrNoSnapshotFound = errors.New("no snapshot found")
|
|
||||||
|
|
||||||
// FindLatestSnapshot finds latest snapshot with optional target/directory, tags and hostname filters.
|
|
||||||
func FindLatestSnapshot(ctx context.Context, repo Repository, targets []string, tags []string, hostname string) (ID, error) {
|
|
||||||
var (
|
|
||||||
latest time.Time
|
|
||||||
latestID ID
|
|
||||||
found bool
|
|
||||||
)
|
|
||||||
|
|
||||||
for snapshotID := range repo.List(ctx, SnapshotFile) {
|
|
||||||
snapshot, err := LoadSnapshot(ctx, repo, snapshotID)
|
|
||||||
if err != nil {
|
|
||||||
return ID{}, errors.Errorf("Error listing snapshot: %v", err)
|
|
||||||
}
|
|
||||||
if snapshot.Time.After(latest) && (hostname == "" || hostname == snapshot.Hostname) && snapshot.HasTags(tags) && snapshot.HasPaths(targets) {
|
|
||||||
latest = snapshot.Time
|
|
||||||
latestID = snapshotID
|
|
||||||
found = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !found {
|
|
||||||
return ID{}, ErrNoSnapshotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
return latestID, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindSnapshot takes a string and tries to find a snapshot whose ID matches
|
|
||||||
// the string as closely as possible.
|
|
||||||
func FindSnapshot(repo Repository, s string) (ID, error) {
|
|
||||||
|
|
||||||
// find snapshot id with prefix
|
|
||||||
name, err := Find(repo.Backend(), SnapshotFile, s)
|
|
||||||
if err != nil {
|
|
||||||
return ID{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return ParseID(name)
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
package restic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"restic/errors"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrNoSnapshotFound is returned when no snapshot for the given criteria could be found.
|
||||||
|
var ErrNoSnapshotFound = errors.New("no snapshot found")
|
||||||
|
|
||||||
|
// FindLatestSnapshot finds latest snapshot with optional target/directory, tags and hostname filters.
|
||||||
|
func FindLatestSnapshot(ctx context.Context, repo Repository, targets []string, tags []string, hostname string) (ID, error) {
|
||||||
|
var (
|
||||||
|
latest time.Time
|
||||||
|
latestID ID
|
||||||
|
found bool
|
||||||
|
)
|
||||||
|
|
||||||
|
for snapshotID := range repo.List(ctx, SnapshotFile) {
|
||||||
|
snapshot, err := LoadSnapshot(ctx, repo, snapshotID)
|
||||||
|
if err != nil {
|
||||||
|
return ID{}, errors.Errorf("Error listing snapshot: %v", err)
|
||||||
|
}
|
||||||
|
if snapshot.Time.After(latest) && (hostname == "" || hostname == snapshot.Hostname) && snapshot.HasTags(tags) && snapshot.HasPaths(targets) {
|
||||||
|
latest = snapshot.Time
|
||||||
|
latestID = snapshotID
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
return ID{}, ErrNoSnapshotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return latestID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindSnapshot takes a string and tries to find a snapshot whose ID matches
|
||||||
|
// the string as closely as possible.
|
||||||
|
func FindSnapshot(repo Repository, s string) (ID, error) {
|
||||||
|
|
||||||
|
// find snapshot id with prefix
|
||||||
|
name, err := Find(repo.Backend(), SnapshotFile, s)
|
||||||
|
if err != nil {
|
||||||
|
return ID{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ParseID(name)
|
||||||
|
}
|
Loading…
Reference in New Issue