Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save PrinceSinghhub/385efae769fdad5040e565f1c9dc65e6 to your computer and use it in GitHub Desktop.

Select an option

Save PrinceSinghhub/385efae769fdad5040e565f1c9dc65e6 to your computer and use it in GitHub Desktop.
MongoDB Backup and Restore with mongodump and mongorestore

MongoDB Backup and Restore with mongodump and mongorestore

MongoDB provides convenient tools for creating backups and restoring data, essential for managing your databases effectively. In this article, we'll explore the use of mongodump for creating a database dump and mongorestore for restoring the data.

Creating a Database Dump with mongodump

The mongodump command allows you to create a dump of your MongoDB database. Here's the basic syntax:

mongodump --uri "mongodb+srv://<username>:<password>@cluster.mongodb.net/<dbname>"
  • <username>: Your MongoDB Atlas username.
  • <password>: Your MongoDB Atlas password.
  • <dbname>: The name of the database you want to dump. Replace these placeholders with your actual credentials and database name. The command will create a dump of the specified database in the current working directory.

Restoring Data with mongorestore

Once you have a database dump, you can use mongorestore to restore the data into a new or existing database. Here's an example command:

mongorestore --uri "mongodb+srv://<username>:<password>@cluster.mongodb.net/" --nsFrom "sourcedb.*" --nsTo "destinationdb.*" dump/
  • <username>: Your MongoDB Atlas username.
  • <password>: Your MongoDB Atlas password.
  • --nsFrom "sourcedb.*": Specifies the namespace pattern of the source database collections.
  • --nsTo "destinationdb.*": Specifies the namespace pattern for the destination database collections.
  • dump/: The path to the source database dump.

Replace the placeholders with your actual credentials, database names, and the path to your dump. This command will restore the data from the source database into the destination database.

Conclusion

Backing up and restoring MongoDB databases is a crucial aspect of database management. The combination of mongodump and mongorestore provides a reliable way to safeguard your data and migrate it between databases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment