mirror of
https://github.com/restic/restic.git
synced 2025-10-20 18:48:36 +00:00
Update snapshot summary on rewrite
Signed-off-by: Alex Johnson <hello@alex-johnson.net>
This commit is contained in:
@@ -134,20 +134,29 @@ func rewriteSnapshot(ctx context.Context, repo *repository.Repository, sn *resti
|
||||
return true
|
||||
}
|
||||
|
||||
rewriter := walker.NewTreeRewriter(walker.RewriteOpts{
|
||||
RewriteNode: func(node *restic.Node, path string) *restic.Node {
|
||||
if selectByName(path) {
|
||||
return node
|
||||
}
|
||||
Verbosef(fmt.Sprintf("excluding %s\n", path))
|
||||
return nil
|
||||
},
|
||||
DisableNodeCache: true,
|
||||
})
|
||||
rewriteNode := func(node *restic.Node, path string) *restic.Node {
|
||||
if selectByName(path) {
|
||||
return node
|
||||
}
|
||||
Verbosef(fmt.Sprintf("excluding %s\n", path))
|
||||
return nil
|
||||
}
|
||||
|
||||
rewriter, querySize := walker.NewSnapshotSizeRewriter(rewriteNode)
|
||||
|
||||
filter = func(ctx context.Context, sn *restic.Snapshot) (restic.ID, error) {
|
||||
return rewriter.RewriteTree(ctx, repo, "/", *sn.Tree)
|
||||
id, err := rewriter.RewriteTree(ctx, repo, "/", *sn.Tree)
|
||||
if err != nil {
|
||||
return restic.ID{}, err
|
||||
}
|
||||
ss := querySize()
|
||||
if sn.Summary != nil {
|
||||
sn.Summary.TotalFilesProcessed = ss.FileCount
|
||||
sn.Summary.TotalBytesProcessed = ss.FileSize
|
||||
}
|
||||
return id, err
|
||||
}
|
||||
|
||||
} else {
|
||||
filter = func(_ context.Context, sn *restic.Snapshot) (restic.ID, error) {
|
||||
return *sn.Tree, nil
|
||||
|
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/restic/restic/internal/restic"
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
"github.com/restic/restic/internal/ui"
|
||||
)
|
||||
|
||||
func testRunRewriteExclude(t testing.TB, gopts GlobalOptions, excludes []string, forget bool, metadata snapshotMetadataArgs) {
|
||||
@@ -33,6 +34,24 @@ func createBasicRewriteRepo(t testing.TB, env *testEnvironment) restic.ID {
|
||||
return snapshotIDs[0]
|
||||
}
|
||||
|
||||
func getSnapshot(t testing.TB, snapshotID restic.ID, env *testEnvironment) *restic.Snapshot {
|
||||
t.Helper()
|
||||
|
||||
ctx, repo, unlock, err := openWithReadLock(context.TODO(), env.gopts, false)
|
||||
rtest.OK(t, err)
|
||||
defer unlock()
|
||||
|
||||
snapshots, err := restic.TestLoadAllSnapshots(ctx, repo, nil)
|
||||
rtest.OK(t, err)
|
||||
|
||||
for _, s := range snapshots {
|
||||
if *s.ID() == snapshotID {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestRewrite(t *testing.T) {
|
||||
env, cleanup := withTestEnvironment(t)
|
||||
defer cleanup()
|
||||
@@ -63,10 +82,21 @@ func TestRewriteReplace(t *testing.T) {
|
||||
defer cleanup()
|
||||
snapshotID := createBasicRewriteRepo(t, env)
|
||||
|
||||
snapshot := getSnapshot(t, snapshotID, env)
|
||||
|
||||
// exclude some data
|
||||
testRunRewriteExclude(t, env.gopts, []string{"3"}, true, snapshotMetadataArgs{Hostname: "", Time: ""})
|
||||
bytesExcluded, err := ui.ParseBytes("16K")
|
||||
rtest.OK(t, err)
|
||||
|
||||
newSnapshotIDs := testListSnapshots(t, env.gopts, 1)
|
||||
rtest.Assert(t, snapshotID != newSnapshotIDs[0], "snapshot id should have changed")
|
||||
|
||||
newSnapshot := getSnapshot(t, newSnapshotIDs[0], env)
|
||||
|
||||
rtest.Equals(t, snapshot.Summary.TotalFilesProcessed-1, newSnapshot.Summary.TotalFilesProcessed, "snapshot file count should have changed")
|
||||
rtest.Equals(t, snapshot.Summary.TotalBytesProcessed-uint64(bytesExcluded), newSnapshot.Summary.TotalBytesProcessed, "snapshot size should have changed")
|
||||
|
||||
// check forbids unused blobs, thus remove them first
|
||||
testRunPrune(t, env.gopts, PruneOptions{MaxUnused: "0"})
|
||||
testRunCheck(t, env.gopts)
|
||||
|
Reference in New Issue
Block a user