Ensuring the security and resilience of your data hinges on having a robust backup strategy, and Percona XtraBackup (PXB), our open source backup solution for all versions of MySQL, is designed to make backups a seamless procedure without disrupting the performance of your server in a production environment.

When combined with the versatility of Docker containers, it becomes a dynamic duo, offering a scalable approach to data backup and recovery. Let’s take a look at how they work together.

Working with Percona Server for MySQL 8.1 and PXB 8.1 Docker images

Start a Percona Server for MySQL 8.1 instance in a Docker container

Percona Server for MySQL has an official Docker image hosted on Docker Hub. For additional details on how to run an instance in a Docker environment, refer to this section in Percona Documentation:

sudo docker runThis is the command to run a Docker container

--name percona-server-8.1 – Assigns the name “percona-server-8.1” to the Docker container

-v mysql_data:/var/lib/mysql – Creates a Docker volume named “mysql_data” and mounts it to the “/var/lib/mysql” directory inside the container. This is typically used to store MySQL data persistently.

-v /var/run/mysqld:/var/run/mysqld–  Mounts the host’s “/var/run/mysqld” directory to the container’s “/var/run/mysqld” directory. This can be useful for sharing the MySQL socket file for communication between processes. 

-p 3306:3306 –  Maps port 3306 on the host to port 3306 on the container. This is the default MySQL port, and it allows you to access the MySQL server running inside the container from the host machine.

-e MYSQL_ROOT_HOST=% –  Sets an environmental variable MYSQL_ROOT_HOST to ‘%’  (which means any host). This is often used to allow root connections from any host.

-e MYSQL_ROOT_PASSWORD=mysql Sets an environmental variable MYSQL_ROOT_PASSWORD to ‘mysql’. This is the password to the MySQL root user

-d Run the container in the background (detached mode).

percona/percona-server:8.1.0 Specifies the Docker image to use for creating the container. In this case, it is the Percona Server for MySQL version 8.1.0

Note: 

  • To work with Percona Server for MySQL 8.0 Docker images, replace the Docker image name with percona/percona-server:8.0 and Docker container name with percona-server-8.0.
  • To work with Percona Server for MySQL 5.7 Docker images, replace the Docker image name with percona/percona-server:5.7 and Docker container name with percona-server-5.7.
  • Percona XtraBackup 8.1 can only take backups of Percona Server for MySQL 8.1. Similarly, Percona XtraBackup 8.0 and Percona XtraBackup 2.4 can only take backups of Percona Server for MySQL 8.0 and 5.7, respectively.

Add data to the database

Let’s add some data to the Percona Server database. Create a test database and add a table t1 inside with five rows.

Note:

  • In the case of Percona Server for MySQL 8.0, replace the container name with  percona-server-8.0.
  • In the case of Percona Server for MySQL 5.7, replace the container name with  percona-server-5.7.

Run Percona XtraBackup 8.1 in a container, take a backup, and prepare

The Docker command runs Percona XtraBackup 8.1 within a container using the data volume of the Percona Server container (percona-server-8.1). It performs a MySQL backup and stores the data on the volume (pxb_backup_data). The container is removed  (--rm) after execution, providing a clean and efficient solution for MySQL backup operation. In the case of Percona XtraBackup 8.0 or 2.4, replace the Docker image name in the below command to percona/percona-xtrabackup:8.0 or percona/percona-xtrabackup:2.4 respectively.

Stop the Percona Server container

Before attempting to restore the backup, make sure the Percona Server container is stopped.

Note:

  • In the case of Percona Server for MySQL 8.0, sudo Docker stop percona-server-8.0.
  • In the case of Percona Server for MySQL 5.7, sudo Docker stop percona-server-5.7.

Remove the MySQL data directory

This step ensures that the MySQL data directory is empty before you attempt the --copy-back operation. Remember to replace the Docker image and container names in case Percona Server for MySQL 8.0 or 5.7 is used.

Run Percona XtraBackup 8.1 in a container to restore the backup

The Docker command uses the data volume from a Percona Server for MySQL 8.1 container (percona-server-8.1) and runs Percona XtraBackup 8.1 within a separate container. The command executes the xtrabackup --copy-back operation, restoring MySQL data from the specified directory (/backup_81) to the MySQL data directory (/var/lib/mysql).

Note:

  • When Percona XtraBackup 8.0 is used, replace the Docker image name to percona/percona-xtrabackup:8.0 and Percona Server container name to percona-server-8.0
  • When Percona XtraBackup 2.4 is used, replace the Docker image name to percona/percona-xtrabackup:2.4 and Percona Server container name to percona-server-5.7 respectively.

Start the Percona Server container to verify the restored data

When we stop and remove the original Percona Server container, the ownership and permission of the files in the mounted volumes may change. A more secure and targeted approach would be to identify the correct user and group IDs needed for the MySQL process and set the ownership accordingly. 

This sets the correct ownership for the MySQL data directory. Now, start the Percona Server instance inside the container.

Once the server is started, fetch the total number of records in the test.t1 table to verify the correctness of the restored data.

Summary

To sum up, Percona XtraBackup is an essential tool for data protection because it provides a dependable and effective backup for MySQL databases. Its easy integration with Docker containers increases its usefulness even more by offering a scalable and adaptable method for recovering and backing up data.

We encourage users to continue using Percona XtraBackup and hope that this blog is useful. Happy MySQLing!

Percona XtraBackup is a free, open source, complete online backup solution for all versions of Percona Server for MySQL and MySQL. It performs online non-blocking, tightly compressed, highly secure backups on transactional systems so that applications remain fully available during planned maintenance windows.

Download Percona XtraBackup

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments