blog

How to Backup your Dockerized WordPress Data using Named Volumes vs Backup Ninja

Andrew Abwoga

Published

With containers, there are a couple of ways that you can backup and restore your data. The most typical way to do data backups for databases in container environments is by creating database dumps. Unlike the typical database dumps, container engines like docker give you provisions to migrate or backup your data from named volumes or bind mounts.

Named volumes and bind mounts are the two mechanisms that docker uses to persist data. Bind mounts have limited functionality compared to named volumes. When using bind mounts, a file or directory in the host machine is mounted into a container. When using named volumes, a special folder is created within Docker’s storage directory and docker manages that directory on the host machine. Named volumes are much easier to back up or restore than bind mounts as we shall see in this blog. Also, other key advantages of using named volumes are:

  • You can manage volumes using Docker CLI tools and API
  • They can be more safely shared using multiple containers
  • Some volume drivers will let your store volume on remote hosts or cloud providers and encrypt the contents of volumes
  • New volumes can be pre-populated by a container

Backup and Restore named data volumes

Backup

For example, to backup some_named_data_volume to /tmp/some_data_archive.tar.bz2, you can simply run:

$ docker run --rm -v some_named_data_volume:/volume -v /tmp:/backup alpine tar -cjf /backup/some_database_archive.tar.bz2 -C /volume ./

Restore

To restore the same database volume, you can run:

$ docker run --rm -v some_named_data_volume:/volume -v /tmp:/backup alpine sh -c "rm -rf /volume/* /volume/..?* /volume/.[!.]* ; tar -C /volume/ -xjf /backup/some_database_archive.tar.bz2"

Some open source utilities like volume-backup have made these commands much simpler as in the example below.

Simpler Backup

$ docker run -v some_named_data_volume:/volume -v /tmp:/backup --rm loomchild/volume-backup backup some_database_archive

Simpler Restore

$ docker run -v some_named_data_volume:/volume -v /tmp:/backup --rm loomchild/volume-backup restore some_database_archive

Backup and Restore WordPress Data Volumes

Steps to Setup a Docker WordPress website

  • Create a directory for the WordPress project
$ mkdir WordPress
  • Change into the project directory
$ cd WordPress
  • Create a docker-compose file with a quickstart configuration file. Below is a quickstart configuration file that will use docker-compose to set up a WordPress website on docker. Save the contents below onto a docker-compose.yml file.
    • NB: Take note of the mysql db_data volume that is configured on the docker compose file.
version: '3.3'
   
services:
   
   db:
   
     image: mysql:5.7
   
     volumes:
   
       - db_data:/var/lib/mysql
   
     restart: always
   
     environment:
   
       MYSQL_ROOT_PASSWORD: somewordpress
   
       MYSQL_DATABASE: wordpress
   
       MYSQL_USER: wordpress
   
       MYSQL_PASSWORD: wordpress
   
   wordpress:
   
     depends_on:
   
       - db
   
     image: wordpress:latest
   
     ports:
   
       - "8000:80"
   
     restart: always
   
     environment:
   
       WORDPRESS_DB_HOST: db:3306
   
       WORDPRESS_DB_USER: wordpress
   
       WORDPRESS_DB_PASSWORD: wordpress
   
       WORDPRESS_DB_NAME: wordpress
   
volumes:
   
    db_data: {}
  • Build the project using docker-compose.
$ docker-compose up -d

At this point, WordPress should start running on port 8080. When you open port 8080 on your browser the first thing you will have to do is to go through the setup and configuration process.

Later on, you may need to fill up your WordPress content depending on your needs for the website. If you have to backup and restore all the configuration and content data you can follow the steps as shown below.

Steps to Backup and Restore WordPress Data Volumes

  • Backup your WordPress data volumes
    • Identify your named volume by list all the named volumes using the command below.
$ docker volume ls
   
output:
   
DRIVER              VOLUME NAME
   
local               wordpress_db_data
  • Run the backup command as below.
$ docker run --rm -v wordpress_db_data:/var/lib/mysql -v /tmp:/backup alpine tar -cjf /backup/my_wordpress_database_backup.tar.bz2 -C /volume ./
  • Restore your WordPress data volume
$ docker run --rm -v wordpress_db_data:/var/lib/mysql -v /tmp:/backup alpine sh -c "rm -rf /volume/* /volume/..?* /volume/.[!.]* ; tar -C /volume/ -xjf /backup/my_wordpress_database_backup.tar.bz2"

Backup and Restore your Dockerized WordPress Data Using Backup Ninja

Steps to install Backup Ninja Agent

  1. Assuming that you have set up your WordPress website using steps 1-3 in the previous section. The next thing you need to do is to identify the network and port that the docker container is running on. You could decide to join MySQL Docker container to a separate network namespace on your docker compose configuration. See details about how to configure networks using docker compose Networking in Compose.
  2. Once you identify the network namespace where your MySQL instance is running on, sign in to the Backup Ninja web portal.
  3. Select Servers on the navigation bar on the left. Backup and Restore your Dockerized WordPress Data Using Backup Ninja
  4. Click on the green Add Server button at the center or at the top right of the page. A new page will appear.
  5. Select your preferred version of MySQL on the page that appears next.Backup and Restore your Dockerized WordPress Data Using Backup Ninja
  6. Press Continue on the lower left part of the page.
  7. Specify the network namespace where the MySQL container resides on the Hostname field as in the diagram shown below.Backup and Restore your Dockerized WordPress Data Using Backup Ninja
  8. Go on to complete the installation process by clicking the Continue button.

Steps to Create a Backup Schedule and Restore Your WordPress Data

  1. Once your installation is complete, you can set up a Backup Schedule by selecting the Schedules option on the left navigation bar. Another page will appear. Steps to Create a Backup Schedule and Restore Your WordPress Data
  2. Specify the names of your backup and other relevant details on the page that appears. Mysqldump is available by default.Steps to Create a Backup Schedule and Restore Your WordPress Data
  3. Continue on to finish up the schedule setup by pressing the Continue button. At some point you will need to specify your preferred schedule as in the diagram below. Steps to Create a Backup Schedule and Restore Your WordPress Data
  4. Once you are done setting up the schedules, backups will begin based on your schedule and you will be able to see a trace of all your backups as shown below. To restore a specific backup you can select the Backups option on the left navigation bar, select the More dropdown and select the Restore option as shown below.

Final Thoughts

Although container engines like Docker give you provisions to backup your database, you may have to consider backing up at the rate of change on your WordPress configuration and content data. You can achieve a much more fluid and continuous backup and restore process by considering the right tools.

To ensure you have continuous backups you can pull together a script that will be executed using cron jobs to automate the backup process using the commands we have gone through above. The challenge would be how you can be able to monitor if your backups are consistent and reliable, the rate at which you can restore your backup at a previous point in time and the hustle of having to maintain your scripts. There are better tools that can guarantee you a more seamless and automated backup and restore processes like Backup Ninja.

Subscribe below to be notified of fresh posts