How to Back Up and Restore a Set of Collections in MongoDB Atlas
Introduction
Recently, I responded to an emergency where we needed to back up only a single collection in a database hosted on MongoDB Atlas. At the time of this writing, collection backup and restore are not currently available via Atlas' UI, which is a gap since there are many instances where we need to import and export a specific resource within a database. In this blog, you can find the methods to back up a single collection in MongoDB Atlas to avoid that headache in your emergency scenario.
Method 1: Use MongoDB Compass
For this method, you must connect to your MongoDB Atlas via MongoDB Compass, export the collection(s) one at a time via the UI, and do the same when you need to import. Below, you can find the details on how to perform this procedure:
Step 1: Connect to the Atlas instance
For this step, we have two options. We can get the connection string directly from the MongoDB Atlas website or pull it from the Atlas CLI. The code to do so is as follows:
atlas clusters connectionStrings describe <clusterName> --projectId <projectId>
How do you find "clusterName" and "projectId"? I find the easiest way to do it is simply by looking at the URL, as all Atlas clusters have the following standard web address for all clusters:
https://cloud.mongodb.com/v2/#/clusters/detail/<clusterName>
Running the command above gives us a sample URI string:
mongodb+srv://testing.xvb16.mongodb.net
From this, you can connect to a database via MongoDB compass with the following connection string:
mongodb+srv://<username>:<password>@testing.xvb16.mongodb.net/
Step 2: Export the data
Then, simply go to the collection and select "Export Data," which then allows you to either export a whole collection or a subset as query results:
Step 3: Restore the data
Just as easy, you can then add the data into a database as a JSON or CSV file.
Method 2: Use mongoimport/mongoexport Compass
When you export multiple collections or databases or when MongoDB Compass is unavailable, you can use mongoexport and mongoimport to perform backup and restore. The sample bash script below would export a list of collections.
USERNAME="yourCoolUsername"
PASSWORD="superDuperSecret"
CONNECTION_STRING="mongodb+srv://${USERNAME}:${PASSWORD}@testDatabase.xvb16.mongodb.net/sample_database"
COLLECTIONS=("movies" "sessions" "users")
for i in "${COLLECTIONS[@]}"; do
OUTPUT="$i.json"
mongoexport --uri ${CONNECTION_STRING} --username ${USERNAME} --password ${PASSWORD} --collection $i --out ${OUTPUT}
done
Vice versa, to restore the same list, you can run this script:
USERNAME="yourCoolUsername"
PASSWORD="superDuperSecret"
CONNECTION_STRING="mongodb+srv://${USERNAME}:${PASSWORD}@testDatabase.xvb16.mongodb.net/sample_database"
COLLECTIONS=("movies" "sessions" "users")
for i in "${COLLECTIONS[@]}"; do
INPUT="$i.json"
mongoimport --uri ${CONNECTION_STRING} --username ${USERNAME} --password ${PASSWORD} --collection $i --file ${INPUT}
done
Final remarks
This blog came from me trying to solve an emergency in which I frantically tried to back up and restore a single collection via mongoimport/mongoexport for an Atlas cluster in which I continuously ran into parsing errors with the URI part of the command line. I hope this blog can help you avoid those scenarios when dealing with a production emergency.
Share this
You May Also Like
These Related Stories
No Comments Yet
Let us know what you think