MongoDB is a popular NoSQL database known for its flexibility, scalability, and ease of use. However, to protect our data from potential data loss or corruption, it’s critical to have a reliable MongoDB backup strategy in place.
In this article, we will go through the process of creating a MongoDB dump using the powerful mongodump
tool, ensuring that our MongoDB database backup is secure and easy to restore in case of failure.
How to Create a MongoDB Dump of Database?
Data loss or corruption can occur for various reasons such as hardware failure, human error, or software bugs. Regularly backing up our MongoDB database is essential to minimize these risks. One of the best ways to backup your MongoDB database is by creating a MongoDB dump using the mongodump
tool. Below are the steps that help us to create a backup for an entire server are as follow:
- Create Direct Backups Using Mongodump
- Backup a Remote MongoDB Instance
- Backup a Secure MongoDB Instance
- Select Databases & Collections
- Change the Backup Directory
- Create an Archive File
- Compress the MongoDB Backup
- Restore Database
Step 1: Create Direct Backups Using Mongodump
We can run the mongodump command using the below syntax from the system command line as follows:
mongodump <options> <connection-string>
This structure also permits us to connect to a Mongo database with the –uri command along with a formatted string or flag such as –user, –db, and –password. However, we can’t use more than 1 flag in a single command.
Alternatively, we can use the default configurations and create a Mongo Backup via Mongodump command as follows:
Mongodump
This statement operates on the assumption that your database resides in localhost (127.0.0.1), uses port 27017, and requires no access authentication. This backup process creates a dump folder directly in the current directory as shown in the below image.
mongodumpStep 2: Backup a Remote MongoDB Instance
As mentioned in Step 1, you can customize a host and a port number with the –uri connection string using the following syntax:
mongodump --uri="mongodb://<host URL/IP>:<Port>" [additional options]
Moreover, you can set up a server connection with the host option using the following command:
mongodump --host="<host URL/IP>:<Port>" [additional options]
If you also wish to specify the port number, implement the following statement:
mongodump --host="<host URL/IP>" --port=<Port> [additional options]
The following code demonstrates the process of backing up a remote MongoDB instance:
mongodump --host="10.10.10.59" --port=27017
Output:
OutputStep 3: Backup a Secure MongoDB Instance
MongoDB’s Mongodump command allows you to implement access control mechanisms for your data backups. This will require you to provide a Username, Password, and specific Authentication options in the following syntax:
mongodump --authenticationDatabase=<Database> -u=<Username> -p=<Password> [additional options
For instance, we can use a Username & Password to connect to a remote MongoDB instance using the following command:
mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword"
Output:
OutputStep 4: Select Databases & Collections
In Step 3 you learned how to back up a remote database using Mongodump. This step involves the –db and –collection options that indicate a database and a collection that requires backing up. You can run the –db option in a standalone manner, but to execute a collection, you have to specify a database. Moreover, you can remove a collection from the backup process, by using the –excludeCollection option.
To select a particular database use the following command:
mongodump --db=<Backup Target - Database> [additional options]
If you wish to select a whole collection, execute the following:
mongodump --db=<Backup Target - Database> --collection=<Collection Name> [additional options]
To exclude a particular collection, run the following command:
mongodump --db=<Backup Target - Database> --excludeCollection=<Collection Name> [additional options]
Step 5: Change the Backup Directory
We can use the –out option to specify the backup folder’s location in the following way:
mongodump --out=<Directory Location> [additional options]
Now, if we wish to switch the backup directory with the “dbbackup” folder, run:
mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" --out=dbbackup
Output:
BackupDirectoryStep 6: Create an Archive File
The Mongodump utility provides us with a method to create an archive file. We can use the –archive option for specifying the file to be archived. In case no specification is given, the output will be in the standard form (stdout).
Keep in mind that you can’t use the –archive option along with the –out option.
mongodump --archive=<file> [additional options]
You can easily define any archive file using the following command:
mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" --archive=db.archive
Output:
OutputStep 7: Compress the MongoDB Backup
Now, since you know how to backup data using Mongodump, it’s time to understand the process of compressing these files. You can use the –gzip option to compress the JSON and BSON files individually using the following command:
mongodump --gzip [additional options]
For instance, the below statement will compress a complete Mongo database:
mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" --gzip
Output:
OutputStep 8: Restore Database
Apart from excellent backups, MongoDB also provides us the facility to restore our data. The Mongorestore command will seamlessly load data from the Mongodumb backups and restore our Mongo database. The mongorestore command, however, can not overwrite documents present in the database if the id for the document already exists. Otherwise, Mongorestore will generate a new database or will add data to the existing one.
The Mongorestore command requires you to mention the path to your dump directory and the syntax is s follows:
mongorestore dump/
Moreover, in the Mongostore command:
- We must specify the –uri flag or provide every standard connection flag.
- We can directly use the most preferred –nsInclude option to restore different collections. It allows users to choose a namespace when restoring the collections for a database.
For instance, in the following command, you can isolate and import the db1 database to restore it in your local environment:
mongorestore --db=redbase --nsInclude="db1.*" dump/
This will restore all the collections of db1 that were earlier dumped. However, it won’t touch the data stored in db2 even both dn1 and db2 have their data stored in the same dump directory.
Benefits of Using Mongodump for MongoDB Backup
The mongodump
tool is a powerful and flexible way to create MongoDB backups. Some benefits of using mongodump
include:
- Efficient Data Backups: Whether you want to back up a single collection, query, or an entire MongoDB database,
mongodump
supports various backup strategies.
- Oplog Support: By including the oplog file, you can create a point-in-time snapshot of your MongoDB data for easy recovery.
- Small-Scale Backups:
mongodump
works exceptionally well for small MongoDB databases, and with the oplog feature, it’s ideal for incremental backups.
Conclusion
In conclusion, backing up your MongoDB database is a crucial part of database management, ensuring data safety and integrity. The mongodump tool makes it simple to create secure, efficient, and scalable MongoDB database backups, whether you're dealing with local, remote, or secure instances. Regularly backing up your data with MongoDB dumps will protect you from data loss and help ensure smooth recovery when needed.
Similar Reads
How to Create a MongoDB Dump of Database?
MongoDB is a popular NoSQL database known for its flexibility, scalability, and ease of use. However, to protect our data from potential data loss or corruption, itâs critical to have a reliable MongoDB backup strategy in place. In this article, we will go through the process of creating a MongoDB d
7 min read
How to Connect Node to a MongoDB Database ?
Connecting Node.js to MongoDB is a common task for backend developers working with NoSQL databases. MongoDB is a powerful, flexible, and scalable database that stores data in a JSON-like format. In this step-by-step guide, we'll walk through the entire process from setting up your development enviro
6 min read
How to Create Database and Collection in MongoDB
MongoDB is a widely used NoSQL database renowned for its flexibility, scalability, and performance in managing large volumes of unstructured data. Whether youâre building a small application or handling big data, MongoDB offers an easy-to-use structure for managing your data. In this article, we wil
6 min read
How to create new Mongodb database using Node.js ?
mongodb module: This Module is used to performing CRUD(Create Read Update Read) Operations in MongoDb using Node.js. We cannot make a database only. We have to make a new Collection to see the database. The connect() method is used for connecting the MongoDb server with the Node.js project. Please r
1 min read
Create a database in MongoDB using Python
MongoDB is a general-purpose, document-based, distributed database built for modern application developers and the cloud. It is a document database, which means it stores data in JSON-like documents. This is an efficient way to think about data and is more expressive and powerful than the traditiona
2 min read
MongoDB - Create Database using Mongo Shell
MongoDB is a popular NoSQL database that uses collections and documents, which are highly flexible and scalable. Unlike relational databases (RDBMS), MongoDB does not use tables and rows but stores data in a more dynamic, JSON-like format. In this article, we'll explore how to create a MongoDB datab
4 min read
How to Connect to a MongoDB Database Using Node.js
MongoDB is a NoSQL database used to store large amounts of data without any traditional relational database table. To connect to a MongoDB database using NodeJS we use the MongoDB library "mongoose". Steps to Connect to a MongoDB Database Using NodeJSStep 1: Create a NodeJS App: First create a NodeJ
4 min read
How to drop database of MongoDB using Node.js ?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo
2 min read
How to Back Up and Restore a MongoDB Database?
MongoDB is considered one of the classic examples of NoSQL systems. Its documents are made up of key-value pairs, which are the basic unit of data in MongoDB. Whether we're dealing with accidental data loss, hardware failures, or other unforeseen issues, having a solid backup and restoration plan ca
5 min read
Create Database using MongoDB Compass
MongoDB is a popular NoSQL database that uses a document-oriented storage model, differing from traditional relational databases. Instead of storing data in tables with rows and columns, MongoDB stores data as documents in BSON format. MongoDB Compass is a graphical user interface (GUI) that allows
6 min read