DockerLinux

3 Simple Steps MariaDB Backup Restore Containers to Another Server

Diagram showing Docker configuration for MariaDB backup and restore process.

As businesses and developers increasingly rely on containerized applications, ensuring the Docker MariaDB backup restore containers databases becomes critical. MariaDB, an open-source relational database management system, is often run inside Docker containers. In this comprehensive guide, we’ll walk you through the detailed steps required to back up and restore MariaDB containers from one server to another. This can be particularly useful for disaster recovery, migrating to new hardware, or simply for maintaining data redundancy.

Prerequisites

Before diving into the process, ensure that the following prerequisites are met:

  • Docker is installed and running on both the source and target servers.
  • A MariaDB container is running on the source server.
  • Access to the command line interface (CLI) on both servers.
  • Sufficient permissions to execute docker and scp or rsync commands.

A sleek, modern vector illustration of a server represented as a clean, minimalistic box on a white backdrop. The server is shown transmitting data, symbolized by elegant lines and arrows, to another server, depicting the concept of data transfer. The scene is designed in a flat, contemporary style to highlight the simplicity and efficiency of the process.


Step 1: MariaDB Backup Restore Containers

First, we need to create a backup of the MariaDB data from the source server.

1. Identify the MariaDB Container

Start by identifying the name or ID of your running MariaDB container. This can be done using the following command:

docker ps

This command lists all running containers along with their respective IDs, names, and statuses. Look for the MariaDB container in the output.

2. Create a Backup Directory

Next, create a directory on your source server where the backup file will be stored.

mkdir -p /path/to/backup

Replace /path/to/backup with your preferred backup directory path.

3. Execute the Backup Command

The following command is used to create a dump of your MariaDB database:

docker exec mariadb_container_name /usr/bin/mysqldump -u root --password=rootpassword database_name > /path/to/backup/backup.sql

Replace mariadb_container_name with the actual name of your MariaDB container, and database_name with the name of the database you wish to back up. This command will prompt you to enter the root password for the MariaDB server. The output will be a SQL dump file located at /path/to/backup/backup.sql.

4. Security Considerations

It’s important to note that while backing up your database, sensitive data such as passwords should be handled securely. Avoid writing passwords in scripts or command lines that might be exposed to unauthorized users.

Step 2: Transfer Backup File to the Target Server

Once the backup file is created, the next step is to transfer it from the source server to the target server.

1. Using SCP to Transfer the File

Secure Copy Protocol (SCP) allows you to securely transfer files between hosts over an SSH connection. Use the following command to transfer the backup file:

scp /path/to/backup/backup.sql user@target_server:/path/to/destination/

Replace user with the username on the target server and target_server with its address. Similarly, replace /path/to/destination/ with the directory path on the target server where you want the backup file to be stored.

2. Verify the File Transfer

After the transfer completes, it’s good practice to verify the integrity of the transferred backup file on the target server.

ls -lh /path/to/destination/backup.sql

This command checks if the file exists and confirms its size, ensuring that the transfer was successful and complete.

Step 3: Restore Backup on the Target Server

With the backup file successfully transferred, the next objective is to restore the database on the target server.

1. Start a New MariaDB Container

Before restoring the backup, ensure a MariaDB container is up and running on the target server. You can start a new MariaDB container using the following command:

docker run --name mariadb_container -e MYSQL_ROOT_PASSWORD=root_password -d mariadb:latest

Replace mariadb_container with your chosen name for the container and root_password with a secure root password.

2. Copy the Backup File into the Container

Use the docker cp command to copy the backup file into the running MariaDB container:

docker cp /path/to/destination/backup.sql mariadb_container:/backup.sql

3. Execute the Restore Command

Access the MariaDB container and execute the following command to restore the database from the backup file:

docker exec -i mariadb_container /usr/bin/mysql -u root --password=root_password database_name < /backup.sql

Replace database_name with the name of the database you wish to restore. This command will prompt you to enter the root password of the MariaDB server within the container.

If database_name does not exist, create it before restoring by executing:

docker exec -i mariadb_container /usr/bin/mysql -u root --password=root_password -e "CREATE DATABASE database_name;"

4. Post-Restoration Verification

After restoring the backup, it’s crucial to verify that the data was restored correctly. You can log into the MariaDB container and run some queries to confirm that the data is intact:

docker exec -it mariadb_container /usr/bin/mysql -u root --password=root_password

Conclusion

By following the detailed steps outlined in this guide, you can successfully back up and restore MariaDB containers to another server. This process not only ensures the security and reliability of your data but also provides a strategy for disaster recovery and data redundancy. Always remember to test backup and restoration processes in a development or staging environment before applying them to your production systems.

Backing up and restoring databases is a critical component of data management. Consistently performing these tasks ensures you’re prepared for unexpected data loss scenarios. If you have any questions or run into issues during the process, don’t hesitate to seek further assistance.

Hi, I’m Sysadmin.ID

Leave a Reply

Your email address will not be published. Required fields are marked *