Report #10682
[bug\_fix] database disk image is malformed \(SQLite corruption\)
Restore from a verified backup taken before the corruption event, or use the \`.recover\` command \(SQLite 3.29\+\) to salvage data from the corrupt file. To prevent recurrence, enable WAL mode for better crash resilience, ensure the database is not stored on NFS \(which can cause locking issues leading to corruption\), and implement a UPS \(Uninterruptible Power Supply\) for graceful shutdowns. The root cause is usually power loss or filesystem unmounting during a commit, leaving the database file in a partially written state.
Journey Context:
A developer runs a Python-based data logger on a Raspberry Pi in a remote weather station, storing readings in a SQLite database on an external USB flash drive. After a power outage, the script crashes on startup with "sqlite3.DatabaseError: database disk image is malformed". Running \`PRAGMA integrity\_check;\` reveals numerous errors like "database disk image is malformed" and " btree is malformed". The developer initially attempts to dump the data using \`.dump\`, but the process fails mid-way with an error on a specific corrupted page. The developer considers the data lost, but then discovers the \`.recover\` command introduced in SQLite 3.29, which attempts to salvage data directly from the database pages bypassing the B-tree logic. Running \`.recover\` successfully extracts 99% of the data into a new clean database file. Investigation reveals the root cause was the USB drive's write cache not being flushed to disk when power was lost, combined with the Raspberry Pi's sudden shutdown. The fix involves implementing a UPS \(Uninterruptible Power Supply\) to allow graceful shutdowns, switching the database to WAL mode \(PRAGMA journal\_mode=WAL\) which is more resilient to corruption due to its append-only nature, and moving the database to the Pi's SD card \(with regular backups\) instead of the unreliable USB drive. The developer also sets up a cron job to run \`PRAGMA integrity\_check;\` weekly to detect corruption early.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T11:20:09.683209+00:00— report_created — created