Sub topic 15
Testing Database Restores
Assume you can use the Linux command line, run commands with sudo, edit configuration files, and run basic SQL. This chapter uses PostgreSQL examples and notes the MySQL equivalent.
A backup is useful only if you can restore it. A successful backup command proves that data was read. It does not prove that the backup is complete, readable, or usable after a failure. Test restores regularly in a separate scratch database or host.
Restore into an isolated database
Never test by overwriting the production database. Restore into a new database with a name such as appdb_restore_test. Use a separate host when possible so that the test also checks storage, permissions, network access, and required extensions.
For a PostgreSQL custom-format dump, create the scratch database and restore the dump:
createdb appdb_restore_test
pg_restore --dbname=appdb_restore_test --verbose appdb.dumpFor a MySQL SQL dump, create the database and load the file:
mysql -e 'CREATE DATABASE appdb_restore_test'
mysql appdb_restore_test < appdb.sqlThe account running these commands must have permission to create databases and restore objects. Restore failures often identify missing extensions, unavailable users, incompatible versions, or incorrect file permissions.
Validate the restored data
Check more than the exit status. Compare important table counts, confirm indexes and constraints exist, and run representative queries. A count check is not a complete integrity test, but it provides a quick signal.
SELECT COUNT(*) AS customer_count FROM customers;
SELECT COUNT(*) AS order_count FROM orders;
SELECT MAX(created_at) AS newest_order FROM orders;Compare these results with a known production report captured near the backup time. Also test an application read path and, in a controlled scratch environment, a transaction that inserts and rolls back a row.
| Check | What it proves |
|---|---|
| Restore command succeeds | The backup can be read and loaded |
| Expected tables exist | The schema was restored |
| Counts and timestamps match | Important data is present |
| Application smoke test passes | Clients can use the restored schema |
| Measured duration is recorded | Recovery time can be compared with the target |
Record recovery results
Record the backup identifier, source database version, restore start and end times, errors, validation queries, and the result. The restore duration is evidence for the recovery time objective (RTO), which is the maximum acceptable time to resume service. The oldest data that can be restored defines the recovery point objective (RPO).
MySQL administrators should also check that routines, events, triggers, and character-set settings were included in the dump. PostgreSQL administrators should check extensions, ownership, and privileges.
Exercise
Why should a restore test use a separate database instead of the production database?
Show answer
It prevents the test from overwriting live data and lets you validate the backup safely.