Database backup and recovery

Before shipping any database change — migration, schema update, or data transformation — verify that backup and recovery procedures are in place and tested.

Pre-deploy backup checklist

Before applying any migration or schema change to a production database:

  1. Back up the database — use .backup, VACUUM INTO, or the Online Backup API. For production server-side SQLite with Litestream, verify the continuous backup is current and a snapshot exists.
  2. Verify the backup is restorable — restore to a temporary location and run PRAGMA quick_check. A backup that cannot be restored is not a backup.
  3. Record the current schema versionPRAGMA user_version or equivalent. This is your rollback target if the migration fails.

WAL files during restore

When restoring a backup, you MUST delete any existing *-wal and *-shm files at the destination before copying the backup file. A stale or mismatched WAL file will corrupt the restored database.

rm -f restored.db-wal restored.db-shm
cp backup.db restored.db

Pre-deploy integrity check

Run PRAGMA quick_check on the production database before applying migrations. If the database is already damaged, applying a migration will make recovery harder. integrity_check is more thorough but slower — use it for scheduled audits, not deploy gates.

Post-deploy verification

After a migration completes:

  1. Run PRAGMA quick_check to confirm the database is intact
  2. Run PRAGMA foreign_key_check if the migration touched foreign key relationships
  3. Verify the schema version was incremented correctly
version
1.1.1
platforms
sqlite, postgresql
tags
database, backup, recovery, operations, litestream, wal
author
Mike Fullerton
modified
2026-04-09

Change History

Version Date Author Summary
1.1.1 2026-04-09 Mike Fullerton Add trigger tags
1.1.0 2026-04-09 Mike Fullerton Tailor for shipping use case — focus on pre-deploy backup verification
1.0.1 2026-04-09 Mike Fullerton Reorganize into use-case directory
1.0.0 2026-04-06 Mike Fullerton Initial version