How to set up an auto backup routine for MongoDB w/ Docker

You might think that creating a Docker volume is enough to keep the data safe, but it’s definitely not!

--

Volumes are useful and bring us the confidence that our data will be safe even when the container stops or is deleted, but there’s one serious problem to deal with: it does not work as a backup. As you might know, volumes SHARE the data between the container and the host machine, which means that if the database corrupts your volume will be consequently corrupted too.

To solve this problem we can use some approaches, in this article I’ll explain one of them, using the default MongoDB’s backup tool. Mongodump is a MongoDB utility that creates a binary export of the database content.

Basically, what we’re going to do is to set a cron job to run mongodump every N time. The command will create the backup inside a Docker volume, ensuring that the backup will be safe if we, for some reason, lose the container.

Hands-on!

Then, let’s create a backup volume, as the dump folder will be created inside the container and we need to share it with the host machine.

sudo docker volume create mongo_backup

Now we can run the container using the created volume (you can optionally use the -d option to detach from the container):

sudo docker run --name mongo -d -v mongo_backup:/mongo_backup mongo

With the container running we need to attach to its bash. For this, let’s use the docker exec command with the -it option (interactive mode) to run bash inside the container.

sudo docker exec -it mongo bash 

Now we are inside the container, as the root user:

By default, Mongo’s image doesn’t come with any text editor, so we need to install one. I will install vim, but you can use any other, like nano. Don’t forget to update the repositories before installing the editor:

apt-get update && apt-get install vim

The Crontab

An amazing tool to automate scripts on Linux is cron, it is a time-based job scheduler, which means that we can set a custom periodicity to run a script. However, as the text editor, MongoDB’s official Docker image doesn’t come with the cron installed, so, let's install it:

apt-get install cron

With cron installed, we just need to create the backup script and set it to run periodically. It will look like the following code:

I named the script “auto_dump.sh”

The DEST variable uses the current date and hour to set the backup directory name, this assures that a folder won’t be overwritten and helps us to identify when the backup was created

Now that we have the script created, it’s necessary to make the script executable running the command:

chmod +x auto_dump.sh

Then, we need to use cron to define the periodicity that the script will run, to open the cron editor run:

crontab -e

A similar file will open:

Now, let’s append the periodicity and the script to run at end of the file. I will set the auto_dump.sh script to run every day at 2 a.m.

0 2 * * * /auto_dump.sh

The first number sets the minutes, the second the hours and then the day of the month, the month, and the day of the week. We can use * to mean all.

With this ready, we just need to restart the cron service, with the command:

service cron restart

We’re almost done!

Deleting old backups

You probably imagined that running this job every day will eat the available storage, therefore we can add another command to our script to delete all of the content older than N days (I’ll set 5 days). Then it will look like this…

A great explanation of this command is available here (I strongly recommend the reading)

With these pretty simple steps, we now have the database’s history from the 5 previous days backed up!

References

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

--

--

Computer Science student and web development teacher. In love with software development <3