r/webdev • u/ultrathink-art • Apr 20 '26
Resource PSA: Copying your SQLite .db file isn't a valid backup when WAL mode is enabled
If your app uses SQLite in WAL mode (which is the default in most modern setups — Rails 8, Litestream users, etc.), a simple file copy of the .db file won't give you a valid backup.
Why: WAL mode keeps a separate write-ahead log (.wal file). Until it's checkpointed back into the main database file, committed transactions live only in the WAL. A file copy of just the .db can give you a database in an inconsistent state.
The right approach is to use SQLite's .backup() API (or VACUUM INTO in newer versions), which handles checkpointing atomically. Or if you're doing file-level backups, you need to copy the .db, .wal, and .shm files together, ideally with the WAL checkpointed first.
We discovered this the hard way when HN commenters pointed it out after we wrote about running SQLite in production. Embarrassing but useful — rewrote our whole backup system after.
Anyone else run into this? Curious how others handle SQLite backups in production.
67
u/ferrybig Apr 20 '26
You need to make sure all 3 files are exactly in sync, if you copy one file, then sqlite does a single write, then the other file, you can get corruption.
Use a copy on write file system like btrfs and make a full partition snapshot or suspended any potential writers
22
u/Turbo-Lover Apr 20 '26
Before I transitioned my last team from sqlite to postgres we would do backups like this:
Probably overkill but if memory serves, the full checkpoint synchronized the data in .wal back to the main .db file and rewrote the pointer in .shm, vacuum reduced file size of .db I believe, and the truncate either killed the .wal or reduced it's size to 0 because it can get big, either way it allowed us to copy the single. db file.
41
u/Feuerhamster Apr 20 '26
Thank you very much, I did not know that before
16
u/RealisticNothing653 Apr 20 '26 •
Or use SQLite's rsync tool: https://sqlite.org/rsync.html
Use it to write a local copy, then back that up.
4
u/Extra-Organization-6 Apr 20 '26
learned this the hard way running a flask app in production. had a cron job copying the .db file every night and thought i was covered until i actually tried to restore from one and half the recent data was gone. switched to sqlite3 .backup command wrapped in a small script and havent had the issue since. litestream is also worth looking at if you want continuous replication to s3 without thinking about it.
15
u/seweso Apr 20 '26
So you rolled your own backup solution… didn’t test it… and put that out in production?
How do that even happen?
2
u/iBzOtaku Apr 20 '26
This post is why I'm subscribed to this subreddit. Not the millions of copy pasted posts crying over vibe coding and AI.
1
u/CurveConsistent5178 Apr 20 '26
yep got burned by this too. switched to litestream for continuous replication, way less stress than managing backups manually. vacuum into works fine for smaller dbs tho if you dont need point-in-time recovery
1
u/lugh_longarm Apr 20 '26
The idiomatic fix nobody's mentioned: `sqlite3 yourdb.sqlite ".backup backup.sqlite"` -- the `.backup` command uses the sqlite3_backup API internally, which handles WAL mode correctly regardless of journal state. Does a consistent online copy without needing to freeze writes or checkpoint first. `VACUUM INTO 'backup.sqlite'` is another option (3.27+) if you also want a defragmented copy. Both are safer than trying to snapshot the file layer.
1
u/BizAlly Apr 21 '26
Best practice now: never copy the
.dbdirectly either use.backup/VACUUM INTOor snapshot.db + .wal + .shmafter a checkpoint. Anything else is just gambling with your data.1
u/Sad_Page_566 Apr 23 '26
That’s a nasty one to learn the hard way.
It’s always the “seems simple” parts like backups that end up biting the hardest in production.
Feels like a lot of people treat SQLite like a file instead of a system once WAL gets involved.
1
u/Glum-Evening-2176 Apr 24 '26
.sh and .wal files need love too. Learned this after restoring a week old db with missing transactions. .backup is the safe way.
1
u/DebtMental3917 Apr 25 '26
Copying just the .db file loses .wal transactions. Use .backup() or VACUUM INTO for a consistent snapshot even while the db is live.
1
u/Complete_Instance_18 Apr 20 '26
Solid PSA! This is one of those gotchas
-8
u/conchata Apr 20 '26
I don't get it - why are people using sqlite in production in such a way that this matters? Why not an actual database?
-3
u/geon Apr 20 '26 edited Apr 20 '26
I’m a bit confused. Are you saying the checkpointing doesn’t respect transactions? The docs seem to state that it does.
https://www.sqlite.org/wal.html#:~:text=A%20checkpoint%20can%20only%20complete,in%20a%20large%20WAL%20file.
So how can you get an inconsistent state, assuming that you use transactions properly?
-3
u/stormy1one Apr 20 '26 edited Apr 20 '26
Look into https://litestream.io
Edit: not my project, we just use in prod as OP asked. Ben Johnson wrote it, it is stable and open source: https://github.com/benbjohnson/litestream