Overview

Skill Level: Intermediate

This article describes taking the backup of Mongo DB database & collections created with in a dockerized Mongo DB.

Ingredients

Following are the prerequisites for the articles:

1. Docker Toolbox on Windows-7

2. Mongo DB container running on docker

3. Create a database and atleast one collection in that database.

4. Knowledge about the Docker's concepts,  command and Docker Volumes.

5. Knowledge about the Mongo DB

Step-by-step

  1. Introduction & Purpose

    Assuming that you have already Mongo DB running as a container and created a database atleast with one collection.  That collection has got some documents in it and you would like to take the backup of your data at some point of time using one of the options provided by Mongo.

    As I said, “options“, so there are many ways to take the backup, like using Mongo DB Atlas, Cloud Manager, File System and “mongodump” command. This article focuses on taking the backup using the mongodump command as it is quite easiest method for small database.

    The purpose of writing this article was to share the exprienece I got when I was trying to take the back-up of a database running on Windows-7 and I spent good number of hours trying to figure out why the docker volume was not producing any output even after the successful execution of the commands.

  2. Map the Folders with Volume directory

    Before you start writing the commands, I would suggest you to map the folder where you want to store the backup on windows. For example, I created a folder on windows : C:\os\dbbackup. 

    Map the windows folder (written above) in the “Settings” option of your docker machine as shown below. The value in “Folder Name” will be used in docker command while creating a volume. You can keep whatever you like. This step is one of most important step for windows environment. I had to struggle to get to this option, hope you won’t have to.

    dockermachine-vol-mapping

     

     

     

  3. Back up

    Run the following command finally to take the backup of your database “resourcedb” , here, in your running mongo db container :

    Command -> docker run -it –rm –link resourcedb:mongo -v //c/os/dbbackup:/backup mongo bash -c “mongodump -v –host mongo:27017 –db resourcedb –out=/backup”

    Explanation of above command :

    I will explain some important points of this command, not all the options:

    1. “resourcedb” is the name of mongodb container running.

    2. -v //c/os/dbbackup  Рis the name of that mapped folder as mentioned in step 1 (above).

    3. /backup – is the directory created in the shell container of Mongo DB. Same directory is used in mongodump command also with “out” option. “mongodump” command creates the back up in “backup” directory created at root level. This “backup” directory is mapped with volume created and mapped with c/os/dbbackup folder.

    • ¬†You can use this command to get into shell of your mongo container
      • docker exec -it <mongo-container-id> /bin/sh
      • Once you are into shell, run “mkdir backup” command to create a backup directory.

    4. mongo:27017 – is the port used with in mongo db container.

    5. “resourcedb” used in “mongodump” command is the name of actual database to be backed up.

     

  4. Check

    Once the command is executed successfully, it will create the folder structure matching your database name. That folder should include “BSON” file(s) as per your database & collection(s) and meta data files too.

  5. Conclusion

    To keep it simple, I started with “backup” only. I hope you liked this article and thanks for your time.

    Waiting for your feedback.

Join The Discussion