Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,19 @@ type BackupProgress struct {

// BackupSnapshot summarizes one immutable recovery point.
type BackupSnapshot struct {
ID string
ParentID string
CreatedAt string
Tag string
MetadataFormat string
Nodes int64
Files int64
Blobs int64
BlobBytes int64
PacksAdded int
BytesAdded int64
DurationSeconds float64
ID string
MinReaderVersion int
ParentID string
CreatedAt string
Tag string
MetadataFormat string
Nodes int64
Files int64
Blobs int64
BlobBytes int64
PacksAdded int
BytesAdded int64
DurationSeconds float64
}

// BackupVerifyProblem identifies one repository-integrity finding.
Expand Down Expand Up @@ -427,7 +428,8 @@ func backupSnapshot(manifest *backup.Manifest) (BackupSnapshot, error) {
}
return BackupSnapshot{
ID: manifest.SnapshotID, ParentID: manifest.ParentID, CreatedAt: manifest.CreatedAt,
Tag: manifest.Options.Tag, MetadataFormat: metadataFormat,
MinReaderVersion: manifest.MinReaderVersion,
Tag: manifest.Options.Tag, MetadataFormat: metadataFormat,
Nodes: stats.Nodes, Files: stats.Files, Blobs: stats.Blobs, BlobBytes: stats.BlobBytes,
PacksAdded: len(manifest.NewPacks), BytesAdded: manifest.BytesAdded,
DurationSeconds: manifest.DurationSeconds,
Expand Down
69 changes: 69 additions & 0 deletions backup_repository_restore_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package docbank_test

import (
"crypto/sha256"
"io"
"os"
"path/filepath"
"strconv"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -13,6 +15,73 @@ import (
"go.kenn.io/docbank/sqlite/modernc"
)

func TestRepositoryRestoreLargeExtraAfterSourceLoss(t *testing.T) {
r := require.New(t)
size := int64(64<<20 + 1)
if value := os.Getenv("DOCBANK_LARGE_OBJECT_TEST_BYTES"); value != "" {
var err error
size, err = strconv.ParseInt(value, 10, 64)
r.NoError(err)
r.Greater(size, int64(64<<20))
}
source := filepath.Join(t.TempDir(), "source")
vault, err := docbank.New(t.Context(), docbank.Config{Root: source})
r.NoError(err)
t.Cleanup(func() { r.NoError(vault.Close()) })
repositoryPath := filepath.Join(t.TempDir(), "repository")
repository, err := docbank.InitBackupRepository(repositoryPath)
r.NoError(err)
extra, err := os.CreateTemp(t.TempDir(), "recovery-*")
r.NoError(err)
t.Cleanup(func() { _ = extra.Close() })
r.NoError(extra.Truncate(size))
_, err = extra.WriteAt([]byte("synthetic recovery header"), 0)
r.NoError(err)
_, err = extra.WriteAt([]byte("tail"), size-4)
r.NoError(err)
digest := sha256.New()
n, err := io.Copy(digest, extra)
r.NoError(err)
r.Equal(size, n)
wantHash := digest.Sum(nil)
r.NoError(extra.Close())
snapshot, err := vault.CreateBackup(t.Context(), repository, docbank.BackupOptions{
ExtraFiles: []docbank.BackupExtraFile{{Path: extra.Name(), RecordAs: "application/recovery.bin"}},
})
r.NoError(err)
r.Equal(6, snapshot.MinReaderVersion)
r.NoError(vault.Close())
r.NoError(os.RemoveAll(source))
r.NoError(os.Remove(extra.Name()))
moved := filepath.Join(t.TempDir(), "moved-repository")
r.NoError(os.Rename(repositoryPath, moved))
repository, err = docbank.OpenBackupRepository(moved)
r.NoError(err)
snapshots, err := repository.Snapshots()
r.NoError(err)
r.Equal([]docbank.BackupSnapshot{snapshot}, snapshots)
_, err = repository.Prune(t.Context(), docbank.BackupPruneOptions{})
r.NoError(err)
verified, err := repository.Verify(t.Context(), docbank.BackupVerifyOptions{SnapshotID: snapshot.ID})
r.NoError(err)
r.Empty(verified.Problems)
report, err := repository.Restore(t.Context(), docbank.BackupRestoreOptions{
SnapshotID: snapshot.ID, Target: filepath.Join(t.TempDir(), "recovered"),
})
r.NoError(err)
r.Equal(snapshot.ID, report.SnapshotID)
r.Equal(1, report.ExtrasFiles)
restored, err := os.Open(filepath.Join(report.Target, "application", "recovery.bin"))
r.NoError(err)
t.Cleanup(func() { _ = restored.Close() })
digest.Reset()
n, err = io.Copy(digest, restored)
r.NoError(err)
r.NoError(restored.Close())
r.Equal(size, n)
r.Equal(wantHash, digest.Sum(nil))
}

func TestRepositoryRestoreAfterSourceLoss(t *testing.T) {
for _, tc := range []struct {
name string
Expand Down
21 changes: 20 additions & 1 deletion docs/embedding.md
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,11 @@ return err
```

Use `OpenBackupRepository` after process restart. `Snapshots` lists recovery
points in chronological order.
points in chronological order. Save the repository's `ID()` and the returned
snapshot's `ID` and `MinReaderVersion` with your recovery record.
`MinReaderVersion` names the required backup format reader, not a Docbank release.
Pass the saved snapshot ID to verification and restore so a newer backup on a
shared repository cannot change which recovery point you use.

Capture prevents content reclamation for the whole snapshot. Ordinary appends
resume once Docbank fixes the SQLite view that the backup will use. Physical
Expand All @@ -789,6 +793,20 @@ Your application must coordinate its own writes during `Prepare`; Docbank's
freeze pauses Docbank mutations, not changes to an unrelated application
database. The callback must not call vault mutations while that lock is held.

Extras larger than 64 MiB stream through bounded chunks, including files over
4 GiB. Those snapshots require backup reader version 6. Once one exists, older
readers cannot list snapshots, create backups, prune, or restore or verify the
latest snapshot; verification of all snapshots also fails. They can still
restore or verify a supported older snapshot by explicit ID. Upgrade every
reader before writing large extras to a shared repository. Existing extras
larger than 64 MiB are stored again as chunks on their first capture; their old
whole-file blobs remain until the older snapshots are removed and pruned.

Capture fails if an extra's length changes after it is opened. This does not
detect in-place changes, so the immutable-source requirement still applies.
Restore stages all extras as complete temporary files before replacing their
destinations. Allow disk space for those copies alongside any existing files.

Mark files containing credentials or tokens as `Sensitive`. Docbank rejects
sensitive files in a plaintext repository unless your application explicitly
sets `AllowPlaintextSecrets` for that backup.
Expand All @@ -809,6 +827,7 @@ if err != nil {
return err
}
_, err = repository.Restore(ctx, docbank.BackupRestoreOptions{
SnapshotID: snapshotID,
Target: restoreRoot,
ProtectedRoots: applicationStorageRoots,
})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require (
github.com/tdewolff/parse/v2 v2.8.16
github.com/tetratelabs/wazero v1.12.0
github.com/yuin/goldmark v1.8.5
go.kenn.io/kit v0.30.1-0.20261002205741-6db3beaab1fd
go.kenn.io/kit v0.30.1
golang.org/x/image v0.44.0
golang.org/x/net v0.59.0
golang.org/x/sync v0.23.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ github.com/yuin/goldmark v1.8.5 h1:r6N5afV5qj/5S4UTch8agZHJ8UxNCMwX7WjkkJam2NA=
github.com/yuin/goldmark v1.8.5/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.kenn.io/kit v0.30.1-0.20261002205741-6db3beaab1fd h1:+qI1uvLXEmxusfbjxQTwzmQ4ybXPhkzOIUYWjQ0Fn8c=
go.kenn.io/kit v0.30.1-0.20261002205741-6db3beaab1fd/go.mod h1:1dwPTub5BoAcdZT8dL6O4juFULzZ38b9tqHxiniduVk=
go.kenn.io/kit v0.30.1 h1:ehgo3jBDwCloS8o1vxTgGRowl418vOGV2YWF1ZRaKto=
go.kenn.io/kit v0.30.1/go.mod h1:1dwPTub5BoAcdZT8dL6O4juFULzZ38b9tqHxiniduVk=
go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE=
go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0=
go.opentelemetry.io/otel v1.44.0 h1:JjwHmHpA4iZ3wBxluu2fbbE7j4kqlE8jXyAyPXH7HqU=
Expand Down
Loading