Mongo Automatic Backup Docker Image

SunilSK
3 min readOct 10, 2020

MongoDB stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time

Like, any database we should consider taking regular backup strategy in order to restore during any unexpected events.

There are several methods out in market but for our use case the ‘mongodump’ and ‘mongorestore’ programs qualified our selection criteria.

Lets start with our plan :

Restore Frequency

It depends on how critical is your data , I went with once a day at midnight when the user traffic at my site was down.

Taking Backup

  1. mongodump is a utility for creating a binary export of the contents of a database.
  2. On the Docker instance where the Mongo is running .Run mongodump from the system command line, not the mongo shell.

The basic syntax for this backup is : mongodump <options> <connection-string>

There are several flags . Considering all the flags this would be our backup command

mongodump --uri="mongodb://localhost:27017/collection_name" --gzip --out=<location_where_youwant_toStore>

mongodump

This zipped output can be pulled out using ansible (or any tool and placed on our safe_host (DR server). We will then be using this to restore as mentioned above.).Since we are using the gzip flag the output bson file is pretty small.

Restoring from Backup

  1. The mongorestore program loads data from either a binary database dump created by mongodum(which we did above) or the standard input into a mongod or mongos instance.
  2. On the Mongo docker instance where the MongoDb is running :

> Run mongorestore from the system command line, not the mongo shell.

The basic syntax for the restore is : mongorestore <options> <connection-string> <directory or file to restore>

Considering all the flags as specified in the mongo official page, I went with the following for my project :

mongorestore --uri="mongodb://localhost:27017" -d collection_name --gzip --dir=/Users/sunil.khannukar/Desktop/work/Blue_Print_Check_React/MonDump/New/bp_analyzer

mongorestore

OK all the above is the the details of backup and restore strategy of Mongo. Let’s build an image which does all the above. Here’s the Dockerfile

# Pull base image.
FROM dockerfile/ubuntu
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install cron && apt-get clean && rm -rf /var/lib/apt/lists/ WORKDIR /data/db
COPY mongo_backup.sh /data/mongo_backup.sh
COPY CMDS.sh /etc/CMDS.sh
RUN chmod 777 /etc/CMDS.sh
COPY mongodump-cron /etc/cron.d/mongodump-cron
RUN chmod 0644 /etc/cron.d/mongodump-cron
RUN crontab /etc/cron.d/mongodump-cron
CMD ./etc/CMDS.sh

Just go ahead and spin your container with the above dockerfile and you get a fully automated backup dockerised mongoDB container.

Note : You need to still add the volume mounts and network which will get covered in your docker-compose file.

Sign up to discover human stories that deepen your understanding of the world.

SunilSK
SunilSK

Written by SunilSK

Entering into Full Stack development from the analytical world of Performance Engineering !

No responses yet

Write a response