r/sqlite • u/ultrathink-art • Apr 20 '26
WAL mode gotcha: copying your .db file isn't a valid backup when WAL is enabled
Common mistake: you set up SQLite in WAL mode (it's faster, great choice), then back it up by copying the .db file. This gives you a corrupted or outdated backup.
WAL mode keeps uncommitted changes in a separate .wal file. When you copy just the .db, you're missing those changes. Worse, whether the backup is valid depends on whether a checkpoint has run recently.
The fix is to use the sqlite3 backup API, or run PRAGMA wal_checkpoint(FULL) before copying. We learned this the hard way after HN commenters pointed out our backup script was silently broken: https://ultrathink.art/blog/hn-fixed-our-sqlite-backups?utm_source=reddit&utm_medium=social&utm_campaign=organic
The HN thread was humbling — turns out this is a really common misunderstanding. The official docs mention it but it's easy to miss if you're just following basic SQLite tutorials.
14
u/HamerUA Apr 20 '26
It is slso useful to read article "Hot to corrupt a SQLite database" from official docs :-)
6
u/HamerUA Apr 20 '26
sqlite_rsync
4
u/Dezgeg Apr 20 '26
I don't think WAL mode matters here, the situation should be the exact same with rollback journal - if it's present and you don't copy it, the database will be corrupted.
2
u/Common_Green_1666 Apr 20 '26
I don’t think this is a WAL gotcha. If there’s a process currently writing to the DB file and you copy it it in the middle, then your copy will have a corrupted database. The same issue will happen if you use a rollback journal. This is just a race condition issue.
To get a correct copy, you will need the kind of control over the DB file that only SQLite does. That’s why you need to use its backup API
1
u/aa599 Apr 20 '26
We have wal_checkpoint in our kopia snapshot before-folder-action
1
u/Spare_Warning7752 Apr 23 '26
Seems to me like skill issue (the skill to not read the documentation).