Qcache
18 November 2026Storage & SQLite

Backing up SQLite in WAL mode without locking readers

When a database runs with PRAGMA journal_mode=WAL;, recent writes sit inside the -wal sidecar file rather than the main .db file. Running plain cp on the main file alone leaves you with a stale or inconsistent snapshot.

Use VACUUM INTO for clean single-file snapshots

SQLite 3.27+ includes VACUUM INTO, which produces a compacted, self-contained copy without interrupting concurrent readers:

sqlite3 /var/lib/app/data.db "VACUUM INTO '/var/backups/app/data-2026-11-18.db';"

Verify integrity right after

sqlite3 /var/backups/app/data-2026-11-18.db "PRAGMA integrity_check;"

Keeping seven daily snapshots rotated by modification time takes less than ten lines of shell and prevents silent corruption from ever overwriting your last good copy.