logo

Vinchin Blog

Vinchin Blog data backup and recovery of MongoDB

data backup and recovery of MongoDB

2021-05-20

1. Data backup operation


In actual application scenarios, it is often necessary to back up business data to prepare for disaster recovery. mongodb provides backup and recovery functions, which are the mongodump.exe and mongorestore.exe files in the MongoDB download directory, and the specific backup command format as follows:


>mongodump -h dbhost -d dbname -o dbdirectory


-h: The address of the server where MongDB is located, for example: 127.0.0.1, of course, you can also specify the port number: 127.0.0.1:27017


-d: The database instance that needs to be backed up, for example: kaiye


-u: mongodb username (add if you have one)


-p: mongodb user password (add if you have one)


-o: The storage location of the backup data, for example: C:\mongodump. Of course, the directory needs to be created in advance. After the backup is completed, the system automatically creates a directory with the name of the backup db under the dump directory, and this directory stores the database instance Backup data.


--AuthenticationDatabase admin (add this sentence when the remote authority is insufficient)


Example: mongodump -h localhost:27017 -u admin -p admin -d kaiye -o C:/mongodump --authenticationDatabase admin


Single table backup:


Mongodump -h localhost:27017 -u admin -p admin --collection collectionName --db databaseName --out dataDirectory


 

2.Data recovery operation


In practical applications, if the database is deleted by mistake due to a machine failure or manual shaking, the backup data needs to be restored at this time. The command format for restoring data is as follows:


>mongorestore -h dbhost -d dbname dbdirectory


-h: the address of the server where MongoDB is located


-d: The database instance that needs to be restored,


-c: the name of the table that needs to be restored


For example: kaiye, of course, the name can also be different from the backup time, such as kaiye


Dbdirectory: the location of the backup data, for example: E:\data\credit, remember the directory where the backed up data is located, the backup directory + "\" + the name of the database that needs to be restored


Example: mongorestore -h localhost -u admin -p admin -d credit E:\data\credit


Single table recovery:


Mongorestore -h localhost -u admin -p admin -c org_info -d credit E:\data\credit\org_info.bson

  • Tag:
  • Trending

Interested Blogs More

DOWNLOAD NOW YOU CAN ENJOY A 60-DAYS FULL-FEATURED FREE TRIAL !