Level: Hard — A core DBA responsibility; choosing the wrong backup method can mean hours of downtime during a restore.
Scenario: You must set up backups for a 500 GB production database. Using mysqldump takes 6 hours to back up and even longer to restore, locking things up. The interviewer asks: "What is the difference between logical and physical backups, and which would you use for a large, busy database?"
| Aspect | mysqldump (Logical) | XtraBackup (Physical) |
|---|---|---|
| What it copies | SQL statements | Raw data files |
| Speed (large DB) | Slow | Fast |
| Restore speed | Slow (re-runs SQL) | Fast (file copy) |
| Locking impact | Can lock (without care) | Hot / non-blocking |
| Portability | High (any version/engine) | Lower (version-specific) |
| Selective restore | Easy (single table) | Harder (whole instance) |
| Best for | Small DBs, migrations | Large, busy production DBs |
The rule of thumb: Use mysqldump for small databases, migrations, or when you need portability and single-table restores. Use a physical backup (XtraBackup / MySQL Enterprise Backup / clone) for large production databases where speed and low locking matter.
# Consistent backup of one database WITHOUT locking (InnoDB)
mysqldump --single-transaction --routines --triggers --events \
-u root -p shop > shop_backup.sql
# --single-transaction: consistent snapshot via one transaction (no table locks)
# --routines/--triggers/--events: include stored programs
# Backup all databases + binlog position (for point-in-time recovery)
mysqldump --single-transaction --master-data=2 --all-databases \
-u root -p > full_backup.sql
# Restore = feed the SQL back to the server
mysql -u root -p shop < shop_backup.sql
Critical flag — --single-transaction: Without it, mysqldump can lock tables and block writes for the whole dump. With it (on InnoDB), it takes a single consistent snapshot using a transaction, so the database keeps serving traffic. Always use it for InnoDB.
# Take a hot physical backup (server keeps running, no locking of InnoDB)
xtrabackup --backup --target-dir=/backups/full \
--user=root --password=secret
# Prepare it (apply redo logs to make it consistent)
xtrabackup --prepare --target-dir=/backups/full
# Restore: stop MySQL, copy files back, fix permissions, start
xtrabackup --copy-back --target-dir=/backups/full
# chown -R mysql:mysql /var/lib/mysql && start mysqld
Interviewer follow-up: "A backup is useless if you have never restored it — how do you ensure yours works?" → Regularly test restores on a separate server (automated restore drills). Verify row counts and checksums, measure the actual restore time against your RTO (Recovery Time Objective), and store backups off-site/encrypted. An untested backup is a hope, not a strategy.