Developers are increasingly adopting containers as their preferred way to build cloud-native applications. They are portable, easy to use and consistent. Bitnami provides a wide range of pre-packaged Docker containers. These ready-to-use assets follow the industry best practices, and bundle the most up to date and secure versions of applications and their components.
Creating regular backups of your container deployments is a good practice that prevents data loss, and gives the ability to restore your backup elsewhere.
This guide shows you how to create a backup of your container's persisted data and restore it using Bitnami container images. It uses the Bitnami WordPress image as an example, but you can follow this guide using any other Bitnami container image.
Assumptions and prerequistes
This guide makes the following assumptions:
- You are running a Bitnami container image that mounts persistent volumes and includes custom data that you wish to back up. Check this guide to learn how to develop a custom WordPress using Bitnami containers.
- You already have a Docker environment with Docker Compose installed.
- You are running a solution which is comprised of two containers: one for the application and another for the database.
IMPORTANT: Some Bitnami images use a single container and others use more than one container. The example WordPress container image used in this guide uses two separate containers, one for the application and another for the database. To backup and restore the application data, you must backup and restore all volumes mounted in each container.
Step 1: Backup data volumes
To back up the data stored in the running container, it is essential to backup all the volumes for all the containers created when running the container. In the case of the Bitnami WordPress image, there are two different containers that persist the data and mount volumes: one for the application, and another for the database.
NOTE: Check out its docker-compose.yml file](https://github.com/bitnami/bitnami-docker-wordpress/blob/master/docker-compose-mysql.yml) to learn how the image is configured.
To back up your container data, you must generate a tar.gz file within the backup directory in each container. In this case, backup both the volumes mounted in the application container and the database container. Follow these instructions:
TIP: Some Bitnami containers such as Magento, create extra volumes apart for the application and database ones. Check out container's docker-compose.yml file to learn which are the volumes that your container will mount.
Copy the container ID of each container:
docker-compose ps -q wordpress docker-compose ps -q mariadb
Stop the containers you want to back up.
$ docker-compose stop Stopping wp_wordpress_1 ... done Stopping wp_mariadb_1 ... done
Execute the commands below to create a backup file for each container. Remember to replace the CONTAINER-WORDPRESS and CONTAINER-MARIADB placeholder with the corresponding WordPress or MariaDB container ID.
$ docker run --rm --volumes-from=CONTAINER-WORDPRESS -v $(pwd)/backup:/tmp bitnami/minideb tar czf /tmp/wordpress_data_backup.tar.gz -C /bitnami/wordpress . $ docker run --rm --volumes-from=CONTAINER-MARIADB -v $(pwd)/backup:/tmp bitnami/minideb tar czf /tmp/mariadb_data_backup.tar.gz -C /bitnami/mariadb .
Check that you own the right permissions on the backup file:
$ ls -lah backup/wordpress_data_backup.tar.gz
You should see something similar to this:
-rw-r--r-- 1 root root backup/wordpress_data_backup.tar.gz
Step 2: Restore the data on each destination container
You can now restore the backed up data on a new application image. Restart each container (application and database) as if they have empty volumes. Execute the commands below. Remember to replace the CONTAINER-WORDPRESS and CONTAINER-MARIADB placeholder with the corresponding WordPress or MariaDB container ID.
Restore the application data:
docker run --rm --volumes-from=CONTAINER-WORDPRESS -v $(pwd)/backup:/tmp bitnami/minideb bash -c "rm -rf /bitnami/wordpress/* && tar xzf /tmp/wordpress_data_backup.tar.gz -C /bitnami/wordpress" docker restart CONTAINER-WORDPRESS
Restore the database data:
$ docker run --rm --volumes-from=CONTAINER-MARIADB -v $(pwd)/backup:/tmp bitnami/minideb bash -c "rm -rf /bitnami/mariadb/* && tar xzf /tmp/mariadb_data_backup.tar.gz -C /bitnami/mariadb" $ docker restart CONTAINER-MARIADB
Your data is now restored. You can check it by accessing the application and verifying that the data exists and the application is functioning correctly.
To learn more about the topics discussed in this guide, use the links below: