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 [[email protected] ~]# 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.

    Example:

[[email protected] ~]# mongodump --host 127.0.0.1:27017 -u mongodb -p mongodb --authenticationDatabase admin --gzip --out /etc/mongodb_databk

Tip: If SSL is enabled, run the mongodump --host <ip>:<port> -u <username> -p <password> --
authenticationDatabase <dbname> --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 --out <filepath> command.

Example:

[[email protected] ~]# mongorestore --host 127.0.0.1:27017 -u mongodb -p mongodb --authenticationDatabase admin --gzip --out /etc/mongodb_databk

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