MySQL Backup and Restore: mysqldump, Binary Logs and Point-in-Time Recovery

MySQL 60 min min read Updated: Nov 27, 2026 Intermediate
MySQL Backup and Restore: mysqldump, Binary Logs and Point-in-Time Recovery
Intermediate Topic 2 of 3

Why Backups Matter

Hardware fails, people run DROP TABLE by mistake, and attackers encrypt data. A backup you have never restored is only a hope. Plan three things: how often you back up, where copies are stored (not only on the same server), and how fast you can restore.

Logical vs Physical Backups

Logical (mysqldump, mysqlpump, MySQL Shell dump)Physical (file copy: Percona XtraBackup, MySQL Enterprise Backup, snapshots)
What it savesSQL statementsThe data files
Speed on big dataSlower, especially to restoreMuch faster
Portable between versionsYesUsually same major version
Good forSmall and medium databases, migrationsLarge production databases

Taking a Dump

This ran on a real MySQL 8.0 server (mysqldump 8.0). The command and the main part of its output for a tiny database:

bash mysqldump -u root -p --single-transaction --routines --triggers --events shopdemo > shopdemo.sql
text DROP TABLE IF EXISTS `customers`; CREATE TABLE `customers` ( `id` int NOT NULL, `name` varchar(20) DEFAULT NULL, `email` varchar(30) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; INSERT INTO `customers` VALUES (1,'Asha','a@x.com'); DROP TABLE IF EXISTS `orders`; CREATE TABLE `orders` ( `id` int NOT NULL, `total` int DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; INSERT INTO `orders` VALUES (1,500); restored customers: 1

The lines starting with /*! and -- (version comments and timestamps) were removed here to keep it short. Notice the file contains DROP TABLE IF EXISTS, CREATE TABLE and INSERT statements: it is a plain SQL script.

Important Options

OptionWhy
--single-transactionConsistent snapshot of InnoDB tables without locking them
--routines --triggers --eventsInclude stored programs (triggers are included by default, routines and events are not)
--databases db1 db2 / --all-databasesInclude CREATE DATABASE
--no-dataStructure only
--set-gtid-purged=OFFAvoid GTID statements when restoring to a different server
--hex-blobSafe dumping of binary data
--source-data=2 (8.0.26+)Write the binary log position in a comment, for point-in-time recovery

Restoring

bash mysql -u root -p -e "CREATE DATABASE shopdemo_restored" mysql -u root -p shopdemo_restored < shopdemo.sql # compressed mysqldump ... | gzip > shopdemo.sql.gz gunzip < shopdemo.sql.gz | mysql -u root -p shopdemo_restored

The script above was also run: the dump was loaded into a new database and a row count was compared. The real result was 1 customer restored, matching the source. Always verify a restore like this, with row counts or checksums (CHECKSUM TABLE t).

sql CREATE TABLE t (id INT PRIMARY KEY, v VARCHAR(10)); INSERT INTO t VALUES (1, 'a'), (2, 'b'); SELECT COUNT(*) AS row_count, SUM(id) AS id_sum FROM t;

Result:

row_countid_sum
23

Binary Logs and Point-in-Time Recovery

A nightly dump alone loses everything since the last dump. The binary log records every change. Restore the last backup, then replay the log up to just before the mistake:

bash # 1. restore the last full backup mysql -u root -p < full_backup.sql # 2. replay binary logs from the backup position up to the moment before the bad statement mysqlbinlog --start-position=157 --stop-datetime="2025-07-01 13:59:59" binlog.000012 | mysql -u root -p

These commands need server log files and a real incident, so they are shown, not run. Check that logging is on:

sql SELECT @@log_bin AS binary_log_enabled, @@binlog_format AS format, @@binlog_expire_logs_seconds AS keep_seconds;

Result:

binary_log_enabledformatkeep_seconds
1ROW2592000
Binary logging is on by default in MySQL 8 It was off by default in 5.7. Binary logs are also what replication uses.

A Practical Backup Plan

  1. Nightly logical or physical backup, kept for at least 7-30 days.
  2. Binary logs retained at least until the next full backup, copied off the server.
  3. Copies in a second location (another region or storage account).
  4. Backups encrypted, with limited access.
  5. A restore test every month into a scratch server, with a written checklist.
Managed services Cloud MySQL services (RDS, Cloud SQL, Azure Database) provide automatic backups and point-in-time restore, but you still need to test restores and set the retention period yourself.

Common Beginner Mistakes

MistakeWhat happensFix
Never testing a restoreBackup is unusable when neededScheduled restore tests
Backing up without --single-transactionInconsistent data or table locksAdd it for InnoDB
Forgetting routines and eventsMissing stored programs--routines --events
Backups stored only on the same diskLost together with the serverOff-server copies
Passwords on the command lineVisible in the process listUse -p prompt or a protected option file
Using mysqldump for multi-terabyte dataTakes days to restorePhysical backup tools

Practice

  1. Dump a small database and restore it to a new name.
  2. Compare row counts of both.
  3. Dump only the structure with --no-data.
  4. Write down your recovery steps for "someone dropped a table at 14:03".

Conclusion

Use mysqldump --single-transaction --routines --triggers --events for small to medium databases, keep binary logs for point-in-time recovery, copy backups off the server and test restoring them regularly.

Quick Summary: mysqldump, restore, binary log, point-in-time recovery, backup strategy.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators