Backup / Restore

Restore

A restore puts a backup back into an empty directory, checking every byte against the manifest and the zip, and leaves nothing if any byte is wrong. It runs before a store opens there, so it is a command, and in Rust a function.

sh
tinystore restore backups/chat-2026-10-19.zip ./data
● ./data  restored from backups/chat-2026-10-19.zip · 3 files, 52 KiB
rust
tinystore::backup::restore("backups/chat-2026-10-19.zip", "data")?; // before Store::open
let store = Store::open("data", Options::default())?;

A program in TypeScript, Python or Go restores through the command line: a restore runs before a store opens, when an SDK has nothing yet to call.

await Bun.$`tinystore restore ${zip} ./data`
const store = await open('./data')
subprocess.run(["tinystore", "restore", zip_path, "./data"], check=True)
store = await tinystore.open("./data")
if err := exec.CommandContext(ctx, "tinystore", "restore", zip, "./data").Run(); err != nil {
	return err
}
store, err := tinystore.Open(ctx, "./data")

What a restore checks

StepIf it fails
the directory is empty or absentinvalid: a restore never mixes two stores
no store holds the directoryin use
the manifest reads, of format 1corrupt, naming both formats
on Windows, no file is named as a deviceinvalid, naming the database or the file
each file's CRC-32, size and SHA-256 matchcorrupt, naming the file
  • The restore holds the directory's LOCK meanwhile, so no store opens there before it ends.
  • Each file is written beside its name, checked, synced, and only then named. A file that does not match fails the restore and leaves the directory empty (contract BAK-02).
  • Before its first file, the restore writes a RESTORING file into the directory, and removes it once every file is synced. If the restore's process dies in between, RESTORING stays and the directory does not open, in a program or in the command line: the error, invalid, says to remove the directory and restore again. A restore into it is refused the same way.
  • On Windows, a backup that holds a file named as Windows names a device, con, prn, aux, nul, com0 to com9 or lpt0 to lpt9, whatever its extension, is refused before anything is written: before Windows 11, sql/nul.db is the device NUL, not a file. Such a backup comes from Linux or macOS, of a database an older release let a program name so, and restores there as it is.
  • A restore of a backup without the encryption key says so: its encrypted buckets need the key put back first. See The encryption key.
tinystore restore: restore full.zip into ./data: the directory is not empty: a restore writes into an empty one: invalid

Check a backup without restoring it

tinystore verify makes every check of the table above that reads the zip, and writes nothing: no directory, no file, no LOCK (contract BAK-06). It prints the bytes a restore of it would write, so you can check the disk has room before one.

sh
tinystore verify backups/chat-2026-10-19.zip
● backups/chat-2026-10-19.zip  ok · 3 files · 52 KiB · checked in 0.1s
  a restore of it writes 53248 bytes

A file that does not match exits 1, naming it:

tinystore verify: verify backups/chat-2026-10-19.zip: sql/chat.db: its SHA-256 is not its manifest's: corrupt

In Rust it is a function, tinystore::backup::verify, which answers what the backup holds as restore does. The SDKs have no restore, so they have no verify either: run the command.

await Bun.$`tinystore verify ${zip}`
subprocess.run(["tinystore", "verify", zip_path], check=True)
if err := exec.CommandContext(ctx, "tinystore", "verify", zip).Run(); err != nil {
	return err
}
let backup = tinystore::backup::verify("backups/chat-2026-10-19.zip")?; // Backup { files, bytes, .. }

Verify reads every byte, as a restore does, so it takes about as long as one without the writes. It tells you the zip is whole, not that it holds the rows you expect: that is what a restore you test, below, is for.

Test your restores

A backup nobody restored is a hope. Restore last night's into a scratch directory on a schedule, open it, and read something you know is there:

await Bun.$`tinystore restore ${zip} ${scratch}`
const restored = await open(scratch, { private: true })
const count = await (await restored.database('chat')).scalar<number>`select count(*) from messages`
if (count === 0) log.error('last night\'s backup restored empty', { zip })
await restored.close()
subprocess.run(["tinystore", "restore", zip_path, scratch], check=True)
async with await tinystore.open(scratch, private=True) as restored:
    db = await restored.database("chat")
    if await db.scalar(int, "select count(*) from messages") == 0:
        log.error("last night's backup restored empty", zip=zip_path)
if err := exec.CommandContext(ctx, "tinystore", "restore", zip, scratch).Run(); err != nil {
	return err
}
restored, err := tinystore.Open(ctx, scratch, tinystore.Private())
db, err := sqldb.Open(ctx, restored, "chat")
count, err := sqldb.Scalar[int64](ctx, db, `select count(*) from messages`)
tinystore::backup::restore(&zip, &scratch)?;
let restored = Store::open(&scratch, Options::default())?;
let count: i64 = restored.database("chat").open()?.scalar("select count(*) from messages")?;

The restore itself checks every byte; reading a row checks that the backup holds what you think it does.

What a backup holds

A backup is a plain zip, its files stored without compression, which any tool that opens a zip opens:

manifest.json         { format: 1, created, files: [{ engine, name, size, sha256 }], keyLeftOut }
kv.db                 each engine's file, all at one moment
jobs.db
sql/chat.db
blobs/blobs.db        and every file blobs keeps
app.json              a file of the program's, when included

Every SQLite file is copied at one moment of the store. For that instant, the backup holds back every file's commits and begins a read of each. Then the commits go on, and no commit waits for the copies. Each copy is made from its file's read, with every row and no free page. A transaction under way finishes first. While one runs longer than a tenth of a second, the backup lets the other commits through as it waits. A file that blobs keeps is linked into the snapshot rather than copied, so a large file costs the snapshot a directory entry until the zip is written.

The snapshot sits in the store's directory, on the store's disk. Before it copies anything, the backup checks that the disk has room for every SQLite file and its WAL on top of the space the store keeps free, keep_free, 1 GiB unless the store's options say otherwise. Without that room the backup fails with limit at once, saying how much its snapshot needs and how much is free, and copies nothing. The zip needs room of its own where it is written, which the backup does not check.

See also

Edit this page on GitHub Updated 16 November 2026

esc
↑↓ navigate ↵ open ⇥ filter 0 results