In this post, I’ll show how to back up and restore data from a database MongoDB hosted on MongoDB Atlas.
- Basic knowledge of MongoDB
- Basic knowledge of Bash Scripts
- Basic knowledge of MongoDB Tools
Download and install mongodump.
The mongodump is, quoting from the official site, “a utility that creates a binary export of a database’s contents…“.
Before to use this tool you must get the db connection string, and depending on cluster structure you have to compose your connection string.
In the official guide they explain how to compose the connection string in case you have to connect to one instance, replica set and sharded cluster.
In my case is a replica set and my connection string is something like shown below:
mongodb://<user>:<password>@mycluster-00-00.xyzw.mongodb.net:27017,@mycluster-00-01.xyzw.mongodb.net:27017,@mycluster-00-02.xyzw.mongodb.net:27017
So, once we prepared the connection string, we are ready to back up data of our database:
am@animus:~$ mongodump
--uri="mongodb://mongodb://<user>:<password>@mycluster-00-00.xyzw.mongodb.net:27017,@mycluster-00-01.xyzw.mongodb.net:27017,@mycluster-00-02.xyzw.mongodb.net:27017"
--ssl
--authenticationDatabase=<db user>
--db=<database name>
The command will produce a folder dump with all data backup.
Download and install mongorestore.
The mongorestore is, quoting from official site, “a program that loads data from either a binary database dump created by mongodump or the standard input into a mongod or mongos instance“.
Before to use this tool we need the connection string to the destination cluster and the considerations made for the dump are valid for the restore too.
am@animus:~$ mongorestore
--uri="mongodb://mongodb://<user>:<password>@mycluster-00-00.xyzw.mongodb.net:27017,@mycluster-00-01.xyzw.mongodb.net:27017,@mycluster-00-02.xyzw.mongodb.net:27017"
--ssl
--authenticationDatabase=<db user>
--db=<database name>
<database dump folder>
After the command run is completed, you’ll have the database completely restored.
MongoDB Atlas is a cloud service and, as such, you can count on better backup and restore data strategies able to not impact on the normal activity of the service. The manual backup and restore could be useful in dev or test scenario and when the dimension of data to copy is not so huge. Take in mind the use of mongodump and mongorestore in production could have an impact on the normal activity of the service.
Source link
lol