Applications and operations
Back up SQLite safely
Create and verify a consistent backup without copying one file from an active database.
8 minute lesson
If every connection is closed, you can copy the database file. While the database is active, use a SQLite-aware backup method.
From the SQLite shell, create a consistent backup with:
.backup notes-backup.db
Alternatively, use your library’s SQLite backup API or VACUUM INTO when that behavior fits the application.
Do not copy only notes.db while writes continue. In WAL mode, committed data may still be in notes.db-wal.
Open the backup separately and verify it:
sqlite3 notes-backup.db
PRAGMA integrity_check;
PRAGMA foreign_keys = ON;
PRAGMA foreign_key_check;
SELECT COUNT(*) FROM notes;
integrity_check should return ok. foreign_key_check should return no rows. A backup is useful only after you prove it restores.
Lesson completed