一、备份数据

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的备份与恢复 测试完成,有疑问留言。