Hot MySQL Backups with Percona XtraBackup 8.4
In this article
This guide walks you through creating hot, online backups of MySQL 8.4 databases using Percona XtraBackup 8.4. Hot backups let you take a consistent snapshot without stopping MySQL or locking tables for writes, which is essential for production systems. You'll learn the complete workflow from installation to restoration, with practical commands you can adapt to your environment.
What You'll Need
- A server running MySQL 8.4 (or a compatible version) with root or sudo access.
- Percona XtraBackup 8.4 installed (see Step 1).
- Enough free disk space to hold the backup (typically 1.5x the size of your data directory).
- MySQL user with
BACKUP_ADMIN,RELOAD,PROCESS,LOCK TABLES, andREPLICATION CLIENTprivileges (or use root).
Step-by-Step
- Install Percona XtraBackup 8.4
On Debian/Ubuntu:
On RHEL/CentOS:wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb sudo apt update sudo apt install percona-xtrabackup-84
Verify withsudo yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm sudo yum update sudo yum install percona-xtrabackup-84xtrabackup --version. - Prepare a Dedicated Backup User (Optional but Recommended)
Create a MySQL user with least-privilege access:
Use this user in the backup commands to avoid using root.CREATE USER 'backup'@'localhost' IDENTIFIED BY 'strong_password'; GRANT BACKUP_ADMIN, RELOAD, PROCESS, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'backup'@'localhost'; FLUSH PRIVILEGES; - Create a Full Backup
Run the following command to create a full backup in a directory (e.g.,/backups/full):
This copies all data files while MySQL keeps running. The output shows progress and ends with a message likextrabackup --backup \ --target-dir=/backups/full \ --user=backup \ --password=strong_password \ --host=localhostcompleted OK. - Prepare the Backup
Before restoring, you must apply transaction logs to make the backup consistent:
This step replays committed transactions and rolls back uncommitted ones. It can take time on large databases. Always run it before restore.xtrabackup --prepare --target-dir=/backups/full - Restore the Backup
Stop MySQL, move the original data directory aside, and copy the backup into place:
Thesudo systemctl stop mysql sudo mv /var/lib/mysql /var/lib/mysql.bak sudo mkdir /var/lib/mysql sudo xtrabackup --copy-back --target-dir=/backups/full sudo chown -R mysql:mysql /var/lib/mysql sudo systemctl start mysql--copy-backcommand copies files with correct permissions. Adjust paths if your MySQL data directory is different. - Verify the Backup
After restore, check that MySQL starts and runs a quick query:
Also compare table counts or checksums against the original if possible.mysql -u root -p -e "SELECT 1;" - Automate Incremental Backups (Optional)
For daily incremental backups, first create a full backup with--backup, then run:
Each incremental uses the previous backup asxtrabackup --backup \ --target-dir=/backups/inc1 \ --incremental-basedir=/backups/full \ --user=backup --password=strong_password--incremental-basedir. To restore, prepare the full backup with--apply-log-onlyand then apply each incremental in order, followed by a final--prepare.
Common Mistakes
- Skipping the prepare step – Restoring an unprepared backup leads to corruption or MySQL failing to start.
- Wrong privileges – Missing
BACKUP_ADMINorRELOADcauses permission errors. Double-check your GRANT statements. - Not checking disk space – Backups can be large; running out of space mid-backup leaves a partial, unusable backup.
- Restoring over an existing data directory – Always move the original aside first, or use
--copy-backto an empty directory. - Mixing versions – XtraBackup 8.4 is designed for MySQL 8.4; using it with MySQL 5.7 or 8.0 may fail or produce incompatible backups.
Tips
- Use
--compress(e.g.,--compress=lz4) to save disk space, but remember to decompress during prepare. - Store backups on a different disk or server to protect against disk failures.
- Test restores regularly in a staging environment to ensure your backup process works.
- Set a cron job for backups and monitor logs for errors.
- For very large databases, consider using
--parallelto speed up backup and prepare operations.
Troubleshooting
- Backup fails with "Access denied" – Verify the user has all required privileges. Run
SHOW GRANTS FOR 'backup'@'localhost';and re-grant if needed. - MySQL won't start after restore – Check file permissions (
chown -R mysql:mysql) and the error log (/var/log/mysql/error.log). Ensure you ran--preparesuccessfully. - Prepare fails with "This target directory is not empty" – Use a fresh directory for the backup, or remove existing files if you intend to overwrite.
- Incremental backup fails with LSN mismatch – Ensure the
--incremental-basedirpoints to the last backup (full or incremental) in the chain, and that no other changes occurred between backups. - Backup is slow – Check I/O performance, use
--parallel=4(adjust based on CPU cores), and consider backing up during off-peak hours.
Frequently Asked Questions
Can I take a backup without stopping MySQL?
Yes, that's the whole point of XtraBackup. It copies data files while MySQL runs, using redo log tracking to maintain consistency.
What is the difference between full and incremental backups?
A full backup copies all data. An incremental backup only copies changes since the last backup, saving space and time. Incrementals must be applied on top of a full backup during prepare.
How long does a backup take?
It depends on database size and server I/O. A small database (a few GB) might take seconds; a multi-terabyte database could take hours. The --parallel option can help.
Do I need to prepare the backup every time?
Yes, before restoring, you must run --prepare to apply logs and make the backup consistent. For incremental backups, prepare the full backup first with --apply-log-only, then each incremental, then a final prepare.
Can I use XtraBackup to clone a database to another server?
Yes, after preparing the backup, copy it to the new server and restore it. Ensure the new server has the same MySQL version and compatible configuration.
What if my backup is corrupted?
If the backup fails or prepare errors, delete the backup and start over. Always verify backups by restoring them to a test instance. Consider using --check-privileges to catch permission issues early.