Backups for MongoDB made easy
Ever accidentally drop something out of a database and realize that you aren't sure if it's in your backups? Need to compare data across different points in time? Or ever want to just look at a subset of your historical data without restoring your entire database? Queryable backup, provided in MongoDB Atlas, can help you address some of these challenges.
Select Your Snapshot
If you've already enabled backup services when you created your cluster, you can go ahead and begin. If not, you can go to the Configuration section of your MongoDB Atlas interface and enable BACKUPS it at any time via the toggle switch.
To get started, you can log into your MongoDB Atlas panel and the "Backup" icon on the left side of your screen. Once you've reached the backup section, you'll find an ellipsis ("...") dropdown menu with some options – select the "Query" option.
After you select Query from this menu, you're given the option to select a specific snapshot in your archive. Select the time frame you'd like to query, then click NEXT.
You'll see a "Query a Snapshot" pane. MongoDB Atlas creates a virtual cluster with your backup snapshot. The virtual cluster provides you with the ability to query data much like you would any other MongoDB cluster using the mongo shell.
Open The Secure Tunnel
The connection to your snapshot will be over TLS/SSL and use an X.509 client certificate for authentication. You have the option of using a secure tunnel binary provided by our team or downloading the required certificates. In this example, we'll use the secure tunnel binary, which is available for Windows, Linux, and MacOS. A simple binary is downloaded when you request a backup query. This tunnel binary establishes a network connection to port 27017, directly to the snapshot you selected earlier.
I'll select OSX and then click "DOWNLOAD BACKUP TUNNEL" which will prompt me for a two factor password authentication request. This is to ensure those who are accessing your backup data are authorized.
Once the download has completed, decompress it with a double click or via the tar command.
Execute the tunnel to establish our connection like this:
bash-3.2$ ./tunnel-58e3eb6a3b34b96d5cd4b3e5
2017/04/04 15:09:49 Starting queryable backup tunnel v1.0.0.100 (9a817429f7e870b42e22e709148f650026a6b572)
2017/04/04 15:09:49 Listening on localhost:27017'
Access Your Backup
Now that a tunnel to our backup snapshot has been established, it's possible now to connect via the mongo
shell.
Open a new terminal window, ensuring you leave the existing connection for your tunnel online. Now you can access your backups using the mongo
shell:
bash-3.2$ mongo
MongoDB shell version v3.4.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.3
MongoDB Enterprise > show databases
admin 0.000GB
zips 0.002GB
zips1 0.002GB
zips2 0.002GB
MongoDB Enterprise > use zips
switched to db zips
MongoDB Enterprise > db.zips.find()
{ "_id" : "01001", "city" : "AGAWAM", "loc" : [ -72.622739, 42.070206 ], "pop" : 15338, "state" : "MA" }
{ "_id" : "01002", "city" : "CUSHMAN", "loc" : [ -72.51565, 42.377017 ], "pop" : 36963, "state" : "MA" }
{ "_id" : "01005", "city" : "BARRE", "loc" : [ -72.108354, 42.409698 ], "pop" : 4546, "state" : "MA" }
Close the Tunnel
When you're done with your backup queries, just exit the session and terminate the binary for the tunnel with a control-c or via killing the PID. You can determine the PID and kill on Linux or MacOS with the following command :
bash-3.2$ ps auxww |egrep tunnel
jaygordon 35312 0.0 0.0 145190748 6580 s001 S+ 12:17PM 0:00.14 /Users/jaygordon/Downloads/tunnel-58e7b6f9d383ad2a1bbf2dbb/tunnel-58e7b6f9d383ad2a1bbf2dbb
bash-3.2$ kill -9 35312
Your MongoDB Atlas backup query session will last for 48 hours and then will close on its own.
Conclusion
Queryable backups are a huge time saver. Imagine wanting to inspect just a single document in a 2TB collection backup.The time associated with downloading the snapshot, decompressing it, getting it running in a local MongoDB node, and finally running the query would be significant. Not only that, but there are obvious nontrivial costs – both monetary and operational – associated with having to quickly spin up new environments. With MongoDB Atlas, there’s no extra time spent provisioning hardware to retrieve your backup data – you simply run the query and get back to coding!