The built-in MongoDB command mongodump is a simple and efficient tool for backing up a small volume of MongoDB data. However, for a large volume of data, it is more time-consuming than using the cp command introduced in Backing Up MongoDB Data.

Note: Make sure the service of MongoDB is running when you run the mongodump command.

Note: The dumped data can be used to restore data in any server. If you have set up a MongoDB replica set for high availability, you only need to dump data from the primary node.

1.Log in to the Linux server where the MongoDB is installed as root user.

2.Open a command prompt and run the [root@localhost ~]# mkdir /etc/mongodb_databk command to create a directory under the /etc directory to save the backup data.

3.Run the mongodump --host <ip>:<port> -u <username> -p <password> --authenticationDatabase
 <database_name> --authenticationMechanism SCRAM-SHA-256 --gzip --out <filepath> command to dump the MongoDB data to the /etc/mongodb_databk directory.

Note: The <database_name> mentioned in the above command must be admin for NetBrain.

    Example:

[root@localhost ~]# mongodump --host 127.0.0.1:27017 -u mongodb -p mongodb --authenticationDatabase admin --authenticationMechanism SCRAM-SHA-256 --gzip --out /etc/mongodb_databk

Tip: If SSL is enabled, run the mongodump --host <ip>:<port> -u <username> -p <password> --
authenticationDatabase <database_name> --ssl --sslAllowInvalidCertificates --authenticationMechanism
 SCRAM-SHA-256 --gzip --out <filepath> command.

4.Verify the backup result.

1)Run the cd /etc/mongodb_databk command to navigate to the /etc/mongodb_databk directory.

2)Run the ls -al command under the mongodb_databk directory to browse the backup data.

Restoring Dumped MongoDB Data

Restore the dumped data by using the mongorestore command provided by MongoDB.

Note: Make sure the service of MongoDB is running when you run the mongorestore command.

Note: Make sure other relevant services are stopped.

To restore the dumped MongoDB data onto the MongoDB server, run the mongorestore --host <ip>:<port> -u <username> -p <password> --authenticationDatabase <database_name> --authenticationMechanism
 SCRAM-SHA-256 --gzip <filepath> command.

Note: The <database_name> mentioned in the above command must be admin for NetBrain.

Example:

[root@localhost ~]# mongorestore --host 127.0.0.1:27017 -u mongodb -p mongodb --authenticationDatabase admin --authenticationMechanism SCRAM-SHA-256 --gzip /etc/mongodb_databk

Tip: If SSL is enabled, run the mongorestore --host <ip>:<port> -u <username> -p <password> --authenticationDatabase <database_name> --ssl --sslAllowInvalidCertificates --authenticationMechanism SCRAM-SHA-256 --gzip <filepath> command.

Note: The <database_name> mentioned in the above command must be admin for NetBrain.