Mongodb 備份與恢復
一、備份數據
[email protected] ~]# mongodump -h localhost -d admin -o /home/qiuyuetao/ 2017-08-10T16:07:51.188+0800writing admin.system.users to 2017-08-10T16:07:51.188+0800done dumping admin.system.users (3 documents) 2017-08-10T16:07:51.189+0800writing admin.system.version to 2017-08-10T16:07:51.190+0800done dumping admin.system.version (2 documents) 2017-08-10T16:07:51.190+0800writing admin.FirstCollection to 2017-08-10T16:07:51.190+0800writing admin.Student to 2017-08-10T16:07:51.191+0800done dumping admin.Student (0 documents) 2017-08-10T16:07:51.191+0800done dumping admin.FirstCollection (1 document)
二、查看數據庫並清空數據
> show dbs admin 0.000GB local 0.000GB mongodb 0.000GB > use admin switched to db admin > db.getCollectionNames() ##查看admin庫下有哪些表 [ "FirstCollection", "Student", "system.users", "system.version" ] > db.FirstCollection.find() #查看表數據 { "_id" : ObjectId("598c0c7f866edf4d34ddfe8e"), "name" : "jack", "age" : 22 } #清空表數據 > db.FirstCollection.remove({name:"jack"}) WriteResult({ "nRemoved" : 1 }) > > db.FirstCollection.find() > >##已經清空數據 > > exit bye [[email protected]
三、恢復數據
[[email protected] ~]# mongorestore -h localhost:27017 -d admin /home/qiuyuetao/admin 2017-08-10T16:13:34.157+0800the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead 2017-08-10T16:13:34.158+0800building a list of collections to restore from /home/qiuyuetao/admin dir 2017-08-10T16:13:34.197+0800reading metadata for admin.FirstCollection from /home/qiuyuetao/admin/FirstCollection.metadata.json 2017-08-10T16:13:34.197+0800restoring admin.FirstCollection from /home/qiuyuetao/admin/FirstCollection.bson 2017-08-10T16:13:34.199+0800reading metadata for admin.Student from /home/qiuyuetao/admin/Student.metadata.json 2017-08-10T16:13:34.207+0800restoring admin.Student from /home/qiuyuetao/admin/Student.bson 2017-08-10T16:13:34.353+0800no indexes to restore 2017-08-10T16:13:34.353+0800finished restoring admin.FirstCollection (1 document) 2017-08-10T16:13:34.353+0800no indexes to restore 2017-08-10T16:13:34.353+0800finished restoring admin.Student (0 documents) 2017-08-10T16:13:34.353+0800restoring users from /home/qiuyuetao/admin/system.users.bson 2017-08-10T16:13:34.787+0800done ##恢復完成 [[email protected]