Home / Databases / MySQL

DatabasesRunbookAdvanced

MySQL physical backups with Percona XtraBackup: full and incremental backups, prepare, restore and verify

Take hot full and incremental MySQL backups with Percona XtraBackup, prepare the chain in the right order, restore to an empty data directory and roll forward with binlogs.

Deepak Bhardwaj6 min read

Applies toMySQL 8.0 and 8.4 (and Percona Server) with Percona XtraBackup 8.0 / 8.4 on Linux; not for managed services such as RDS or Azure Flexible Server

When to use this

mysqldump becomes too slow at around a few hundred GB. The dump takes hours and the restore takes longer, because every index is rebuilt. Percona XtraBackup copies the InnoDB files while the server keeps running and records the redo generated during the copy. The restore is then a file copy. This runbook covers a weekly full backup with daily incrementals, the prepare step that people get wrong, and a tested restore.

Caution

The XtraBackup major version must match the server: 8.0 for MySQL 8.0 and 8.4 for MySQL 8.4. Its minor version should be the same as the server's or newer. Older builds refuse to back up a newer server, or produce backups they can't prepare. Upgrade XtraBackup before you upgrade MySQL.

Step 1: create the backup user

CREATE USER 'bkpuser'@'localhost' IDENTIFIED BY '<strong-password>';
GRANT BACKUP_ADMIN, PROCESS, RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'bkpuser'@'localhost';
GRANT SELECT ON performance_schema.log_status TO 'bkpuser'@'localhost';
GRANT SELECT ON performance_schema.keyring_component_status TO 'bkpuser'@'localhost';
GRANT SELECT ON performance_schema.replication_group_members TO 'bkpuser'@'localhost';

The full list of required privileges changes between releases, so check the "connection and privileges" page for your XtraBackup version. Store the credentials in a client option file readable only by the backup user, not on the command line:

# /etc/mysql/xtrabackup.cnf  (chmod 600)
[xtrabackup]
user=bkpuser
password=<strong-password>

Step 2: take the full backup

BK=/backups/mysql/$(date +%F)
xtrabackup --defaults-extra-file=/etc/mysql/xtrabackup.cnf \
  --backup --parallel=4 --target-dir=$BK/full 2>&1 | tee $BK-full.log

tail -1 $BK-full.log          # must end with "completed OK!"
cat $BK/full/xtrabackup_checkpoints

xtrabackup_checkpoints shows backup_type = full-backuped and the from_lsn/to_lsn range. xtrabackup_binlog_info records the binlog file, position and GTID set at the point the backup is consistent. You need that for point-in-time recovery.

Warning

By default XtraBackup takes a backup lock (LOCK INSTANCE FOR BACKUP) that blocks DDL for the whole copy. An ALTER TABLE or a migration scheduled during the backup window will wait, and the application queries queued behind it wait too. Move schema changes out of the backup window. Recent 8.0 and 8.4 releases offer a reduced-locking --lock-ddl mode; check which values your build supports.

Step 3: take incremental backups

Each incremental copies only the pages changed since the LSN of its base. Chain each one to the previous backup:

# Monday: based on the full backup
xtrabackup --defaults-extra-file=/etc/mysql/xtrabackup.cnf --backup --parallel=4 \
  --target-dir=$BK/inc1 --incremental-basedir=$BK/full

# Tuesday: based on Monday's incremental
xtrabackup --defaults-extra-file=/etc/mysql/xtrabackup.cnf --backup --parallel=4 \
  --target-dir=$BK/inc2 --incremental-basedir=$BK/inc1

Check that the chain is continuous. Each incremental's from_lsn must equal its base's to_lsn:

grep -H -E 'backup_type|from_lsn|to_lsn' $BK/*/xtrabackup_checkpoints

Compressed and streamed backups

# Compressed (zstd is the default algorithm from 8.0.34)
xtrabackup --defaults-extra-file=/etc/mysql/xtrabackup.cnf --backup \
  --compress=zstd --compress-threads=4 --target-dir=$BK/full

# Stream straight to another host instead of writing locally
xtrabackup --defaults-extra-file=/etc/mysql/xtrabackup.cnf --backup --stream=xbstream \
  --target-dir=/tmp | ssh backup01 "cat > /backups/orders-$(date +%F).xbstream"

On restore, unpack a stream with xbstream -x -C <dir> < file.xbstream. Decompress with xtrabackup --decompress --target-dir=<dir> before you prepare; the zstd binary must be installed. Add --remove-original to delete the .zst files afterwards.

Step 4: prepare the chain

A raw backup is not consistent. The prepare step applies the redo log, and for incrementals the order and flags matter.

Step Command --apply-log-only
Base full xtrabackup --prepare --apply-log-only --target-dir=full Yes
Each middle incremental xtrabackup --prepare --apply-log-only --target-dir=full --incremental-dir=incN Yes
Last incremental xtrabackup --prepare --target-dir=full --incremental-dir=incLast No
Full backup with no incrementals xtrabackup --prepare --target-dir=full No

Work on a copy, never on your only set of backups:

R=/restore/orders && mkdir -p $R && cp -a $BK/full $BK/inc1 $BK/inc2 $R/

xtrabackup --prepare --apply-log-only --target-dir=$R/full
xtrabackup --prepare --apply-log-only --target-dir=$R/full --incremental-dir=$R/inc1
xtrabackup --prepare --target-dir=$R/full --incremental-dir=$R/inc2

grep backup_type $R/full/xtrabackup_checkpoints   # full-prepared

Caution

If you prepare the base without --apply-log-only, uncommitted transactions are rolled back. After that, no incremental can be applied to it, and you must start again from an untouched copy. Don't apply the same incremental twice, and apply them strictly in order.

Step 5: restore

systemctl stop mysqld                         # service name varies: mysql / mysqld
mv /var/lib/mysql /var/lib/mysql.old.$(date +%s)
mkdir /var/lib/mysql

xtrabackup --copy-back --target-dir=$R/full   # reads datadir from my.cnf
chown -R mysql:mysql /var/lib/mysql
systemctl start mysqld

Use --move-back instead of --copy-back if you don't have room for a second copy. It moves the files, so the prepared backup is gone afterwards.

Troubleshooting the restore

Symptom Cause Fix
Original data directory ... is not empty! The datadir still has files in it Move the old datadir aside; the target must be empty
mysqld fails with permission errors The files are owned by root after the copy chown -R mysql:mysql on the datadir (and on any separate redo/undo directories)
Server starts, InnoDB complains about redo or undo innodb_* paths or sizes in my.cnf differ from the source Use the source's backup-my.cnf (in the backup directory) as the reference
Can't open table for encrypted tables The keyring wasn't available to prepare or start Restore the keyring file/component configuration, then prepare again from a clean copy
Applying an incremental fails after the base was prepared (backup_type = full-prepared) --prepare was run without --apply-log-only before the incrementals were applied Start again from an unprepared copy

Step 6: roll forward to a point in time (optional)

The backup is consistent at the position in xtrabackup_binlog_info. To recover up to just before a bad change, replay the binlogs from that position:

cat $R/full/xtrabackup_binlog_info
# binlog.000412   1934502   3e11fa47-...:1-88123412

mysqlbinlog --start-position=1934502 --stop-datetime="2026-11-16 09:41:00" \
  /binlogs/binlog.000412 /binlogs/binlog.000413 | mysql -u root -p

With GTIDs on, read the GTID set from the same file and check the replay carefully. The MySQL point-in-time recovery guide covers stopping at the exact event. To rebuild a replica from the restored server, compare this approach with the CLONE plugin.

Step 7: verify

SELECT @@version, @@gtid_executed;
SELECT table_schema, COUNT(*) FROM information_schema.tables
WHERE table_schema NOT IN ('mysql','sys','information_schema','performance_schema')
GROUP BY table_schema;
CHECK TABLE orders.order_lines;     -- spot-check the big, busy tables

Compare row counts of a few key tables with the source, and run one application smoke query.

Prevent it next time

  • Restore the latest chain to a scratch host every week, automatically, and alert if any step doesn't end in completed OK!.
  • Back up the binlogs on their own schedule (for example mysqlbinlog --read-from-remote-server --raw --stop-never). Without them, the backup only takes you back to the time it was taken.
  • Keep XtraBackup upgrades in the same change ticket as MySQL minor upgrades.
  • Limit incremental chains to about a week. A long chain means a long prepare and more pieces that can break.
  • Copy backups off the host and keep an immutable copy. A backup that lives next to the datadir dies with it.