Taking MySQL backups using XtraBackup


When it comes to managing your MySQL databases, ensuring that your data remains safe and secure is paramount. One effective method to protect your data is by regularly creating backups. In this guide, we’ll explore how to use XtraBackup, a powerful open-source backup solution for MySQL and its derivatives like Percona Server and MariaDB. We’ll cover various aspects of XtraBackup, complete with examples and subheadings, to help you understand and implement it in your database management workflow.

What is XtraBackup?

XtraBackup is an open-source, free tool developed by Percona that provides a flexible and efficient way to create MySQL backups. Unlike other backup methods, XtraBackup creates non-blocking, non-intrusive backups of your InnoDB and XtraDB databases, allowing your applications to continue running smoothly during the backup process. XtraBackup’s hot backup feature is a significant advantage for businesses with high-transaction environments, such as e-commerce websites and large-scale data management systems.

Getting Started: Installation and Prerequisites

Before diving into the backup process, let’s walk through the installation and prerequisites for XtraBackup. Here are the primary steps:

Install XtraBackup: Visit the Percona website and download the appropriate package for your operating system. For Debian and Ubuntu-based systems, you can use the apt-get command:

sudo apt-get install percona-xtrabackup 

For CentOS and RHEL-based systems, use the yum command:

sudo yum install percona-xtrabackup

Ensure that you have access to your MySQL server and proper privileges. The XtraBackup user should have the RELOAD, PROCESS, LOCK TABLES, and REPLICATION CLIENT privileges.

Creating a Full Backup

Now that you’ve installed XtraBackup and ensured the necessary privileges, let’s walk through creating a full backup of your MySQL database.

Choose your backup directory: Decide where you want to store your backup files. For this example, we’ll use the /backups directory.

Run the XtraBackup command:

sudo xtrabackup --backup --target-dir=/backups/full_backup 

This command will create a full backup of your MySQL data and store it in the /backups/full_backup directory.

Prepare the backup: Before you can restore or use the backup, you’ll need to “prepare” it. Run the following command:

sudo xtrabackup --prepare --target-dir=/backups/full_backup 

This command ensures that the backup is consistent and ready for use.

Creating Incremental Backups

Incremental backups are a more efficient way to back up your MySQL database, as they only store the changes made since the last backup. Here’s how to create an incremental backup using XtraBackup:

Choose your backup directory: For this example, we’ll use the /backups directory again and create a new folder called incremental_backup.

Determine the last LSN (Log Sequence Number): The LSN is a unique identifier that represents a specific point in the database’s transaction log. You can find the last LSN by checking the xtrabackup_checkpoints file in your most recent full or incremental backup. For this example, let’s assume the last LSN is 123456.

Run the XtraBackup command:

sudo xtrabackup --backup --target-dir=/backups/incremental_backup --incremental-basedir=/backups/full_backup --incremental-lSN=123456

This command creates an incremental backup based on the last LSN and stores it in the /backups/incremental_backup directory.

Prepare the incremental backup: Before using the incremental backup, you’ll need to apply it to the full backup. First, prepare the full backup:

sudo xtrabackup --prepare --apply-log-only --target-dir=/backups/full_backup

Next, apply the incremental backup to the full backup:

sudo xtrabackup --prepare --apply-log-only --target-dir=/backups/full_backup --incremental-dir=/backups/incremental_backup

Restoring Backups

In the event of data loss or corruption, you’ll need to restore your MySQL database from the backups. Here’s how to do it using XtraBackup:

  1. Stop your MySQL server:
sudo service mysql stop
  1. Move or rename your current MySQL data directory (located at /var/lib/mysql by default):
sudo mv /var/lib/mysql /var/lib/mysql_old
  1. Create a new data directory:
sudo mkdir /var/lib/mysql
  1. Restore the backup: Copy the contents of the prepared full backup (with the applied incremental backups, if any) to the new data directory:
sudo xtrabackup --copy-back --target-dir=/backups/full_backup
  1. Change the ownership of the data directory to the MySQL user:
sudo chown -R mysql:mysql /var/lib/mysql
  1. Start your MySQL server:
sudo service mysql start

Scheduling Automatic Backups

To ensure that your MySQL database remains protected, it’s essential to schedule automatic backups. You can use the cron utility to create scheduled tasks on Linux systems.

  1. Open your crontab file:
crontab -e
  1. Add a new cron job: For example, to schedule a full backup every day at 3 a.m., add the following line to your crontab file:
0 3 * * * /usr/bin/xtrabackup --backup --target-dir=/backups/full_backup

You can customize this line to create different types of backups (full or incremental) and adjust the frequency as needed.


XtraBackup is a powerful and flexible tool for creating MySQL backups, ensuring that your data remains safe and secure. By following this comprehensive guide, you’ll be able to create full and incremental backups, restore your database, and schedule automatic backups to protect your data continuously. With XtraBackup in your toolbox, you’ll be well-equipped to handle any data-related challenges that come your way.

Related Articles