MongoDB 数据库备份 与 恢复

  • 本文只讲 MongoDB 数据库的备份与恢复,其余内容不清楚的可以参考《MongoDB》其它章节。

MongoDB 数据备份

  • MongoDB 提供了数据备份和恢复的功能,分别是 MongoDB 下载目录 bin 目录下的 mongodump.exe 和 mongorestore.exe文件
  • 备份数据命令:mongodump -h dbhost -d dbname -o dbdirectory

-hMongDB 所在的服务器地址,例如:127.0.0.1,当然也可以同时指定端口号:127.0.0.1:27017

-d:需要备份的数据库实例,例如:test

-o:备份的数据存放位置,例如:d:\data\dump,目录不存在时会自动新建,备份完成后,dump 目录下会建立一个 test 目录,里面存放该数据库实例的备份数据。

  • 不同的 mongoDB 版本,参数选项可能不一致,可以在 cmd 命令行中输入 mongodump --help 进行查看
C:\Users\Administrator.SC-201707281232>mongodump --help
Usage:
  mongodump <options>

Export the content of a running server into .bson files.

Specify a database with -d and a collection with -c to only dump that database or collection.

See http://docs.mongodb.org/manual/reference/program/mongodump/ for more information.

general options:
      /help                                                 print usage
      /version                                              print the tool
                                                            version and exit

verbosity options:
  /v, /verbose:<level>                                      more detailed log
                                                            output (include
........
  • 因为是直接调用 mongodump.exe 与 mongorestore.exe 程序进行操作,所以直接在 cmd 命令行中使用即可,不需要登录 MongoDB 数据库
  • 如下所示,mongoDB 中现在有 5 个数据库实例,其中 mydb1 数据库中有 100 条数据,现在以备份 mydb1 数据库为例。
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
mydb1   0.000GB
mydb2   0.186GB
> db
mydb1
> db.c1.find().count()
100
> db.c1.find()
{ "_id" : ObjectId("5b98b375c03ec6172729490d"), "name" : "zhangSan", "age" : 1 }
{ "_id" : ObjectId("5b98b375c03ec6172729490e"), "name" : "zhangSan", "age" : 2 }
{ "_id" : ObjectId("5b98b375c03ec6172729490f"), "name" : "zhangSan", "age" : 3 }
{ "_id" : ObjectId("5b98b375c03ec61727294910"), "name" : "zhangSan", "age" : 4 }
{ "_id" : ObjectId("5b98b375c03ec61727294911"), "name" : "zhangSan", "age" : 5 }
{ "_id" : ObjectId("5b98b375c03ec61727294912"), "name" : "zhangSan", "age" : 6 }
{ "_id" : ObjectId("5b98b375c03ec61727294913"), "name" : "zhangSan", "age" : 7 }
{ "_id" : ObjectId("5b98b375c03ec61727294914"), "name" : "zhangSan", "age" : 8 }
{ "_id" : ObjectId("5b98b375c03ec61727294915"), "name" : "zhangSan", "age" : 9 }
{ "_id" : ObjectId("5b98b375c03ec61727294916"), "name" : "zhangSan", "age" : 10 }
{ "_id" : ObjectId("5b98b375c03ec61727294917"), "name" : "zhangSan", "age" : 11 }
{ "_id" : ObjectId("5b98b375c03ec61727294918"), "name" : "zhangSan", "age" : 12 }
{ "_id" : ObjectId("5b98b375c03ec61727294919"), "name" : "zhangSan", "age" : 13 }
{ "_id" : ObjectId("5b98b375c03ec6172729491a"), "name" : "zhangSan", "age" : 14 }
{ "_id" : ObjectId("5b98b375c03ec6172729491b"), "name" : "zhangSan", "age" : 15 }
{ "_id" : ObjectId("5b98b375c03ec6172729491c"), "name" : "zhangSan", "age" : 16 }
{ "_id" : ObjectId("5b98b375c03ec6172729491d"), "name" : "zhangSan", "age" : 17 }
{ "_id" : ObjectId("5b98b375c03ec6172729491e"), "name" : "zhangSan", "age" : 18 }
{ "_id" : ObjectId("5b98b375c03ec6172729491f"), "name" : "zhangSan", "age" : 19 }
{ "_id" : ObjectId("5b98b375c03ec61727294920"), "name" : "zhangSan", "age" : 20 }
Type "it" for more
>
  • 如下所示 mongodump -h 127.0.0.1:27017 -d mydb1 -o E:/wmx/mongoDump 进行备份 mydb1 数据库

-h :指定 mongoDB 服务端 IP 地址 与 端口,端口不指定时,默认为 27017

-d:需要备份的 MongoDB 数据库实例

-o:备份的数据存放位置,目录不存在时,会自动新建,此目录下会再有一个与 数据库实例同名的子目录用来正式存放数据文件

  • 注意备份、恢复 命令直接在 cmd 命令行中操作,不需要登录 MongoDB ,所以如果之前登录了,则 exit 退出。
{ "_id" : ObjectId("5b98b375c03ec6172729491f"), "name" : "zhangSan", "age" : 19 }
{ "_id" : ObjectId("5b98b375c03ec61727294920"), "name" : "zhangSan", "age" : 20 }
Type "it" for more
> exit
bye

C:\Users\Administrator.SC-201707281232>mongodump -h 127.0.0.1:27017 -d mydb1 -o E:/wmx/mongoDump
2018-09-12T14:40:57.719+0800    writing mydb1.c1 to
2018-09-12T14:40:57.805+0800    done dumping mydb1.c1 (100 documents)

C:\Users\Administrator.SC-201707281232>

  • 接着再次登录 MongoDB 数据库删除原来的 mydb1 数据库,为后来的数据库恢复做准备,如下所示 mydb1 数据库删除成功
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
mydb1   0.000GB
mydb2   0.186GB
> use mydb1
switched to db mydb1
> db.dropDatabase()
{ "dropped" : "mydb1", "ok" : 1 }
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
mydb2   0.186GB
>

MongoDB 数据恢复

  • 恢复数据命令:mongorestore -h dbhost -d dbname /dir dbdirectory

-hMongoDB 所在服务器 IP 地址

-d:需要恢复的数据库实例,例如:test,这个名称也可以和备份时候的不一样,比如 test2、test3 ,自定义即可

/dir:备份数据所在位置,例如:c:\data\dump\test ,路径要指定到数据库目录

  • 不同的 mongoDB 版本,参数选项可能不一致,可以在 cmd 命令行中输入 mongorestore --help 进行查看
C:\Users\Administrator.SC-201707281232>mongorestore --help
Usage:
  mongorestore <options> <directory or file to restore>

Restore backups generated with mongodump to a running server.

Specify a database with -d to restore a single database from the target directory,
or use -d and -c to restore a single collection from a single .bson file.

See http://docs.mongodb.org/manual/reference/program/mongorestore/ for more information.

general options:
      /help                                                 print usage
      /version                                              print the tool
                                                            version and exit

verbosity options:
  /v, /verbose:<level>                                      more detailed log
                                                            output (include
                                                            multiple times for
                                                            more verbosity,
                                                            e.g. -vvvvv, or
...........
  • 上面已经手动删除了 MongoDB 中的 mydb1 数据库实例,现在开始恢复上面备份好的 E:/wmx/mongoDump/mydb1 数据库
  • 同理在 cmd 命令行中直接操作,不用登陆 MongoDB
  • 如下所示,mongorestore -h 127.0.0.1:27017 -d mydb1 /dir E:/wmx/mongoDump/mydb1 进行数据恢复

/h :MongoDB 服务器所在的 IP 地址,端口不写时默认为 27017

-d:需要恢复的数据库实例,名称自定义即可

/dir:备份数据所在位置,指定到数据库目录

C:\Users\Administrator.SC-201707281232>mongorestore -h 127.0.0.1:27017 -d mydb1 /dir E:/wmx/mongoDump/mydb1
2018-09-12T15:38:02.707+0800    the --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
2018-09-12T15:38:02.738+0800    building a list of collections to restore from E:\wmx\mongoDump\mydb1 dir
2018-09-12T15:38:02.739+0800    reading metadata for mydb1.c1 from E:\wmx\mongoDump\mydb1\c1.metadata.json
2018-09-12T15:38:03.129+0800    restoring mydb1.c1 from E:\wmx\mongoDump\mydb1\c1.bson
2018-09-12T15:38:03.131+0800    no indexes to restore
2018-09-12T15:38:03.131+0800    finished restoring mydb1.c1 (100 documents)
2018-09-12T15:38:03.131+0800    done

C:\Users\Administrator.SC-201707281232>
  • 登陆 MongoDB 进行查看,如下所示,数据恢复成功
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
mydb1   0.000GB
mydb2   0.186GB
> use mydb1
switched to db mydb1
> show tables
c1
> db.c1.find().count()
100
> db.c1.find()
{ "_id" : ObjectId("5b98bc379253fbe383c9f04e"), "name" : "zhangSan1", "age" : 1 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f04f"), "name" : "zhangSan2", "age" : 2 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f050"), "name" : "zhangSan3", "age" : 3 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f051"), "name" : "zhangSan4", "age" : 4 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f052"), "name" : "zhangSan5", "age" : 5 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f053"), "name" : "zhangSan6", "age" : 6 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f054"), "name" : "zhangSan7", "age" : 7 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f055"), "name" : "zhangSan8", "age" : 8 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f056"), "name" : "zhangSan9", "age" : 9 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f057"), "name" : "zhangSan10", "age" : 10 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f058"), "name" : "zhangSan11", "age" : 11 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f059"), "name" : "zhangSan12", "age" : 12 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f05a"), "name" : "zhangSan13", "age" : 13 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f05b"), "name" : "zhangSan14", "age" : 14 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f05c"), "name" : "zhangSan15", "age" : 15 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f05d"), "name" : "zhangSan16", "age" : 16 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f05e"), "name" : "zhangSan17", "age" : 17 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f05f"), "name" : "zhangSan18", "age" : 18 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f060"), "name" : "zhangSan19", "age" : 19 }
{ "_id" : ObjectId("5b98bc379253fbe383c9f061"), "name" : "zhangSan20", "age" : 20 }
Type "it" for more
>

 

 

 

版权声明:本文为wangmx1993328原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wangmx1993328/article/details/82660876