Percona XtraBackup
Percona XtraBackup is an open-source, hot-backup utility for MySQL-based servers. Unlike traditional dump tools, it copies actual data files without locking your database, making it ideal for high-availability production environments.
1. Installation
XtraBackup must be installed directly on the database server. Choose the version that matches your database engine:
- XtraBackup 8.0: For MySQL 8.0 and Percona Server 8.0.
- XtraBackup 8.4 (LTS): For MySQL 8.4 LTS and newer editions.
On Ubuntu / Debian Systems
1. Use the apt package manager to dowload percona-release:
sudo apt update
2. Download the percona-release the repository package:
curl -O https://repo.percona.com/apt/percona-release_latest.generic_all.deb
3. Install the downloaded package and its dependencies using apt:
sudo apt install gnupg2 lsb-release ./percona-release_latest.generic_all.deb
4. Refresh the local cache to update the package information:
sudo apt update
5. Enable the specific percona-release product.
sudo percona-release enable pxb-84-lts
6. Install Percona XtraBackup.
sudo apt install percona-xtrabackup-84
Verify Installation
Confirm that the binary is correctly registered in your system PATH:
xtrabackup --version
2. Configuration
Use the MySQL Option File Shortcut
If you are running the backup, you can also place these credentials inside the default user configuration file (~/.my.cnf). XtraBackup automatically checks this location if no other file is specified.
Step 1: Create ~/.my.cnf
nano ~/.my.cnf
# ~/.my.cnf
[client]
user=backup_user
password=your_ultra_secure_password
Step 2: Lock permissions
chmod 600 ~/.my.cnf
Step 3: Establish Backup Directories
Create a dedicated space to house your raw data snapshots:
sudo mkdir -p /var/snapshots/mysql
3. Creating a Live Hot-Backup
Because XtraBackup performs non-locking physical backups, you can run these commands directly on a live production server without interrupting user transactions.
Execute cleanly
Because XtraBackup automatically searches ~/.my.cnf, you can run a completely clean command:
xtrabackup --backup --databases="db1 db2" --target-dir=/var/snapshots/mysql
Execute the Backup Command
Run the execution command. Provide valid database administrative credentials:
xtrabackup --backup \
--user=root \
--password="YourSecurePassword" \
--target-dir=/var/snapshots/mysql
Scroll to the very bottom of your terminal output when the command finishes. Look for the explicit text confirmation line completed OK!
4. Preparing the Backup (Testing & Validation)
When a physical backup is captured, it contains data files exactly as they looked on disk while queries were still running. This means some transactions might be half-written or uncommitted.
Before this data can be safely restored, you must prepare it to make the files structurally consistent.
xtrabackup --prepare --target-dir=/var/snapshots/mysql
Why is this step crucial?
- It replays completed transactions from the database transaction logs (
redo log). - It rolls back uncommitted transactions so the files are safe to start up.
- Testing Benefit: Running this step confirms the structural integrity of your backup files before a real disaster happens.
5. Restoring the Database
Restoring a physical backup replaces your entire existing database directory. Stop your web server or applications to prevent corrupt traffic during this process.
Step 1: Stop the Active MySQL Daemon
sudo systemctl stop mysql
Step 2: Clear or Archive the Old Data Directory
MySQL will refuse to overwrite an active, non-empty data directory. Move your current broken or old data out of the way:
sudo mv /var/lib/mysql /var/lib/mysql.bak
sudo mkdir /var/lib/mysql
Step 3: Copy Back the Prepared Files
Use the --copy-back flag to instantly migrate the prepared snapshot back into its production home directory layout:
xtrabackup --copy-back --target-dir=/var/snapshots/mysql
Step 4: Fix Directory Ownership Permissions
XtraBackup restores files using the system user that executed the command (typically root). You must hand ownership back to the mysql daemon before starting up the service:
sudo chown -R mysql:mysql /var/lib/mysql
sudo chmod -R 750 /var/lib/mysql
Step 5: Restart the Service
Launch your database instance back online:
sudo systemctl start mysql
Verify that your applications can connect smoothly and that your historical tables are completely restored.