Hardware fails, humans run DELETE without a WHERE, and ransomware exists. A tested backup-and-recovery plan is what turns a disaster into an inconvenience. This chapter covers backing up, restoring, and keeping a live copy with replication.
A backup you haven't restored isn't a backup
Logical backups (mysqldump)
mysqldump writes the database as a script of SQL statements — portable and human-readable, ideal for small-to-mid databases:
# Back up one database (single transaction = consistent snapshot on InnoDB)mysqldump --single-transaction -u root -p albero > albero_backup.sql# Restore it into a (possibly new) databasemysql -u root -p albero < albero_backup.sql
For very large databases, physical tools (Percona XtraBackup) copy the data files directly and restore far faster than replaying SQL.
Binary logs & point-in-time recovery
The binary log records every change. With a nightly dump plus the binlog, you can restore to a specific moment — right before that bad DELETE:
# 1) Restore last night's dump, then 2) replay the binlog up to just before the mistakemysqlbinlog --stop-datetime="2026-07-07 09:14:00" mysql-bin.000042 | mysql -u root -p
Replication
Replication streams changes from a source server to one or more replicas. It gives you read scaling (send reads to replicas), a warm standby for failover, and a place to run heavy analytics without touching production:
- The source writes changes to its binary log; each replica replays them.
- Replicas are typically read-only and lag the source slightly (asynchronous by default).
- On failure you promote a replica to become the new source.
Practice
Take a mysqldump --single-transaction backup and restore it into a fresh database, then confirm the row counts match. Describe, in your own words, how a nightly dump plus binary logs enables point-in-time recovery, and one reason you'd add a read replica.
Key takeaways
- 01The only real backup is one you have restored and verified — schedule test restores.
- 02mysqldump --single-transaction makes a consistent logical backup; restore by piping it back into mysql.
- 03Physical backups (XtraBackup) restore large databases far faster than replaying SQL.
- 04A nightly dump plus binary logs enables point-in-time recovery to just before a mistake.
- 05Replication streams changes to replicas for read scaling, failover and offloading analytics.
