Skip to content

mysqldump backups and restores that survive a real failure

A production-grade mysqldump pattern — consistent backups for InnoDB, restore drill, retention, and the limits that push you toward physical backups.

mysqldump produces a logical SQL backup — every CREATE and INSERT needed to reproduce the database elsewhere. It is the right tool for small-to-medium databases (under roughly 100 GB) and for cross-version restores. This article covers the dump invocation that yields a consistent snapshot, the restore drill that proves the backup works, and the size at which you need to move to physical backups (Percona XtraBackup, MySQL Enterprise Backup, or filesystem snapshots).

How to verify

Before relying on a backup file, confirm three things — file size, internal consistency, and that it will actually replay.

# Size and modification time of the latest dump
ls -lh /var/backups/mysql/ | tail -5

# First and last 5 lines — should look like CREATE TABLE and a clean COMMIT/UNLOCK
zcat /var/backups/mysql/all-$(date +%F).sql.gz | head -20
zcat /var/backups/mysql/all-$(date +%F).sql.gz | tail -10

# Replay into a sandbox container (Docker) and count the rows
docker run --rm -e MYSQL_ROOT_PASSWORD=test -p 3307:3306 -d --name dump-test mysql:8
zcat /var/backups/mysql/all-$(date +%F).sql.gz | docker exec -i dump-test mysql -uroot -ptest
docker exec dump-test mysql -uroot -ptest -e "SELECT COUNT(*) FROM orders.orders;"
docker stop dump-test

If the restore fails partway through, the dump itself is broken — common causes include disk-full mid-dump, a stored procedure that references a missing function, or a corrupt gzip stream from a network blip.

What’s happening

mysqldump connects as a regular client, runs SHOW CREATE TABLE for every table, then SELECT * FROM table for the rows. By default it locks each table while it dumps — which means writes block during the backup. For InnoDB you almost never want that; you want --single-transaction, which opens a REPEATABLE READ transaction and dumps from a consistent snapshot without locking.

For mixed InnoDB+MyISAM, --single-transaction does not cover the MyISAM tables — those still need --lock-tables or --lock-all-tables. The right answer is to migrate MyISAM tables to InnoDB and then use --single-transaction exclusively.

The dump file is plain SQL. It can be gzipped, encrypted, parsed, and partially restored. The trade-off versus a physical backup is restore speed — a logical restore replays every INSERT, which is slow for large tables (think 100 GB taking many hours). Above roughly 100 GB, physical backups become the right tool.

The procedure

  1. Create a backup user with the minimum required privileges.

    CREATE USER 'backup'@'localhost' IDENTIFIED BY 'strong-password';
    GRANT SELECT, LOCK TABLES, SHOW VIEW, EVENT, TRIGGER, PROCESS, RELOAD ON *.* TO 'backup'@'localhost';
  2. Write the credentials to a protected client config so they are not in shell history.

    # /root/.my.cnf  (chmod 600)
    [client]
    user = backup
    password = strong-password
    host = localhost
  3. Run a consistent, complete dump of everything.

    mysqldump \
      --defaults-file=/root/.my.cnf \
      --all-databases \
      --single-transaction \
      --quick \
      --routines \
      --triggers \
      --events \
      --master-data=2 \
      --set-gtid-purged=OFF \
      --hex-blob \
      | gzip -9 > /var/backups/mysql/all-$(date +%F).sql.gz

    What each flag does:

    • --single-transaction — consistent snapshot for InnoDB without locking.
    • --quick — stream rows directly to output instead of buffering the entire table.
    • --routines --triggers --events — include stored procedures, functions, triggers, and scheduled events. Default behavior excludes them.
    • --master-data=2 — write the binlog file and position as a comment, useful for restore-then-replicate scenarios.
    • --set-gtid-purged=OFF — avoid the GTID statement that breaks restores into a non-GTID server.
    • --hex-blob — encode binary columns as hex so they survive round-trip cleanly.
  4. Pipe through encryption if the backup leaves the host.

    mysqldump ... | gzip -9 | openssl enc -aes-256-cbc -salt -pbkdf2 -pass file:/etc/backup-key \
      > /var/backups/mysql/all-$(date +%F).sql.gz.enc
  5. Schedule via systemd timer (preferred over cron for the logging integration).

    # /etc/systemd/system/mysql-backup.service
    [Unit]
    Description=MySQL nightly logical backup
    
    [Service]
    Type=oneshot
    ExecStart=/usr/local/sbin/mysql-backup.sh
    # /etc/systemd/system/mysql-backup.timer
    [Unit]
    Description=Nightly MySQL backup
    
    [Timer]
    OnCalendar=*-*-* 02:30:00
    Persistent=true
    
    [Install]
    WantedBy=timers.target
    sudo systemctl enable --now mysql-backup.timer
  6. Ship offsite immediately after the dump completes. A backup on the same host is not a backup — it is a convenience copy. Use rclone, restic, or the cloud provider’s CLI to push to object storage in a different region:

    rclone copy /var/backups/mysql/ remote:my-backups/mysql/ --include "*.sql.gz"
  7. Restore drill — at least monthly. Restore the latest dump into a sandbox host and run a row count or checksum against production for the largest tables. If you have not restored in 30 days, you do not have a backup — you have an untested file.

    # Sandbox restore
    gunzip < all-$(date +%F).sql.gz | mysql -h sandbox.internal -u root -p
  8. Retention. Daily for 14 days, weekly for 8 weeks, monthly for 12 months — a common pattern. Encode it in the backup script or use object-storage lifecycle rules. Always retain at least one off-region copy at all times.

Common pitfalls

  • A dump with no --single-transaction on a busy InnoDB table holds locks long enough to stall the application — you will see write timeouts during the backup window.
  • --single-transaction combined with LOCK TABLES (some pre-built scripts include both) cancels the transaction and you end up with an inconsistent dump anyway.
  • Without --routines --triggers, a restore produces a database with empty stored procedure definitions — applications relying on them fail with cryptic errors.
  • A dump that exceeds disk space mid-write produces a truncated file that looks fine until you try to restore it. Always verify the last lines contain -- Dump completed.
  • For tables larger than memory, the restore replays one INSERT at a time and can take many hours — plan the cutover window accordingly. For databases over 100 GB, use Percona XtraBackup for physical snapshots instead.

In the engagements we run, the MySQL backup pipeline is part of the platform standard — systemd timer, encrypted upload to object storage in a second region, weekly restore drill into a disposable VM, and a Prometheus exporter that pages if the latest dump is older than 26 hours. We carry that under Managed Operations so clients do not learn about the broken backup the day they need it.