Database Backups and Security · Lesson 57 of 112

Database Why Backups Matter

What you will learn

  • The common ways data is lost
  • Why a backup is a separate copy you can go back to
  • The idea of a recovery point and a recovery time
  • A simple backup rule: more than one copy, in more than one place
  • A first recovery with SQLite

1. How data gets lost

A database that works today can lose data tomorrow:

Cause Example
Human mistake someone runs DELETE FROM orders and forgets the WHERE
Software bug a new version of the program overwrites the wrong rows
Hardware failure the disk stops working
Attack someone breaks in and deletes or locks the data
Disaster fire, flood or theft takes the machine

Transactions (chapter 6) protect you from a failed operation. They do not protect you from a successful but wrong one: once COMMIT is done, the delete is permanent. Only a copy made earlier can bring the data back.

2. What a backup is

A backup is a copy of the data, taken at a known time, kept separately from the original, so that you can restore the database to how it was.

Copies that live in the same place as the original are not enough. If the disk fails, both are gone. A widely used rule of thumb is the 3-2-1 rule: keep 3 copies of your data (the live one plus two backups), on 2 different kinds of storage, with 1 copy in a different place (another building or a cloud service). It is a guide, not a guarantee: it does not say how often to back up, or whether the copies work (lesson 8.3). A backup that an attacker or a bug can overwrite along with the original is also not safe, so keep at least one copy that the running system cannot change.

Two numbers describe how good a backup plan is:

  • Recovery point: how much recent work you can afford to lose. A backup taken every night means you may lose up to a day.
  • Recovery time: how long it takes to get running again. Restoring a huge file over a slow link takes longer than restoring a small one.
Quick check: a shop copies its database file to another folder on the same computer every night. What failure is it still not protected from?

Hardware failure, theft or fire. The copy is on the same machine, so it disappears with the original.

3. A first recovery

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 to copy, so a copy of a table made with CREATE TABLE ... AS SELECT stands in for the backup file. Nothing leaves your browser; Reset data puts the original back.

We keep a copy of the reviews table, then make a mistake.

We keep a copy of the audit_log table, then make a mistake.

We keep a copy of the attendance table, then make a mistake.

CREATE TABLE reviews_backup AS SELECT * FROM reviews;
SELECT (SELECT COUNT(*) FROM reviews) AS live_rows, (SELECT COUNT(*) FROM reviews_backup) AS backup_rows;
live_rows  backup_rows
---------  -----------
      585          585
(1 row)

Now the mistake: a delete with no WHERE.

DELETE FROM reviews;
SELECT (SELECT COUNT(*) FROM reviews) AS live_rows, (SELECT COUNT(*) FROM reviews_backup) AS backup_rows;
live_rows  backup_rows
---------  -----------
        0          585
(1 row)
CREATE TABLE audit_log_backup AS SELECT * FROM audit_log;
SELECT (SELECT COUNT(*) FROM audit_log) AS live_rows, (SELECT COUNT(*) FROM audit_log_backup) AS backup_rows;
live_rows  backup_rows
---------  -----------
      120          120
(1 row)

Now the mistake: a delete with no WHERE.

DELETE FROM audit_log;
SELECT (SELECT COUNT(*) FROM audit_log) AS live_rows, (SELECT COUNT(*) FROM audit_log_backup) AS backup_rows;
live_rows  backup_rows
---------  -----------
        0          120
(1 row)
CREATE TABLE attendance_backup AS SELECT * FROM attendance;
SELECT (SELECT COUNT(*) FROM attendance) AS live_rows, (SELECT COUNT(*) FROM attendance_backup) AS backup_rows;
live_rows  backup_rows
---------  -----------
     6012         6012
(1 row)

Now the mistake: a delete with no WHERE.

DELETE FROM attendance;
SELECT (SELECT COUNT(*) FROM attendance) AS live_rows, (SELECT COUNT(*) FROM attendance_backup) AS backup_rows;
live_rows  backup_rows
---------  -----------
        0         6012
(1 row)

The live table is empty, but the copy still has every row. Restoring means putting the copy back:

INSERT INTO reviews SELECT * FROM reviews_backup;
SELECT (SELECT COUNT(*) FROM reviews) AS live_rows, (SELECT COUNT(*) FROM reviews_backup) AS backup_rows;
live_rows  backup_rows
---------  -----------
      585          585
(1 row)
INSERT INTO audit_log SELECT * FROM audit_log_backup;
SELECT (SELECT COUNT(*) FROM audit_log) AS live_rows, (SELECT COUNT(*) FROM audit_log_backup) AS backup_rows;
live_rows  backup_rows
---------  -----------
      120          120
(1 row)
INSERT INTO attendance SELECT * FROM attendance_backup;
SELECT (SELECT COUNT(*) FROM attendance) AS live_rows, (SELECT COUNT(*) FROM attendance_backup) AS backup_rows;
live_rows  backup_rows
---------  -----------
     6012         6012
(1 row)

With a real database file the backup is a copy of the file (shutil.copy in Python) and restoring means putting the copy back in place. Copying a file is fine when nobody is using the database at that moment. Copying a busy database file can give a broken copy, which is why real backups use proper tools (lesson 8.2). A copy kept in the same database, like the one above, is no protection against a failed disk: it is only a safety net against a mistaken statement.

Worked example: choosing a backup plan

A small online shop (made up) takes about 40 orders a day. The owner decides losing more than a few hours of orders is not acceptable, and being offline for more than a day would lose customers.

Question Decision
Recovery point backup every 4 hours, so at most 4 hours of orders are lost
Recovery time keep the newest backup on a fast disk, so a restore takes minutes
Places one copy on the server, one on another machine, one copy off-site
Check every month, restore one backup somewhere else to prove it works (lesson 8.3)

Common mistakes

  • No backup at all ("it has never failed").
  • Keeping the only backup on the same disk as the database.
  • Never trying to restore. A backup you have not tested may not work (lesson 8.3).
  • Thinking replication or a transaction log is a backup. A mistake is copied to the replica too.

Key takeaways

  • Data is lost by mistakes, bugs, hardware failure, attacks and disasters.
  • A committed mistake can only be undone from an earlier copy.
  • A backup is a separate copy; keep several, in different places.
  • Plan the recovery point (data you can lose) and recovery time (how fast you return).

Practice

1. Name three ways data can be lost.

Show answer

For example human mistakes, hardware failure and attacks (also bugs and disasters).

2. Why does ROLLBACK not help after a COMMIT of a wrong DELETE?

Show answer

ROLLBACK only undoes work in a transaction that is still open. After COMMIT the change is permanent; only an earlier backup can bring the data back.

3. What is a recovery point?

Show answer

How much recent data you can afford to lose, set by how often you take backups.

4. Why is a backup on the same disk risky?

Show answer

If the disk fails, is stolen or burns, the original and the backup are lost together.

5. A backup is taken every night at midnight and the database is damaged at 6 pm. About how much work can be lost?

Show answer

About 18 hours of changes, everything since midnight.

6. Give one rule for where copies should be kept.

Show answer

Keep more than one copy, on different devices, with at least one in a different place.