mongodump -h dbhost -d dbname -o dbdir
上述语法中:
-h MongDB所在服务器,可以附带指定端口号‘:port’
-d 需要备份的数据库实例
-o 备份数据存放的位置
如果以上参数都不指定,默认备份127.0.0.1端口号27017的所有数据库到当前的dump目录
C:\Users\Administrator>mongodump
2017-08-06T21:44:20.740+0800 writing admin.system.version to
2017-08-06T21:44:20.822+0800 done dumping admin.system.version (1 document)
2017-08-06T21:44:20.824+0800 writing db1.c1 to
2017-08-06T21:44:20.836+0800 writing db1.c2 to
2017-08-06T21:44:20.844+0800 writing db1.c3 to
2017-08-06T21:44:20.850+0800 done dumping db1.c1 (5 documents)
2017-08-06T21:44:20.852+0800 done dumping db1.c2 (5 documents)
2017-08-06T21:44:20.859+0800 done dumping db1.c3 (0 documents)
C:\Users\Administrator>dir dump
驱动器 C 中的卷没有标签。
卷的序列号是 94AA-6D4F
C:\Users\Administrator\dump 的目录
2017/08/06 21:44 <DIR> .
2017/08/06 21:44 <DIR> ..
2017/08/06 21:44 <DIR> admin
2017/08/06 21:44 <DIR> db1
0 个文件 0 字节
4 个目录 17,894,645,760 可用字节
mongorestore -h hostname:port -d dbname <path>
一些重要的参数
-h[:port] 指定MongoDB所在的服务器地址,默认为localhost
-d 需要恢复的数据库
--drop 恢复的时候先删除当前数据库,然后恢复备份,慎重操作
<path> 设置备份数据的位置,和下边的参数有冲突
--dir 指定备份的目录,和上边的参数有冲突
C:\Users\Administrator>mongorestore --dir ./dump --drop
2017-08-06T21:51:35.869+0800 preparing collections to restore from
2017-08-06T21:51:35.939+0800 reading metadata for db1.c1 from dump\db1\c1.met
adata.json
2017-08-06T21:51:36.047+0800 reading metadata for db1.c2 from dump\db1\c2.met
adata.json
2017-08-06T21:51:36.125+0800 reading metadata for db1.c3 from dump\db1\c3.met
adata.json
2017-08-06T21:51:36.291+0800 restoring db1.c1 from dump\db1\c1.bson
2017-08-06T21:51:36.435+0800 restoring db1.c2 from dump\db1\c2.bson
2017-08-06T21:51:36.559+0800 restoring db1.c3 from dump\db1\c3.bson
2017-08-06T21:51:36.562+0800 no indexes to restore
2017-08-06T21:51:36.565+0800 finished restoring db1.c2 (5 documents)
2017-08-06T21:51:36.567+0800 no indexes to restore
2017-08-06T21:51:36.568+0800 finished restoring db1.c3 (0 documents)
2017-08-06T21:51:36.569+0800 restoring indexes for collection db1.c1 from met
adata
2017-08-06T21:51:36.831+0800 finished restoring db1.c1 (5 documents)
2017-08-06T21:51:36.833+0800 done