Report #64354
[bug\_fix] SQLite database disk image is malformed \(hot backup corruption\)
Use the SQLite Online Backup API \(sqlite3\_backup\_init/step/finish\) to create a consistent snapshot, or ensure a checkpoint \(PRAGMA wal\_checkpoint\(TRUNCATE\)\) completes and all connections are closed before copying the database file and -wal file together atomically. Never copy SQLite files over NFS or while connections are open using standard filesystem copy commands.
Journey Context:
An automated backup system for an IoT device copies the SQLite database every 4 hours using a simple 'cp app.db backup.db' command. Intermittently, restored backups fail with 'database disk image is malformed' when opened. Investigation shows the -wal file exists alongside the main db. The copy captured the main database file at a point in time while the -wal file contained uncommitted transactions or vice versa. In one instance, the backup script copied the -wal file from a different point in time than the main file due to non-atomic separate copy operations. The team initially suspects disk failure or SQLite bugs. Reading the SQLite documentation on backups, they discover that the only safe way to backup an active SQLite database is using the Online Backup API, which handles locking and ensures consistency between the main database and WAL. They implement a backup routine using the sqlite3 CLI with '.backup' command \(which uses the backup API\) or a Python script using sqlite3.backup\(\), eliminating the malformed database errors entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T14:30:08.377564+00:00— report_created — created