Lesson 5 of 5 · Database Administration Fundamentals

Backup, Recovery, and High Availability

How DBAs protect data and keep it available: RPO and RTO, types of backups, point-in-time recovery, the 3-2-1 rule, high availability vs disaster recovery, replication, and failover.

Beginner 10 sections 10 min read

What can go wrong

Protecting data from loss and keeping it available are the two promises a DBA makes to the business. This lesson covers the key ideas behind both: backups, recovery, high availability, and disaster recovery. These are the concepts you'll hear in your first week on the job.

Databases don't usually fail in dramatic ways, but over the years, all of these happen:

  • Human error. The most common cause of data loss: an UPDATE or DELETE without a WHERE clause, or a table dropped in the wrong environment.
  • Hardware failure. Disks, servers, and network equipment wear out and break.
  • Software bugs and corruption. A bug in an application, the operating system, or the database itself damages data.
  • Cyberattacks. Ransomware encrypts files, including databases and sometimes their backups, and demands payment.
  • Disasters. Fire, flood, power loss, or an outage of a whole data center or cloud region.

Every one of these has a different answer. Backups protect against losing data. High availability keeps the database running through local failures. Disaster recovery brings it back after losing a whole site.

RPO and RTO: how much can you afford to lose?

Before designing anything, a DBA agrees two numbers with the business for each database:

RPO recovery point objective: the data you could lose RTO recovery time objective: how long you're down last good copyof the data disaster back online
RPO (recovery point objective)
The most data the business can afford to lose, measured in time. An RPO of 15 minutes means that after a failure, you must be able to recover everything up to at most 15 minutes before it happened.
RTO (recovery time objective)
The longest the database can be down before the business is seriously hurt. An RTO of 1 hour means it must be back in service within an hour.

An online store might need an RPO of near zero (no lost orders) and an RTO of minutes. An internal reporting database might accept losing a day of data and being down for a day. Lower numbers always cost more, so RPO and RTO are business decisions, and the DBA designs backups and high availability to meet them.

Backups

A backup is a copy of the database, kept somewhere safe, that you can restore if the original is lost or damaged. Backups are described in a few ways:

TypeWhat it copiesTrade-off
FullThe whole databaseSimplest to restore, but the slowest and largest
IncrementalOnly what changed since the last backupFast and small, but a restore needs the full backup plus every incremental since
DifferentialEverything that changed since the last full backupA middle ground: a restore needs the full backup plus only the latest differential
  • Physical vs logical. A physical backup copies the database's files, block by block; in Oracle, this is done with RMAN (Recovery Manager). A logical backup, or export, saves the data and object definitions as SQL-level content, such as with Oracle Data Pump. Physical backups are used to recover whole databases; exports are handy for copying a few tables or moving data between databases.
  • Online vs offline. Online (hot) backups run while users keep working. Offline (cold) backups require shutting the database down, so production databases are almost always backed up online.

A common schedule combines the types, such as a full backup each weekend and an incremental backup every night:

Full incr.incr.incr.incr.incr.incr. SunMonTueWedThuFriSat To restore to Thursday at 2:14 PM: Sunday's full backup + Monday to Thursday's incrementals + archived logs

Point-in-time recovery

Backups alone only take you back to when the backup ran. To get closer, databases keep their archived logs, the history of every change, as covered in the Database Architecture and Storage lesson. Recovery happens in two steps:

  1. Restore: copy the backup files back into place.
  2. Recover: replay the archived logs to bring the database forward, to the latest moment or to any point you choose.

That second step is called point-in-time recovery. If someone drops an important table at 2:15 PM, the DBA can restore last night's backup and replay the logs up to 2:14 PM, losing only the minute in between.

Protecting the backups

A backup stored on the same server as the database dies with it. The widely used 3-2-1 rule says to keep:

  • 3 copies of your data (the original plus two backups),
  • on 2 different types of storage,
  • with 1 copy off-site, such as in another data center or cloud region.

Because ransomware now targets backups too, many companies also keep one immutable copy, which can't be changed or deleted for a set period, even by an administrator. Backups should also be encrypted, since they contain all of the database's data.

High availability and disaster recovery

Backups protect the data, but restoring a large database can take hours. When the business can't wait that long, DBAs use two related approaches:

High availability (HA)Disaster recovery (DR)
Protects againstLocal failures: a server, disk, or database instance going downLosing a whole site: a data center or cloud region
HowRedundant copies close by, with automatic failoverA copy far away, in another region
Typical recovery timeSeconds to minutesMinutes to hours
GoalUsers barely noticeThe business survives

The "nines"

Availability is usually stated as a percentage of time a system is up, often described by its number of nines. Each extra nine is much harder, and more expensive, to achieve:

AvailabilityCalledDowntime allowed per year
99%Two ninesAbout 3.65 days
99.9%Three ninesAbout 8.8 hours
99.99%Four ninesAbout 53 minutes
99.999%Five ninesAbout 5 minutes

Replication and failover

The main tool for both HA and DR is replication: keeping a second copy of the database, called a standby or replica, continuously up to date. The primary database ships its changes, its redo, to the standby, which applies them:

MAIN DATA CENTER ANOTHER SITE OR REGION Applications after failover Primary Standby changes shipped continuously (synchronous or asynchronous)
Synchronous replication
A commit on the primary waits until the standby has the change too, so no committed data is lost if the primary fails (an RPO of zero). The cost is a small delay on every commit, which grows with the distance between sites.
Asynchronous replication
The primary commits without waiting, and the standby catches up a moment later. Commits stay fast, even across long distances, but the last few seconds of changes can be lost in a failure.
Failover
When the primary fails, the standby takes over as the new primary, often automatically. Applications then reconnect to it.
Switchover
A planned, controlled swap of roles between primary and standby, with no data loss, used for maintenance or to test the setup.
Clustering
Several servers running one database together, so if one server fails, the others keep serving users. Oracle Real Application Clusters (RAC) works this way.

Every major platform has its own tools for this:

PlatformReplication and failover
OracleData Guard (standby databases); Real Application Clusters (RAC) for clustering
SQL ServerAlways On availability groups; failover cluster instances
PostgreSQLStreaming replication, often managed by tools such as Patroni
MySQLReplication; InnoDB Cluster
Cloud managed databasesBuilt-in options, such as Amazon RDS Multi-AZ deployments, which keep a standby in a second availability zone

Choosing a disaster recovery strategy

The right strategy depends on the RPO and RTO the business needs, and how much it's willing to pay:

StrategyHow it worksTypical RPO and RTOCost
Backup and restoreBackups copied to another region, restored onto new servers after a disasterHours to a dayLow
Standby databaseA replica kept up to date in another region, ready to take overSeconds to minutesMedium to high
Active-activeTwo or more sites serving users at the same timeNear zeroHighest, and the most complex

In practice: Friday, 4:05 p.m.

A support engineer runs a cleanup script against production. It contains DELETE FROM orders with no WHERE clause, and it commits. Every order is gone. Here's how Kofi handles it:

  1. Stop the damage and get the facts. What exactly ran, against which database, and at what time: 4:05 p.m. New orders are still arriving, so simply rolling the whole database back would lose them.
  2. Choose the least disruptive fix. The delete was only minutes ago, so Oracle's undo still holds the old rows. Kofi queries the table as it was at 4:04 p.m. with a flashback query, and copies the missing rows back, without touching anything else.
  3. Know the fallback. Had the mistake been found days later, after the undo was gone, Kofi would restore last night's backup to a separate server, recover it to 4:04 p.m. with the archived logs, and copy just the orders back. Rolling back all of production would wipe out every other change made since.
  4. Prevent the next one. Afterward, the team removes direct delete access from the support account and requires scripts to be reviewed and tested first.

Point-in-time recovery, archived logs, and tested backups are what make each of those options possible.

The DBA's part

Backup and recovery is one of the few areas where the DBA is often the last line of defense. When everything else has failed, the business asks one question: can we get the data back? A good DBA makes sure the answer is yes:

  1. Agree RPO and RTO with the business for every important database.
  2. Automate backups, so they never depend on someone remembering.
  3. Monitor every backup, and get alerted when one fails.
  4. Test restores regularly, and measure how long they take.
  5. Write a runbook: step-by-step instructions anyone on the team can follow during a crisis.
  6. Practice failovers and DR drills, so the first time isn't during a real disaster.
  7. Protect the backups: off-site, encrypted, and with at least one copy that can't be deleted.

Key takeaways

  • Data is lost through human error, hardware failure, corruption, cyberattacks, and disasters. Human error is the most common.
  • RPO is how much data you can afford to lose; RTO is how long you can afford to be down. The business sets them, and the DBA designs to meet them.
  • Full, incremental, and differential backups, plus archived logs, make point-in-time recovery possible.
  • Follow the 3-2-1 rule, keep an immutable copy, and test restores: an untested backup is only a hope.
  • High availability survives local failures in seconds or minutes; disaster recovery survives losing a whole site.
  • Replication keeps a standby up to date for failover, but it isn't a backup, since mistakes are replicated too.