Before upgrading the system, it is highly recommended to back up all MongoDB data in case of any data loss or corruption during the upgrade process. The backup data will be used to restore data after MongoDB is reinstalled. See Restoring MongoDB Data for more details.

In case that you don’t want to stop the service of MongoDB or the volume of the MongoDB data is small, you can refer to Dumping MongoDB Data for another way to back up the data and refer to Restoring Dumped MongoDB Data to restore the dumped data.

The following steps introduces how to use the cp command to copy underlying MongoDB data files directly for backup.


– Make sure you have stopped all relevant services before backing up data.

– The backup data can only be used to restore the database on the same server. If you have set up a MongoDB replica set for high availability, repeat the steps below on each MongoDB node except for the arbiter node.

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

2.Stop the service of MongoDB.

1)Run the systemctl stop mongod command to stop the MongoDB service.

2)Run the ps -ef|grep mongod command to verify whether the mongod process is stopped.

[[email protected] ~]# ps -ef| grep mongod
root     15136 14237  0 10:42 pts/2    00:00:00 grep --color=auto mongod

Note: If the mongod process is stopped, the result should only contain one entry as shown above.

3.Run the mkdir /etc/mongodb_databk command to create a directory under the /etc directory to save the backup data.

[[email protected] ~]# mkdir /etc/mongodb_databk

Note: Ensure the backup directory (/etc/mongodb_databk in this example) has sufficient space to store the backup data.

4.Run the cd /usr/lib/mongodb command to navigate to the /usr/lib/mongodb directory.

Note: If you modified the following default directory to store all MongoDB data files during the MongoDB installation, you must use the new directory (available in mongod.conf) accordingly.  For a freshly installed system, the default directory is /usr/lib/mongodb.

5.Run the du -hs data command under the /usr/lib/mongodb directory to double-check the total size of MongoDB data to ensure the target folder (/etc/mongodb_databk in this example) has sufficient space.

6.Run the cp -a data /etc/mongodb_databk command under the /usr/lib/mongodb directory to copy all MongoDB data files from the data directory to the /etc/mongodb_databk directory.

[[email protected] mongodb]# cp -a data /etc/mongodb_databk

7.Run the cd /etc/mongodb_databk command to navigate to the /etc/mongodb_databk directory.

8.Run the ls -al command under the /etc/mongodb_databk directory to browse the backup data.

[[email protected] mongodb_databk]# ls -al
total 136
drwxr-xr-x.  3 root root         18 Jun 6 22:49 .
drwxr-xr-x.  6 root root         79 Jun 6 22:48 ..
drwxr-xr-x.  4 netbrain netbrain 106496 Jun 6 22:49 data

9.Run the systemctl start mongod command to start the MongoDB service.