mongodb数据库的备份和恢复

Backup and recovery of mongodb database

Posted by Strom on April 18, 2017

创建测试数据

1.创建db:testdb,collection:user,插入10条记录

1>.启动mongodb:

$ mongod --dbpath=./data/db

2>.启动mongo shell

$ mongo
> use testdb    //创建数据库
switched to db testdb
> db.user.insert({id:1,name:"用户1"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:2,name:"用户2"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:3,name:"用户3"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:4,name:"用户4"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:5,name:"用户5"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:6,name:"用户6"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:7,name:"用户7"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:8,name:"用户8"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:9,name:"用户9"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:10,name:"用户10"});
WriteResult({ "nInserted" : 1 })
>
> db.user.find();

{ "_id" : ObjectId("58f5b37f56a550a7f8a08aa3"), "id" : 1, "name" : "用户1" }
{ "_id" : ObjectId("58f5b38f56a550a7f8a08aa4"), "id" : 2, "name" : "用户2" }
{ "_id" : ObjectId("58f5b39956a550a7f8a08aa5"), "id" : 3, "name" : "用户3" }
{ "_id" : ObjectId("58f5b3a256a550a7f8a08aa6"), "id" : 4, "name" : "用户4" }
{ "_id" : ObjectId("58f5b3a956a550a7f8a08aa7"), "id" : 5, "name" : "用户5" }
{ "_id" : ObjectId("58f5b3b156a550a7f8a08aa8"), "id" : 6, "name" : "用户6" }
{ "_id" : ObjectId("58f5b3b856a550a7f8a08aa9"), "id" : 7, "name" : "用户7" }
{ "_id" : ObjectId("58f5b3c056a550a7f8a08aaa"), "id" : 8, "name" : "用户8" }
{ "_id" : ObjectId("58f5b3c856a550a7f8a08aab"), "id" : 9, "name" : "用户9" }
{ "_id" : ObjectId("58f5b3d456a550a7f8a08aac"), "id" : 10, "name" : "用户10" }

2.数据导出 mongoexport

参数说明:

-d     数据库名
-c     collection名
-o     输出的文件名
--type 输出的格式,默认为json
-f     输出的字段,如果--type为csv,则需要加上 -f "字段名"

更多参数说明可参考 mongoexport –help

例子:导出user所有记录到~/myworks/mongodb/user.json

$ mongoexport -d testdb -c user -o myworks/mongodb/user.json


2017-04-18T14:45:25.428+0800	connected to: localhost
2017-04-18T14:45:25.428+0800	exported 10 records

$ cat /tmp/user.json

会看到:

{"_id":{"$oid":"58f5b37f56a550a7f8a08aa3"},"id":1.0,"name":"用户1"}
{"_id":{"$oid":"58f5b38f56a550a7f8a08aa4"},"id":2.0,"name":"用户2"}
{"_id":{"$oid":"58f5b39956a550a7f8a08aa5"},"id":3.0,"name":"用户3"}
{"_id":{"$oid":"58f5b3a256a550a7f8a08aa6"},"id":4.0,"name":"用户4"}
{"_id":{"$oid":"58f5b3a956a550a7f8a08aa7"},"id":5.0,"name":"用户5"}
{"_id":{"$oid":"58f5b3b156a550a7f8a08aa8"},"id":6.0,"name":"用户6"}
{"_id":{"$oid":"58f5b3b856a550a7f8a08aa9"},"id":7.0,"name":"用户7"}
{"_id":{"$oid":"58f5b3c056a550a7f8a08aaa"},"id":8.0,"name":"用户8"}
{"_id":{"$oid":"58f5b3c856a550a7f8a08aab"},"id":9.0,"name":"用户9"}
{"_id":{"$oid":"58f5b3d456a550a7f8a08aac"},"id":10.0,"name":"用户10"}


例子:导出user所有id到/tmp/user.csv

格式为csv但不指定字段会出错

$ mongoexport -d testdb -c user --type csv -o myworks/mongodb/user.csv

报错如下:

2017-04-18T14:49:40.369+0800	Failed: CSV mode requires a field list

正确操作方法

$ mongoexport -d testdb -c user --type csv -f "id" -o myworks/mongodb/user.csv

2017-04-18T14:51:24.473+0800	connected to: localhost
2017-04-18T14:51:24.474+0800	exported 10 records

$ cat myworks/mongodb/user.csv

展示如下:

id
1
2
3
4
5
6
7
8
9
10

3.数据导入 mongoimport

参数说明:

-d           数据库名
-c           collection名
--type       导入的格式,默认json
-f           导入的字段名
--headerline 如果导入的格式是csv,则可以使用第一行的标题作为导入的字段
--file       要导入的文件

更多参数说明可参考 mongoimport –help

导入前先清空collection user

> db.user.drop();
true
> db.user.find();
>

例子:把上例导出的user.json导入

$ mongoimport -d testdb -c user --file myworks/mongodb/user.json

2017-04-18T15:05:56.223+0800	connected to: localhost
2017-04-18T15:05:56.545+0800	imported 10 documents

例子:把上例导出的user.csv导入

$ mongoimport -d testdb -c user --type csv --headerline --file myworks/mongodb/user.csv

2017-04-18T15:10:41.097+0800	connected to: localhost
2017-04-18T15:10:41.327+0800	imported 10 documents

4.数据备份 mongodump

参数说明:

-d 数据库名
-c collection名
-o 备份的文件路径

更多参数说明可参考 mongodump –help

例子:把testdb的user备份到 ~/myworks/mongodb/tmp

$ mongodump -d testdb -c user -o myworks/mongodb/tmp
2017-04-18T15:16:03.677+0800	writing testdb.user to
2017-04-18T15:16:03.677+0800	done dumping testdb.user (10 documents)


5.数据恢复 mongorestore

参数说明:

-d 数据库名
-c collection名

更多参数说明可参考 mongorestore –help

导入前先清空collection user

> db.user.drop();
true
> db.user.find();
>

例子:把上例备份的数据恢复

$ mongorestore -d testdb -c user myworks/mongodb/tmp/testdb/user.bson

2017-04-18T15:22:52.652+0800	checking for collection data in myworks/mongodb/tmp/testdb/user.bson
2017-04-18T15:22:52.652+0800	reading metadata for testdb.user from myworks/mongodb/tmp/testdb/user.metadata.json
2017-04-18T15:22:53.019+0800	restoring testdb.user from myworks/mongodb/tmp/testdb/user.bson
2017-04-18T15:22:53.086+0800	no indexes to restore
2017-04-18T15:22:53.086+0800	finished restoring testdb.user (10 documents)
2017-04-18T15:22:53.086+0800	done