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.
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
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
4.Run the cd /usr/lib/mongodb command to navigate to the /usr/lib/mongodb directory.
5.Run the du -hs data command under the /usr/lib/mongodb directory to check the total size of MongoDB backup data.
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
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.