Skip to main content

Module backup

Module backup 

Source
Expand description

Single-file backup and restore for the libSQL database.

Backup uses VACUUM INTO, which writes a fully-checkpointed, consistent snapshot to a brand-new file with no -wal/-shm sidecars, regardless of the live database’s journal mode. Restore validates the snapshot, takes a safety backup of the current database, swaps the file into place atomically, and then deletes any stale WAL sidecars — left behind they would be replayed onto the restored file and corrupt it.

Hardening: both operations hold an exclusive lock file so a concurrent backup/restore (or a running MCP server doing the same) can’t race; snapshots are written 0600 (dir 0700) since the graph may hold secrets; every snapshot is PRAGMA integrity_check-verified before it is trusted; and the default backup directory is pruned to the most recent keep snapshots.

Functions§

backup
Snapshot the live database to output (or a timestamped default in the managed directory) via VACUUM INTO, verify its integrity, and — when using the default location — prune to the newest keep snapshots. Returns the path written. Refuses to overwrite an existing file.
effective_db_path
The live database file the CLI operates on: MIKU_DATABASE_URL if set, otherwise the resolved workspace db_path(). Backup and restore both go through this so they always target the same file as crate::db::init_db.
restore
Replace the live database with source. Consumes the open db/conn so the connection is fully closed (and the WAL flushed) before the file is swapped.