The original post is written here on my blog -> https://easyontheweb.com/mongodump-and-mongorestore-with-docker/
Taking data backup and restoring that data is one of the most commonly used things in any database (okay not that common but you catch my vibe, right?). MongoDB, which is one of the most widely used databases right now provides two simple yet powerful commands – mongodump and mongorestore for backing up and then restoring your data. In this article we’ll see how you can use mongodump and mongorestore but with a slight twist – when you’re mongo instance is running inside a Docker container.
Mongodump and mongorestore
Let us just take a quick look at what mongodump and mongorestore commands are and what they do before getting into how you can use them with your Docker mongo instance.
So, basically these two sibling commands work in compliment of each other – mongodump creates the backup of some data while mongorestore restores that backup into some other database.
How this happens is by creating a BSON file which can then be read by mongorestore and be used to fill data in some other mongodb database.
Both mongodump and mongorestore are utilities that come bundled when you installed mongo on your system so if you’re reading this, I assume you have these utilities installed as well.
Mongodump is the first of the sibling utilities that is used to create the “dump” of the data and convert the data into BSON. There are a whole lot of options that the mongodump command provides and it is best that you read those options in the official documentation rather than me pointing half of them out.
No matter you want to use srv strings or remote hosts or whatever, mongodump has an option for you.
Example of mongodump command with the uri option
The result of the mongodump command is a directory called dump in which you’ll have your data backed up in BSON format and a metadata for the same.
Once you have this bson file with you, you can use it to restore all your documents and collections (if you have dumped them as well) into any mongodb database.
Example of a mongorestore command [Run this command from your shell, not mongo shell]
Just like mongodump mongorestore has a plethora of options and flags that I’d recommend you to check out in the documentation.
A thing worth noting is that if the database or the collection that you are restoring do not exist, mongo will automatically create them for you.
Dumping and restoring data in a container
Let us see how you can follow the dumping and restoring process when your mongo is being run as a docker container.
There is nothing much to it as the process of creating a bson and restoring is still fundamentally the same. The only difference is that we need to run the commands within the docker container and then move the bson file into or out of the container (depending on whether you’re dumping or restoring).
Taking mongodump inside a container
So, let us assume we want to take the dump of data from within the docker container. The first thing we’ll need to do is get into the container first :-
This command takes you into your container and runs it with the bash command. Once you are inside the container you treat it just as you treated your bash (or whatever shell you use) of your local system and run the mongodump command the same way you would outside.
The same dump directory would be created with the bson file inside the particular database name directory.
The next step though is to move this file from within the docker container to your system outside. Well, as it turns out that’s a very simple docker command for this as well.
Oh, and yeah, run the above command from your system’s command line, not from within the container. I guess the arguments are pretty self explanatory and if run successfully, you’ll have your dump file/directory in your local filesystem.
Mongorestoring data in a container
Now that we have the dump in our local system, we’ll copy it into the container, then access the container and run mongorestore inside it. I guess you had figures it out yourself 😛
Pretty easy, you have successfully restored data in the mongo running as a container.
For more articles on mongodb please check out the section on Mongo here -> Mongo articles
Top comments (0)