Safely copy a SQLite database

It’s trivially easy to copy a SQLite database. It’s less trivial to do this in a way that won’t corrupt it. Here’s how:

$ sqlite3 some.db
sqlite> begin immediate;
<press CTRL+Z>
$ cp some.db some.db.backup
$ exit
sqlite> rollback;

This will give you a nice clean backup that’s sure to be in a proper state, since writing to the database half-way through your copying process is impossible.

Thanks to Habbie for this info!

dr. Sybren A. Stüvel
dr. Sybren A. Stüvel
Open Source software developer, photographer, drummer, and electronics tinkerer