Report #86415
[bug\_fix] database disk image is malformed \(SQLITE\_CORRUPT\)
Use the SQLite Online Backup API \(sqlite3\_backup\_step / backup functions\) which properly handles file locking and ensures a consistent snapshot. Alternatively, ensure the application obtains a shared lock \(e.g., BEGIN IMMEDIATE\) and checkpoints the WAL \(PRAGMA wal\_checkpoint\) before copying files, then copy all three files \(.db, .db-wal, .db-shm\) atomically. Never copy the database file using standard filesystem utilities \(cp, rsync, File.copy\) while the database is open by any process. Root cause is copying a hot journal or WAL file separately from the main database, resulting in a torn page, mismatched transaction state, or partial write.
Journey Context:
An automated backup script running on a production server uses a simple shell command 'cp /var/lib/app/data.db /backups/data.db' to snapshot the SQLite database every night. After a disaster recovery event requiring restore from this backup, the application fails to start with 'database disk image is malformed'. Running PRAGMA integrity\_check returns multiple 'rowid xxx missing from index' and 'wrong \# of entries in index' errors. The database file size appears correct, but internal B-tree pages are corrupted. Investigation of the SQLite documentation on corruption causes reveals that copying an open SQLite database using operating system utilities is unsafe when the database is in WAL mode or has an active rollback journal. When the backup script executed 'cp', the main application had an active transaction with uncommitted pages in the -wal file; the cp command copied the main db file at a point in time that did not include the -wal data, or copied the -wal and -shm files in an inconsistent state relative to the main file. The fix requires replacing the cp command with the SQLite Online Backup API \(e.g., using the backup method in the language's SQLite driver, or the sqlite3 command-line .backup command\), which establishes a shared lock, checkpoints the WAL into the main file, and copies consistently. Alternatively, the application must be shut down or forced to release all connections \(deleting -wal and -shm\) before filesystem-level copies are attempted.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T03:38:17.103318+00:00— report_created — created