2
2
mirror of https://github.com/octoleo/restic.git synced 2024-05-30 15:40:50 +00:00

Remove LoadAllSnapshots

This commit is contained in:
Alexander Weiss 2020-12-06 05:22:27 +01:00
parent 26f85779be
commit e329623771
3 changed files with 44 additions and 35 deletions

View File

@ -159,19 +159,13 @@ func runPruneWithRepo(opts PruneOptions, gopts GlobalOptions, repo *repository.R
Print("warning: running prune without a cache, this may be very slow!\n") Print("warning: running prune without a cache, this may be very slow!\n")
} }
Verbosef("loading all snapshots...\n")
snapshots, err := restic.LoadAllSnapshots(gopts.ctx, repo, ignoreSnapshots)
if err != nil {
return err
}
Verbosef("loading indexes...\n") Verbosef("loading indexes...\n")
err = repo.LoadIndex(gopts.ctx) err := repo.LoadIndex(gopts.ctx)
if err != nil { if err != nil {
return err return err
} }
usedBlobs, err := getUsedBlobs(gopts, repo, snapshots) usedBlobs, err := getUsedBlobs(gopts, repo, ignoreSnapshots)
if err != nil { if err != nil {
return err return err
} }
@ -537,19 +531,34 @@ func rebuildIndexFiles(gopts GlobalOptions, repo restic.Repository, removePacks
return DeleteFilesChecked(gopts, repo, obsoleteIndexes, restic.IndexFile) return DeleteFilesChecked(gopts, repo, obsoleteIndexes, restic.IndexFile)
} }
func getUsedBlobs(gopts GlobalOptions, repo restic.Repository, snapshots []*restic.Snapshot) (usedBlobs restic.BlobSet, err error) { func getUsedBlobs(gopts GlobalOptions, repo restic.Repository, ignoreSnapshots restic.IDSet) (usedBlobs restic.BlobSet, err error) {
ctx := gopts.ctx ctx := gopts.ctx
Verbosef("finding data that is still in use for %d snapshots\n", len(snapshots)) var snapshotTrees restic.IDs
Verbosef("loading all snapshots...\n")
err = restic.ForAllSnapshots(gopts.ctx, repo, ignoreSnapshots,
func(id restic.ID, sn *restic.Snapshot, err error) error {
debug.Log("add snapshot %v (tree %v, error %v)", id, *sn.Tree, err)
if err != nil {
return err
}
snapshotTrees = append(snapshotTrees, *sn.Tree)
return nil
})
if err != nil {
return nil, err
}
Verbosef("finding data that is still in use for %d snapshots\n", len(snapshotTrees))
usedBlobs = restic.NewBlobSet() usedBlobs = restic.NewBlobSet()
bar := newProgressMax(!gopts.Quiet, uint64(len(snapshots)), "snapshots") bar := newProgressMax(!gopts.Quiet, uint64(len(snapshotTrees)), "snapshots")
defer bar.Done() defer bar.Done()
for _, sn := range snapshots { for _, tree := range snapshotTrees {
debug.Log("process snapshot %v", sn.ID()) debug.Log("process tree %v", tree)
err = restic.FindUsedBlobs(ctx, repo, *sn.Tree, usedBlobs) err = restic.FindUsedBlobs(ctx, repo, tree, usedBlobs)
if err != nil { if err != nil {
if repo.Backend().IsNotExist(err) { if repo.Backend().IsNotExist(err) {
return nil, errors.Fatal("unable to load a tree from the repo: " + err.Error()) return nil, errors.Fatal("unable to load a tree from the repo: " + err.Error())
@ -558,7 +567,7 @@ func getUsedBlobs(gopts GlobalOptions, repo restic.Repository, snapshots []*rest
return nil, err return nil, err
} }
debug.Log("processed snapshot %v", sn.ID()) debug.Log("processed tree %v", tree)
bar.Add(1) bar.Add(1)
} }
return usedBlobs, nil return usedBlobs, nil

View File

@ -126,25 +126,6 @@ func ForAllSnapshots(ctx context.Context, repo Repository, excludeIDs IDSet, fn
return wg.Wait() return wg.Wait()
} }
// LoadAllSnapshots returns a list of all snapshots in the repo.
// If a snapshot ID is in excludeIDs, it will not be included in the result.
func LoadAllSnapshots(ctx context.Context, repo Repository, excludeIDs IDSet) (snapshots Snapshots, err error) {
err = ForAllSnapshots(ctx, repo, excludeIDs, func(id ID, sn *Snapshot, err error) error {
if err != nil {
return err
}
snapshots = append(snapshots, sn)
return nil
})
if err != nil {
return nil, err
}
return snapshots, nil
}
func (sn Snapshot) String() string { func (sn Snapshot) String() string {
return fmt.Sprintf("<Snapshot %s of %v at %s by %s@%s>", return fmt.Sprintf("<Snapshot %s of %v at %s by %s@%s>",
sn.id.Str(), sn.Paths, sn.Time, sn.Username, sn.Hostname) sn.id.Str(), sn.Paths, sn.Time, sn.Username, sn.Hostname)

View File

@ -17,6 +17,25 @@ const (
testDepth = 2 testDepth = 2
) )
// LoadAllSnapshots returns a list of all snapshots in the repo.
// If a snapshot ID is in excludeIDs, it will not be included in the result.
func loadAllSnapshots(ctx context.Context, repo restic.Repository, excludeIDs restic.IDSet) (snapshots restic.Snapshots, err error) {
err = restic.ForAllSnapshots(ctx, repo, excludeIDs, func(id restic.ID, sn *restic.Snapshot, err error) error {
if err != nil {
return err
}
snapshots = append(snapshots, sn)
return nil
})
if err != nil {
return nil, err
}
return snapshots, nil
}
func TestCreateSnapshot(t *testing.T) { func TestCreateSnapshot(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo, cleanup := repository.TestRepository(t)
defer cleanup() defer cleanup()
@ -25,7 +44,7 @@ func TestCreateSnapshot(t *testing.T) {
restic.TestCreateSnapshot(t, repo, testSnapshotTime.Add(time.Duration(i)*time.Second), testDepth, 0) restic.TestCreateSnapshot(t, repo, testSnapshotTime.Add(time.Duration(i)*time.Second), testDepth, 0)
} }
snapshots, err := restic.LoadAllSnapshots(context.TODO(), repo, restic.NewIDSet()) snapshots, err := loadAllSnapshots(context.TODO(), repo, restic.NewIDSet())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }