How To Backup and Restore Application Based on Docker Container (With Moodle as a Sample)

Pahrial Ms
5 min readApr 7, 2023

--

Photo by Guillaume Bolduc on Unsplash

Besides being able to deploy, performing backups is also quite important in matters of high availability. I’m sure everyone wouldn’t want any incidents that could cause important services and data to become unavailable. But if that were to happen, with the existence of backups, the threat to high availability can be minimized because we have backups.

In this note, I performed a backup procedure for a Moodle application based on a Docker container. The story behind it is that I will migrate this Moodle application to another server, hence the need to perform a backup in order to do so. In the final section, I tested the restore backup.

In this practice, there are two things that need to be done for backup: first, backup the container by utilizing the docker commit feature. Second, backup the docker volume. This is because docker commit only takes backup of the configuration inside the container and does not include the data stored in the volume. Therefore, a different step needs to be taken to backup the data stored in the docker volume.

And for the restore process, it is more or less the same as what was done in the backup step.

Here is the docker-compose that I use to deploy Moodle based on a Docker container (nano docker-compose.yml) :

version: '2'
services:
mariadb:
image: docker.io/bitnami/mariadb:10.6
environment:
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=yes
- MARIADB_USER=bn_moodle
- MARIADB_DATABASE=bitnami_moodle
- MARIADB_CHARACTER_SET=utf8mb4
- MARIADB_COLLATE=utf8mb4_unicode_ci
volumes:
- 'mariadb_data:/bitnami/mariadb'
moodle:
image: docker.io/bitnami/moodle:4.1
ports:
- '5006:8080'
- '5007:8443'
environment:
- MOODLE_DATABASE_HOST=mariadb
- MOODLE_DATABASE_PORT_NUMBER=3306
- MOODLE_DATABASE_USER=bn_moodle
- MOODLE_DATABASE_NAME=bitnami_moodle
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=yes
volumes:
- 'moodle_data:/bitnami/moodle'
- 'moodledata_data:/bitnami/moodledata'
depends_on:
- mariadb
volumes:
mariadb_data:
driver: local
moodle_data:
driver: local
moodledata_data:
driver: local

Backup moodle & mariadb container with docker commit

There are two containers for the Moodle application, one for the Moodle application itself and another one as the Moodle database (MariaDB).

# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3256e6166897 bitnami/moodle:4.1 "/opt/bitnami/script…" 6 hours ago Up 6 hours 0.0.0.0:5006->8080/tcp, :::5006->8080/tcp, 0.0.0.0:5007->8443/tcp, :::5007->8443/tcp moodle-moodle-1
74e176cde6da bitnami/mariadb:10.6 "/opt/bitnami/script…" 6 hours ago Up 6 hours 3306/tcp moodle-mariadb-1

Here, I use docker commit to save every configuration and change that occurs inside the container into an image form. Simply put, we back up the container into an image, and from that image, we can use it again to create Moodle and MariaDB containers. Run the docker commit container command :

# docker commit -p moodle-moodle-1 backup-moodle-moodle-1
# docker commit -p moodle-mariadb-1 backup-moodle-mariadb-1

Backup the image into a tar file for easier transfer to another server :

# docker save -o backup-moodle-moodle-1 < backup-moodle-moodle-1.tar
# docker save -o backup-moodle-mariadb-1 < backup-moodle-mariadb-1.tar

To restore the image from the previous tar file, we can use the docker load command :

# docker load -i backup-moodle-moodle-1.tar
# docker load -i backup-moodle-mariadb-1.tar

Backing up Docker volumes to safely data

As seen in the docker-compose file above, there are 3 docker volumes used and mounted, 1 is mounted to the MariaDB container and 2 to the Moodle application container. I will back up all three of them using the temporary container technique as explained in the Docker documentation.

Backup the data in the docker volumes into a compressed file archive and save it to the local directory /tmp :

# docker run --rm -v mariadb_data:/bitnami/mariadb -v /tmp:/backup ubuntu tar -czvf /backup/mysql-backup.tar.gz /bitnami/mariadb

# docker run --rm -v moodle_data:/bitnami/moodle -v /tmp:/backup ubuntu tar -czvf /backup/moodle-backup.tar.gz /bitnami/moodle

# docker run --rm -v moodledata_data:/bitnami/moodledata -v /tmp:/backup ubuntu tar -czvf /backup/moodle-data-backup.tar.gz /bitnami/moodledata
# ls -l /tmp/
total 73740
-rw-r--r-- 1 root root 64922813 Apr 7 10:31 moodle-backup.tar.gz
-rw-r--r-- 1 root root 4300308 Apr 7 10:36 moodle-data-backup.tar.gz
-rw-r--r-- 1 root root 6270507 Apr 7 10:27 mysql-backup.tar.gz

By this point, the data in the docker volumes has been backed up into a tar file and can be used when we want to restore the data from the backup.

Restore moodle & mariadb (container + data volume)

The restore process can be done on any Docker host, including the same Docker host as before.

First, load the image from the previous backup archive file so that the container can be created :

# docker load -i backup-moodle-moodle-1.tar
# docker load -i backup-moodle-mariadb-1.tar

# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
backup-moodle-moodle-1 latest 29bc97630535 5 hours ago 639MB
backup-moodle-mariadb-1 latest eb0c0005b976 5 hours ago 338MB

Second, restore the backup data volume from the archive file into a new Docker volume, using the same temporary container technique as when backing up the previous Docker volume.

Create a new Docker volume, and then restore the data from the archive file into the newly created Docker volume :

# docker volume create moodle_mariadb_data
# docker volume create moodle_moodle_data
# docker volume create moodle_moodledata_data

# docker run --rm -v moodle_mariadb_data:/bitnami/mariadb -v /tmp:/backup ubuntu tar -xvzf /backup/mysql-backup.tar.gz -C /

# docker run --rm -v moodle_moodle_data:/bitnami/moodle -v /tmp:/backup ubuntu tar -xvzf /backup/moodle-backup.tar.gz -C /

# docker run --rm -v moodle_moodledata_data:/bitnami/moodledata -v /tmp:/backup ubuntu tar -xvzf /backup/moodle-data-backup.tar.gz -C /

Third, create a new Docker Compose file, or edit the Docker Compose file used at the beginning. Note that in this new Docker Compose file, we will be using the existing Docker volume that has been filled with data from the previous restoration process (nano docker-compose.yml) :

version: '3.6'
services:
mariadb:
image: backup-moodle-mariadb-1
volumes:
- 'mariadb_data:/bitnami/mariadb'
moodle:
image: backup-moodle-moodle-1
ports:
- '5006:8080'
- '5007:8443'
environment:
- MOODLE_DATABASE_HOST=mariadb
- MOODLE_DATABASE_PORT_NUMBER=3306
- MOODLE_DATABASE_USER=bn_moodle
- MOODLE_DATABASE_NAME=bitnami_moodle
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=yes
volumes:
- 'moodle_data:/bitnami/moodle'
- 'moodledata_data:/bitnami/moodledata'

volumes:
mariadb_data:
external: true
name: moodle_mariadb_data

moodle_data:
external: true
name: moodle_moodle_data

moodledata_data:
external: true
name: moodle_moodledata_data

Finally, we just need to run docker-compose up, then verify that the Moodle application is running and all the stored data is still intact.

--

--

No responses yet