How I Test Database Restores (So My Backups Actually Work)
Every backup guide ends with "and test your restores." This is the concrete version of that advice — the routine I actually run, and what it catches that integrity_check alone does not.
The weekly restore test
#!/usr/bin/env bash
set -euo pipefail
BACKUP_DIR=/srv/myapp/backups
SCRATCH=$(mktemp -d)
LATEST=$(ls -t "$BACKUP_DIR"/app-*.db | head -1)
echo "testing restore of: $LATEST"
# 1) restore into a scratch file
cp "$LATEST" "$SCRATCH/restored.db"
# 2) structure check
sqlite3 "$SCRATCH/restored.db" "PRAGMA integrity_check;" | grep -q "^ok$"
# 3) content check: compare a real row count against production
EXPECTED=$(sqlite3 /srv/myapp/data/app.db "SELECT count(*) FROM items;")
ACTUAL=$(sqlite3 "$SCRATCH/restored.db" "SELECT count(*) FROM items;")
if [ "$EXPECTED" -ne "$ACTUAL" ]; then
echo "FAIL: row count mismatch ($EXPECTED vs $ACTUAL)"; exit 1
fi
echo "restore test PASSED"
rm -rf "$SCRATCH"
Why each check exists
integrity_checkproves the file's structure is valid.- The row count comparison proves the data survived — the thing
integrity_checkcannot see (it validates structure, not content). Two corrupted rows would passintegrity_checkbut fail the count. - The scratch directory proves the restore procedure itself works, not just that the file exists.
Run it on a schedule
Put the script behind a systemd timer (weekly) the same way you schedule the backups themselves. If the check fails, it should page you — a silent failing restore is worse than no test at all.
The metric that matters
Ask yourself: "If the server died right now, how long until I have data back?" If the answer is "I hope the backup works," you do not have a restore process — you have a folder. A weekly automated restore test turns that folder into a guarantee.
Size the database you are backing up with the estimator below, then schedule the test before you need it.
Comments (0)
No comments yet.