1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-25 09:18:55 +00:00

rewrite: prepare for code sharing with rewrite snapshots

This commit is contained in:
Michael Eischer 2022-12-27 21:05:21 +01:00
parent 903651c719
commit 375189488c

View file

@ -87,22 +87,31 @@ func rewriteSnapshot(ctx context.Context, repo *repository.Repository, sn *resti
return true return true
} }
return filterAndReplaceSnapshot(ctx, repo, sn,
func(ctx context.Context, sn *restic.Snapshot) (restic.ID, error) {
return walker.FilterTree(ctx, repo, "/", *sn.Tree, &walker.TreeFilterVisitor{
SelectByName: selectByName,
PrintExclude: func(path string) { Verbosef(fmt.Sprintf("excluding %s\n", path)) },
})
}, opts.DryRun, opts.Forget, "rewrite")
}
func filterAndReplaceSnapshot(ctx context.Context, repo restic.Repository, sn *restic.Snapshot, filter func(ctx context.Context, sn *restic.Snapshot) (restic.ID, error), dryRun bool, forget bool, addTag string) (bool, error) {
wg, wgCtx := errgroup.WithContext(ctx) wg, wgCtx := errgroup.WithContext(ctx)
repo.StartPackUploader(wgCtx, wg) repo.StartPackUploader(wgCtx, wg)
var filteredTree restic.ID var filteredTree restic.ID
wg.Go(func() error { wg.Go(func() error {
filteredTree, err = walker.FilterTree(wgCtx, repo, "/", *sn.Tree, &walker.TreeFilterVisitor{ var err error
SelectByName: selectByName, filteredTree, err = filter(ctx, sn)
PrintExclude: func(path string) { Verbosef(fmt.Sprintf("excluding %s\n", path)) },
})
if err != nil { if err != nil {
return err return err
} }
return repo.Flush(wgCtx) return repo.Flush(wgCtx)
}) })
err = wg.Wait() err := wg.Wait()
if err != nil { if err != nil {
return false, err return false, err
} }
@ -113,10 +122,10 @@ func rewriteSnapshot(ctx context.Context, repo *repository.Repository, sn *resti
} }
debug.Log("Snapshot %v modified", sn) debug.Log("Snapshot %v modified", sn)
if opts.DryRun { if dryRun {
Verbosef("would save new snapshot\n") Verbosef("would save new snapshot\n")
if opts.Forget { if forget {
Verbosef("would remove old snapshot\n") Verbosef("would remove old snapshot\n")
} }
@ -125,10 +134,10 @@ func rewriteSnapshot(ctx context.Context, repo *repository.Repository, sn *resti
// Always set the original snapshot id as this essentially a new snapshot. // Always set the original snapshot id as this essentially a new snapshot.
sn.Original = sn.ID() sn.Original = sn.ID()
*sn.Tree = filteredTree sn.Tree = &filteredTree
if !opts.Forget { if !forget {
sn.AddTags([]string{"rewrite"}) sn.AddTags([]string{addTag})
} }
// Save the new snapshot. // Save the new snapshot.
@ -138,7 +147,7 @@ func rewriteSnapshot(ctx context.Context, repo *repository.Repository, sn *resti
} }
Verbosef("saved new snapshot %v\n", id.Str()) Verbosef("saved new snapshot %v\n", id.Str())
if opts.Forget { if forget {
h := restic.Handle{Type: restic.SnapshotFile, Name: sn.ID().String()} h := restic.Handle{Type: restic.SnapshotFile, Name: sn.ID().String()}
if err = repo.Backend().Remove(ctx, h); err != nil { if err = repo.Backend().Remove(ctx, h); err != nil {
return false, err return false, err