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 to copy data files from the MongoDB directly.

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 following command to create a directory under the /etc directory to save the backup data.

[root@localhost ~]# mkdir /etc/mongodb_databk

3.Enter the following command in one line and run it to dump the MongoDB data to the /etc/mongodb_databk directory.

For IEv7.x, run the mongodump --host <ip>:<port> -u <username> -p <password> --authenticationDatabase <database_name> --gzip -out <filepath> command.

For IEv8.0, run the mongodump --host <ip>:<port> -u <username> -p <password> --authenticationDatabase <database_name> --authenticationMechanism SCRAM-SHA-256 --gzip -out <filepath> command.

    Example:

[root@localhost ~]# mongodump --host 127.0.0.1 --port 27017 -u mongodb -p mongodb
--authenticationDatabase admin --gzip --out /etc/mongodb_databk

Tip: If SSL is enabled, run the following command:

- For IEv7.x, run the mongodump --host <ip>:<port> -u <username> -p <password> --authenticationDatabase <dbname> --ssl --sslAllowInvalidCertificates --gzip -out <filepath> command.

- For IEv8.0, 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 mongodump command.

Note: Make sure other relevant services are stopped

Enter the following command in one line and run it to dump the MongoDB data to the /etc/mongodb_databk directory.

For IEv7.x, run the mongorestore --host <ip>:<port> -u <username> -p <password> --authenticationDatabase <database_name> --gzip <filepath> command.

For IEv8.0, run the mongodump --host <ip>:<port> -u <username> -p <password> --authenticationDatabase <database_name> --authenticationMechanism SCRAM-SHA-256 --gzip -out <filepath> command.

Example:

[root@localhost ~]# mongodump --host 127.0.0.1 --port 27017 -u mongodb -p mongodb
--authenticationDatabase admin --gzip --out /etc/mongodb_databk

Tip: If SSL is enabled, run the following command:

- For IEv7.x, run the mongorestore --host <ip>:<port> -u <username> -p <password> --authenticationDatabase <dbname> --ssl --sslAllowInvalidCertificates --gzip <filepath> command.

- For IEv8.0, run the mongorestore --host <ip>:<port> -u <username> -p <password> --authenticationDatabase <dbname> --ssl --sslAllowInvalidCertificates --authenticationMechanism SCRAM-SHA-256 --gzip <filepath> command.