Mongodb 備份與恢復

一、備份數據

root@m01 ~]# 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
[root@m01 ~]# 
[root@m01 ~]#

三、恢復數據

[root@m01 ~]# 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  ##恢復完成
[root@m01 ~]#mongo 登陸驗證
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.6
Server has startup warnings: 
2017-07-24T14:29:11.787+0800 I STORAGE  [initandlisten] 
2017-07-24T14:29:11.787+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-07-24T14:29:11.787+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-07-24T14:29:12.460+0800 I CONTROL  [initandlisten] 
2017-07-24T14:29:12.460+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-07-24T14:29:12.460+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-07-24T14:29:12.460+0800 I CONTROL  [initandlisten] 
2017-07-24T14:29:12.460+0800 I CONTROL  [initandlisten] 
2017-07-24T14:29:12.461+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-07-24T14:29:12.461+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-07-24T14:29:12.461+0800 I CONTROL  [initandlisten] 
2017-07-24T14:29:12.461+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-07-24T14:29:12.461+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-07-24T14:29:12.461+0800 I CONTROL  [initandlisten] 
2017-07-24T14:29:12.461+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 1024 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2017-07-24T14:29:12.461+0800 I CONTROL  [initandlisten] 
> use admin
switched to db admin
> db.FirstCollection.find()  ##查看錶的內容,已恢復
{ "_id" : ObjectId("598c0c7f866edf4d34ddfe8e"), "name" : "jack", "age" : 22 }

至此,mongodb的備份與恢復 測試完成,有疑問留言。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章