Introducing a new way to review your PRs.

Read the announcement

superradcompany / microsandbox

🧱 easy, fast, programmable and local-first microVM runtime and library

View on GitHub

27 documents · Updated Nov 23, 2026

docs / persistent-storage-backup-and-access

Persistent storage backup and access

The persistence layer protects the local SQLite catalog and the files that support it. It creates verified private copies for recovery work and refuses storage paths that do not provide a trusted access boundary. This page complements Persistent sandbox state and Persistence schema and migrations.

Database backup lifecycle

Backup creation

Database backup creation turns the live catalog into a separate SQLite file that is safe to publish only after the copy is complete and readable. The process creates an exclusive private staging file, protects it before SQLite writes, checks the finished copy, and then publishes it without replacing an existing backup.

The backup flow is:

flowchart LR
    A[Live catalog] --> B[Private staging file]
    B --> C[SQLite copy]
    C --> D[Read-only quick check]
    D --> E[Sync file and directory]
    E --> F[Publish backup name]
    C -. failure .-> G[Remove staging file]
    D -. failure .-> G

An interrupted or failed copy can leave only a private incomplete file. It cannot leave a completed backup at the requested name. An existing completed backup is also not replaced by a later failed attempt.

References: crates/…/backup.rs• L16-65 crates/…/backup_tests.rs• L34-122

Backup validation

Backup validation checks whether an existing file is a usable SQLite database without creating a missing file or changing its journal mode. It first applies the private-storage checks, rejects an empty file, opens the file as read-only, and requires SQLite quick_check to return ok.

Validation proves that the file is structurally readable. It does not prove that the backup contains every external artifact or that it can restore a full installation; those resources remain outside the database copy.

References: crates/…/backup.rs• L68-90 crates/…/macos_acl_tests.rs• L122-155

Recovery boundaries

Schema rollback recovery

The self-downgrade flow uses backups as a recovery boundary before it changes the live catalog. It first validates a separate preflight copy by applying the planned schema rollback, then creates or verifies the named backup, and only after that proceeds to revert stored artifacts and the live database.

The downgrade journal records the selected backup path and phase. If the operation stops before destructive work, the journal can be retired. After destructive work starts, the journal remains the guide for resuming or requiring recovery. The --no-backup option removes this safety copy by explicit user choice.

sequenceDiagram
    participant CLI as Self-downgrade
    participant Test as Preflight copy
    participant Live as Live catalog
    participant Backup as Verified backup
    CLI->>Test: Apply planned rollback
    Test-->>CLI: Validate SQLite copy
    CLI->>Backup: Create or validate backup
    Backup-->>CLI: Backup complete
    CLI->>Live: Revert artifacts and schema

A database backup therefore supports recovery of catalog state at the downgrade boundary. It is not a complete rollback of files outside the catalog, so artifact checks and the downgrade journal remain separate recovery controls.

References: crates/…/self_cmd.rs• L957-1044 crates/…/self_cmd.rs• L1062-1199 crates/…/self_cmd.rs• L2810-2851

Private catalog storage

Storage initialization

Private storage gives the catalog a directory and file boundary that belongs to the current account. Catalog writers may create the directory and database file. Readers may inspect an existing file but do not create a missing catalog or alter a secure read-only file.

The storage rules apply to the catalog and its SQLite sidecars. They also repair known legacy catalog files while leaving unrelated files and shared parent directories unchanged. The boundary resolves configured aliases before it checks ownership and path safety.

ResourceRequired boundaryPurpose
Catalog directoryOwner-private on UnixPrevent another account from reaching catalog files or replacing entries.
Database and sidecarsOwner-private regular filesProtect the catalog, WAL, shared-memory, and journal data.
Read-only catalogExisting trusted filePermit inspection without creating files or changing permissions.

References: crates/…/storage.rs• L18-71 crates/…/storage.rs• L80-123 crates/…/pool.rs• L81-107

Access boundary checks

Access checks fail closed when the storage path could be substituted by another account. The directory must be owned by the current account or root, its ancestors must provide a trusted path, and the catalog artifacts must be regular files with one link owned by the current account.

The checks reject symlinked catalog entries, hard-linked files, shared writable ancestors, and storage owned by another account. They change only the owned catalog boundary. They do not repair unrelated targets reached through a link or hard link.

References: crates/…/storage.rs• L126-207 crates/…/tests.rs• L63-106

Platform access controls

Unix systems use directory and file permission checks as the base boundary. macOS adds an ACL check because an ACL can grant access even when permission bits look private. The macOS path removes untrusted grants from the owned catalog directory or file, but it does not rewrite shared ancestors.

The platform policy preserves harmless owner, root, and deny entries. It refuses a shared ancestor with a mutation-granting ACL because changing that ancestor could affect unrelated data. Windows uses the common storage preparation path without the Unix mode and macOS ACL rules.

References: crates/…/storage.rs• L126-157 crates/…/macos_acl.rs• L81-172 crates/…/macos_acl.rs• L196-218

Validation paths

Catalog opening

Every shared database pool prepares the path before SQLite opens it and hardens the catalog files after the pool is connected. The write side owns catalog creation and serializes local writes. The read side uses an existing catalog and can inspect WAL-backed state without becoming a writer.

This common path keeps the host CLI and the in-VM runtime under the same storage rules. A direct read-only inspection also prepares legacy permissions before opening SQLite, so a control or usage lookup cannot bypass the private boundary.

References: crates/…/pool.rs• L37-55 crates/…/pool.rs• L68-107 crates/…/connection.rs• L47-100 sdk/…/database.rs• L47-87

Storage and backup tests

The validation suite checks both normal behavior and interruption boundaries. It confirms private modes under a permissive umask, safe handling of aliases, links, and shared parents, read-only inspection, concurrent opens, backup retry behavior, and protection during a sensitive backup write.

Platform-specific tests also check that macOS ACLs do not widen access and that unsafe grants or mutation-capable ancestors cause refusal. Some cross-account checks are ignored unless the host provides the required unprivileged test account and tools, so those checks need a suitable test environment to run.

References: crates/…/tests.rs• L25-124 crates/…/tests.rs• L151-210 crates/…/tests.rs• L262-338 crates/…/macos_acl_tests.rs• L162-224 crates/…/backup_tests.rs• L124-194 crates/…/backup_tests.rs• L283-330

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep backup staging, validation, publication, and existing-destination behavior aligned. A change in one step can expose partial or stale catalog data.
  • Keep path preparation consistent across writable pools, read-only inspection, backup validation, and downgrade copies. A new opener must not bypass ownership or sidecar checks.
  • Keep Unix mode checks, link checks, ancestor checks, and macOS ACL checks aligned. A permission-bit fix must not leave an ACL or path-substitution route open.
  • Keep downgrade journal phases and backup creation order aligned. Recovery must know whether the live catalog or external artifacts may already have changed.

Important failure modes

TriggerConsequenceGuard
SQLite copy fails or the process stops before publicationNo completed backup is available at the requested name; a private staging file may remain.Exclusive private staging and publish-after-validation.
Backup is empty, corrupt, or not a SQLite databaseRecovery treats the file as unusable.Read-only open plus SQLite quick_check.
Catalog path uses a symlink, hard link, or unsafe shared ancestorAnother path or account could read or replace catalog data.No-follow checks, ownership checks, and trusted-ancestor validation.
macOS ACL grants untrusted accessPermission bits do not describe the real access boundary.Remove untrusted ACLs on owned entries and reject unsafe ancestor grants.
Downgrade stops after destructive work beginsThe live installation may need an explicit recovery or resume operation.Durable downgrade journal and backup phase before live rollback.

Key files

FileWhy to read it
crates/…/backup.rsDefines private backup publication and SQLite validation.
crates/…/storage.rsDefines the shared catalog path, ownership, permission, and link boundary.
crates/…/macos_acl.rsDefines the macOS ACL rules that complement Unix permission checks.
crates/…/pool.rsShows how all database pools apply storage preparation and shared SQLite settings.
crates/…/self_cmd.rsShows how downgrade preflight, backup creation, journal phases, and live rollback are ordered.
crates/…/backup_tests.rsShows backup interruption, retry, validation, privacy, and destination-safety guarantees.
crates/…/tests.rsShows storage initialization, legacy repair, read-only access, and path-substitution validation.
crates/…/macos_acl_tests.rsShows platform-specific ACL preservation and refusal checks.