When I first started out, I thought backing up MySQL was as simple as running mysqldump and stashing away the .sql file. That worked fine until the project grew. Once the users table crossed 10 million rows, the dump file ballooned to 45 GB. Every dump spiked CPU usage to 90%, and a test restore took nearly 3 hours.
As your data grows, you need a hot backup solution that runs while the system is live. Percona XtraBackup solves this cleanly without interrupting your application.
Quick start: Hot backup and restore MySQL in 5 minutes
Before diving into the theory, let’s set up a complete backup and restore cycle on Ubuntu with MySQL 8.0.
Step 1: Install Percona XtraBackup 8.0
On the server running MySQL 8.0, install the XtraBackup package from Percona’s official repository:
wget https://repo.percona.com/apt/percona-release_latest.generic_all.deb
sudo dpkg -i percona-release_latest.generic_all.deb
sudo percona-release enable-only tools release
sudo apt-get update
sudo apt-get install -y percona-xtrabackup-80
Once installed, check the version to make sure the package is ready:
xtrabackup --version
Step 2: Create a dedicated backup user
Avoid using the root account for automated tasks. Instead, create a dedicated MySQL user with only the required privileges:
mysql -u root -p -e "
CREATE USER 'bkpuser'@'localhost' IDENTIFIED BY 'StrongPassword@2026';
GRANT BACKUP_ADMIN, PROCESS, RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'bkpuser'@'localhost';
GRANT SELECT ON performance_schema.log_status TO 'bkpuser'@'localhost';
FLUSH PRIVILEGES;
"
Step 3: Run a Full Backup
Create the target directory and run a hot backup of the entire database:
sudo mkdir -p /var/backups/mysql/full
sudo xtrabackup --backup --user=bkpuser --password='StrongPassword@2026' --target-dir=/var/backups/mysql/full
When you see completed OK! on the screen, it’s done. All read and write queries on InnoDB tables continue running normally throughout this process.
Step 4: Prepare the backup
A freshly copied backup cannot be used to start MySQL right away because it still contains in-flight transactions. You need to run --prepare to bring the data to a consistent state:
sudo xtrabackup --prepare --target-dir=/var/backups/mysql/full
Step 5: Restore the data
Suppose the database runs into an issue and you need to restore from /var/backups/mysql/full. Follow these steps in order:
sudo systemctl stop mysql
sudo rm -rf /var/lib/mysql/*
sudo xtrabackup --copy-back --target-dir=/var/backups/mysql/full
sudo chown -R mysql:mysql /var/lib/mysql
sudo systemctl start mysql
After this step, log back into MySQL to verify. Your data is now restored to the exact point in time when the backup ran.
How it works: Why doesn’t XtraBackup lock tables?
To operate confidently in production, you only need to understand one key concept: the difference between logical backups and physical backups.
- Logical backup (
mysqldump): Reads data row by row and exportsCREATE TABLEandINSERT INTOstatements. During a restore, MySQL has to replay every statement and rebuild all indexes, which is very slow for large tables. - Physical backup (Percona XtraBackup): Copies the InnoDB data files (
.ibd,ibdata1) directly on disk. Its speed is close to regular file copying, while still ensuring data integrity.
InnoDB’s lock-free mechanism
When xtrabackup --backup runs, the tool does two things in parallel:
- Starts a background thread that continuously monitors and copies MySQL’s Redo Log based on the LSN (Log Sequence Number).
- Copies all InnoDB data files to the target directory. Meanwhile, the application keeps reading and writing data as usual.
Copying files can take tens of minutes, meaning the .ibd files will be out of sync with each other in time. However, the Redo Log captures every change made during that window. When you run --prepare, XtraBackup applies committed transactions and rolls back uncommitted ones. The result is a clean, fully synchronized data directory.
One quick note: if your database still has tables using a non-transactional storage engine like MyISAM, XtraBackup will place a brief read lock (FLUSH TABLES WITH READ LOCK) for a few seconds at the end of the process. On a pure InnoDB setup, write operations are virtually unaffected.
Combining Full Backups with Incremental Backups
Once your database hits 300 GB, running a Full Backup every day becomes wasteful. Keeping 7 copies a week eats up over 2 TB of disk space and puts constant I/O pressure on your drives. The standard strategy is: run a Full Backup on Sunday, and only Incremental Backups from Monday through Saturday. Each Incremental Backup only stores data pages that changed since the previous day, typically taking just a few GBs and finishing in 2–3 minutes.
Step 1: Create the base Full Backup (Sunday)
sudo mkdir -p /var/backups/mysql/base
sudo xtrabackup --backup --user=bkpuser --password='StrongPassword@2026' --target-dir=/var/backups/mysql/base
Step 2: Create the 1st Incremental Backup (Monday)
Point the --incremental-basedir flag to the base Full Backup directory:
sudo mkdir -p /var/backups/mysql/inc1
sudo xtrabackup --backup --user=bkpuser --password='StrongPassword@2026' \
--target-dir=/var/backups/mysql/inc1 \
--incremental-basedir=/var/backups/mysql/base
Step 3: Create the 2nd Incremental Backup (Tuesday)
This time, --incremental-basedir must point to the previous day’s inc1 directory to continue the chain:
sudo mkdir -p /var/backups/mysql/inc2
sudo xtrabackup --backup --user=bkpuser --password='StrongPassword@2026' \
--target-dir=/var/backups/mysql/inc2 \
--incremental-basedir=/var/backups/mysql/inc1
Step 4: Prepare when restoring an Incremental chain
This is where many people slip up. When merging Incremental Backups into the Full Backup, you must include the --apply-log-only flag for the Full Backup and every intermediate Incremental Backup. If you omit this flag, uncommitted transactions get rolled back prematurely, and you won’t be able to merge inc1 or inc2 afterward.
# 1. Prepare the base Full Backup (WITH --apply-log-only)
sudo xtrabackup --prepare --apply-log-only --target-dir=/var/backups/mysql/base
# 2. Merge inc1 into the Full Backup (WITH --apply-log-only)
sudo xtrabackup --prepare --apply-log-only --target-dir=/var/backups/mysql/base \
--incremental-dir=/var/backups/mysql/inc1
# 3. Merge the final inc2 into the Full Backup (WITHOUT --apply-log-only)
sudo xtrabackup --prepare --target-dir=/var/backups/mysql/base \
--incremental-dir=/var/backups/mysql/inc2
In the final step (inc2), omitting --apply-log-only lets XtraBackup roll back any remaining uncommitted transactions. The /var/backups/mysql/base directory is now ready for --copy-back, just like in the Quick start section.
Lessons learned from running XtraBackup in production
- Compress backups on the fly: From XtraBackup 8.0.30 onward, you can add
--compress=zstdfor inline compression. Thezstdalgorithm is fast and typically cuts storage size by 50%–70%. When restoring, just runxtrabackup --decompress --target-dir=...before the--preparestep. - Control CPU and I/O usage: On high-traffic servers, use
--parallel=4to leverage multiple CPU threads. If you notice disk I/O spiking, add--throttle=50to cap read/write operations per second. - Match version branches: Always install the XtraBackup branch that matches your MySQL version. Specifically, MySQL 8.0.x pairs with XtraBackup 8.0.x (XtraBackup should be equal to or newer than MySQL’s patch version), while MySQL 8.4 LTS pairs with XtraBackup 8.4.
- Test restores regularly: An untested backup is as good as no backup at all. Set up a monthly cronjob to automatically push your backup to a staging server and verify that MySQL starts up cleanly.
