Backup and Restore Mongo Databases

Complete guide of how to backup and restore a database or a collection from mongodb

Gaurav Mehla
Jan 26, 2018 · 3 min read
Image for post
Image for post

MongoDB is one of the most popular databases we have right now. It is currently been used with a lot of projects. As the usage increases, we start facing issues like taking backup and restoring it. So today, we will learn which tool to use for backup and restore & how to use them.

In this tutorial, we will take hot backup. i.e You can connect to a running instance of MongoDB and take a backup even when users are using the database.

Prerequisites

⚠️ Click Here if you don’t know how to install MongoDB

Image for post
Image for post
MongoDB version I am using

Tools we will use

⚜️ The list

  1. Backup a Single collections
  2. Restore Whole database
  3. Restore Single collection

⚜️ Backup Section

Backup a Whole Database

mongodump -d devdb -o mongoBackups

devdb is the name of the database and mongoBackups is the directory in which I want to dump the database

Backup a Single Collection

mongodump -d devdb -o mongoBackups --collection vehicles

devdb is the name of the database and mongoBackups is the directory in which I want to dump the database and vehicles is the name of the collection which I want to backup.

⚜️ Restore Section

Restore Whole Database

mongorestore -d devdb mongoBackups/devdb

devdb is the name of the database into which we will restore data and mongoBackups is the directory in which we dumped the database devdb

Image for post
Image for post

Restore Single Collection

mongorestore -d devdb -c vehicles devdb/devdb/vehicles.bson

devdb is the name of the database into which we will restore data and vehicles is the name of the collection & then the path of the .bson file.

Image for post
Image for post

🔅 If you have collection in .json then mongorestore will through an error. In that case, you have to use mongoimport i.e :

mongoimport --db devdb --collection vehicles --file vehicles.json --jsonArray

That’s it!!

Happy Coding !!

🎧 Listening to “Two Steps from Hell — Heart of Courage”, Focus… Focus.. & just Focus… Must listen.

Image for post
Image for post

If you like this article, please give it some claps 👏 and share it! If you do not like this post or have any type of questions regarding anything that i mentioned in this post. Feel free to ask me. Just post an issue in my “Ask Me Anything” by clicking here.

For more like this, follow me on Medium or Twitter. To ask a Question visit this link. More about me on my website.

_devblogs

Stories for Full-Stack Web developers which help them in…

Gaurav Mehla

Written by

Software engineer & Web hacker. Spent 30+% of life on playing with JS

_devblogs

_devblogs

Stories for Full-Stack Web developers which help them in pursuing their goals as a developer, mastering the modern web technologies and *hacking the web.

Gaurav Mehla

Written by

Software engineer & Web hacker. Spent 30+% of life on playing with JS

_devblogs

_devblogs

Stories for Full-Stack Web developers which help them in pursuing their goals as a developer, mastering the modern web technologies and *hacking the web.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store