Database Restoring and Testing Backups
What you will learn
- Why an untested backup is only a hope
- How to restore from a logical dump
- How to check a restored database
- What a bad backup looks like
1. Test the restore, not the backup
Making a backup file succeeds even when the file is useless: the job may have run on an empty database, copied a damaged file, or run out of disk space. The only way to know is to restore it somewhere safe and look at the result. Do this on a regular schedule, and write down how long it took.
2. Restoring from a dump
Start with a dump saved as text, as in lesson 8.2. This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top). A browser has no files, so the cells play both roles inside your copy of the data: orders_original is the live table, and orders_restored is the "brand new database" that the dump is loaded into. Reset data removes both.
CREATE TABLE orders_original (id INTEGER PRIMARY KEY, customer TEXT NOT NULL, total INTEGER);
INSERT INTO orders_original (customer, total) VALUES ('Rina', 500), ('Sam', 300), ('Lee', 700);
(3 rows changed)
Now load the dump into the new table. These are the statements a dump of orders_original would contain:
CREATE TABLE orders_restored (id INTEGER PRIMARY KEY, customer TEXT NOT NULL, total INTEGER);
INSERT INTO orders_restored VALUES (1, 'Rina', 500);
INSERT INTO orders_restored VALUES (2, 'Sam', 300);
INSERT INTO orders_restored VALUES (3, 'Lee', 700);
SELECT * FROM orders_restored ORDER BY id;
id customer total
-- -------- -----
1 Rina 500
2 Sam 300
3 Lee 700
(3 rows)
CREATE TABLE orders_original (id INTEGER PRIMARY KEY, customer TEXT NOT NULL, total INTEGER);
INSERT INTO orders_original (customer, total) VALUES ('Rina', 500), ('Sam', 300), ('Lee', 700);
(3 rows changed)
Now load the dump into the new table. These are the statements a dump of orders_original would contain:
CREATE TABLE orders_restored (id INTEGER PRIMARY KEY, customer TEXT NOT NULL, total INTEGER);
INSERT INTO orders_restored VALUES (1, 'Rina', 500);
INSERT INTO orders_restored VALUES (2, 'Sam', 300);
INSERT INTO orders_restored VALUES (3, 'Lee', 700);
SELECT * FROM orders_restored ORDER BY id;
id customer total
-- -------- -----
1 Rina 500
2 Sam 300
3 Lee 700
(3 rows)
CREATE TABLE orders_original (id INTEGER PRIMARY KEY, customer TEXT NOT NULL, total INTEGER);
INSERT INTO orders_original (customer, total) VALUES ('Rina', 500), ('Sam', 300), ('Lee', 700);
(3 rows changed)
Now load the dump into the new table. These are the statements a dump of orders_original would contain:
CREATE TABLE orders_restored (id INTEGER PRIMARY KEY, customer TEXT NOT NULL, total INTEGER);
INSERT INTO orders_restored VALUES (1, 'Rina', 500);
INSERT INTO orders_restored VALUES (2, 'Sam', 300);
INSERT INTO orders_restored VALUES (3, 'Lee', 700);
SELECT * FROM orders_restored ORDER BY id;
id customer total
-- -------- -----
1 Rina 500
2 Sam 300
3 Lee 700
(3 rows)
3. Checking the result
Two checks are cheap and useful.
1. The database's own check. SQLite has PRAGMA integrity_check, which returns ok when it finds no damage:
PRAGMA integrity_check;
integrity_check
---------------
ok
(1 row)
PRAGMA integrity_check;
integrity_check
---------------
ok
(1 row)
PRAGMA integrity_check;
integrity_check
---------------
ok
(1 row)
2. Compare with the original. Compare row counts, totals, or whole tables. EXCEPT returns the rows of the first query that are missing from the second; zero both ways means the tables are identical:
SELECT COUNT(*) AS in_original_not_restored FROM (SELECT * FROM orders_original EXCEPT SELECT * FROM orders_restored);
SELECT COUNT(*) AS in_restored_not_original FROM (SELECT * FROM orders_restored EXCEPT SELECT * FROM orders_original);
in_original_not_restored
------------------------
0
(1 row)
in_restored_not_original
------------------------
0
(1 row)
SELECT COUNT(*) AS in_original_not_restored FROM (SELECT * FROM orders_original EXCEPT SELECT * FROM orders_restored);
SELECT COUNT(*) AS in_restored_not_original FROM (SELECT * FROM orders_restored EXCEPT SELECT * FROM orders_original);
in_original_not_restored
------------------------
0
(1 row)
in_restored_not_original
------------------------
0
(1 row)
SELECT COUNT(*) AS in_original_not_restored FROM (SELECT * FROM orders_original EXCEPT SELECT * FROM orders_restored);
SELECT COUNT(*) AS in_restored_not_original FROM (SELECT * FROM orders_restored EXCEPT SELECT * FROM orders_original);
in_original_not_restored
------------------------
0
(1 row)
in_restored_not_original
------------------------
0
(1 row)
On a big database you cannot compare everything, so you compare counts and sums of important columns.
4. A bad backup
Here a file that is not a database at all stands in for a corrupt backup:
open("bad.db","wb").write(b"this is not a database file" * 100)
try:
sqlite3.connect("bad.db").execute("SELECT * FROM orders")
except sqlite3.DatabaseError as e:
print("bad backup:", e)
bad backup: file is not a database
If you only checked that the file exists, you would think it was fine. The restore test finds the problem while you still have time to fix it.
Worked example: a monthly restore drill
- Take the newest backup.
- Restore it into a separate, empty database (never over the live one).
- Run the integrity check.
- Compare the number of rows in the main tables with the live database, allowing for new rows added since the backup.
- Write down how long it took and anything that went wrong.
- Delete the test copy if it holds private data.
The drill also trains people. At 3 a.m. during a real failure, nobody wants to read instructions for the first time.
Common mistakes
- Restoring over the live database to "test" it.
- Trusting a log that says "backup succeeded" without a restore.
- Keeping restore steps only in someone's head.
- Leaving test restores of private data lying around.
Key takeaways
- A backup is proved only by a successful restore.
- Restore into a separate place; never over the live data.
- Check with the database's own check and with row comparisons.
- Practise on a schedule and record how long it takes.
Practice
1. Why is a "backup succeeded" message not enough?
Show answer
The file may be empty, damaged or incomplete; only a restore shows whether it works.
2. Where should you restore a test copy?
Show answer
Into a separate empty database, never over the live one.
3. What does PRAGMA integrity_check return for a healthy SQLite database?
Show answer
('ok',).
4. Name two things to compare between a restored copy and the original.
Show answer
For example the row counts and the sums of important columns, or the full table contents for small data.
5. What error did SQLite show for the fake backup?
Show answer
file is not a database.
6. Why write down how long a restore took?
Show answer
It tells you the real recovery time, so you know whether your plan meets what the business needs.