数据库的备份和恢复
数据库的备份(不带参数的话,将所有的数据库备份):
mongodump -h dbhost:port -d dbname -o dbdirectory
D:\Program Files\MongoDB\Server\4.0\bin>mongodump.exe -h localhost:27001 -d mldn -o D:\dump
2018-09-10T10:00:17.786+0800 writing mldn.deptcol to
2018-09-10T10:00:17.835+0800 writing mldn.stucol to
2018-09-10T10:00:17.837+0800 writing mldn.students3 to
2018-09-10T10:00:17.845+0800 writing mldn.emps to
2018-09-10T10:00:17.938+0800 done dumping mldn.stucol (999 documents)
2018-09-10T10:00:17.938+0800 writing mldn.students2 to
2018-09-10T10:00:17.974+0800 done dumping mldn.students2 (8 documents)
2018-09-10T10:00:17.975+0800 writing mldn.students to
2018-09-10T10:00:18.003+0800 done dumping mldn.deptcol (3976 documents)
2018-09-10T10:00:18.003+0800 writing mldn.shops to
2018-09-10T10:00:18.012+0800 done dumping mldn.students (7 documents)
2018-09-10T10:00:18.012+0800 writing mldn.news to
2018-09-10T10:00:18.035+0800 done dumping mldn.shops (7 documents)
2018-09-10T10:00:18.035+0800 writing mldn.testCollection to
2018-09-10T10:00:18.080+0800 done dumping mldn.testCollection (5 documents)
2018-09-10T10:00:18.080+0800 writing mldn.t_job_emps to
2018-09-10T10:00:18.088+0800 done dumping mldn.news (5 documents)
2018-09-10T10:00:18.088+0800 writing mldn.dept to
2018-09-10T10:00:18.111+0800 done dumping mldn.t_job_emps (3 documents)
2018-09-10T10:00:18.112+0800 writing mldn.phones to
2018-09-10T10:00:18.143+0800 done dumping mldn.dept (3 documents)
2018-09-10T10:00:18.164+0800 done dumping mldn.phones (0 documents)
2018-09-10T10:00:18.873+0800 done dumping mldn.students3 (8 documents)
2018-09-10T10:00:18.890+0800 done dumping mldn.emps (8 documents)
备份成功
数据库的恢复(恢复数据库的时候,不能出现同名的数据库,有的话需要删除)
mongorestore -h dbhost:port -d dbname dbdirectory --drop
dbnam恢复的数据库之后的名称,dbdirectory恢复数据库所在的目录
带上参数drop的意思,如果存在相同的数据库,删除重新建立。
D:\Program Files\MongoDB\Server\4.0\bin>mongorestore.exe -h localhost:27001 -d mldn D:\dump\mldn --drop
2018-09-10T10:10:55.065+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-10T10:10:55.075+0800 building a list of collections to restore from D:\dump\mldn dir
2018-09-10T10:10:55.080+0800 reading metadata for mldn.deptcol from D:\dump\mldn\deptcol.metadata.json
2018-09-10T10:10:55.080+0800 reading metadata for mldn.stucol from D:\dump\mldn\stucol.metadata.json
2018-09-10T10:10:55.081+0800 reading metadata for mldn.students3 from D:\dump\mldn\students3.metadata.json
2018-09-10T10:10:55.081+0800 reading metadata for mldn.students2 from D:\dump\mldn\students2.metadata.json
2018-09-10T10:10:55.227+0800 restoring mldn.students2 from D:\dump\mldn\students2.bson
2018-09-10T10:10:55.238+0800 no indexes to restore
2018-09-10T10:10:55.239+0800 finished restoring mldn.students2 (8 documents)
2018-09-10T10:10:55.241+0800 reading metadata for mldn.emps from D:\dump\mldn\emps.metadata.json
2018-09-10T10:10:55.403+0800 restoring mldn.emps from D:\dump\mldn\emps.bson
2018-09-10T10:10:55.405+0800 no indexes to restore
2018-09-10T10:10:55.405+0800 finished restoring mldn.emps (8 documents)
2018-09-10T10:10:55.406+0800 reading metadata for mldn.students from D:\dump\mldn\students.metadata.json
2018-09-10T10:10:55.535+0800 restoring mldn.students from D:\dump\mldn\students.bson
2018-09-10T10:10:55.544+0800 restoring indexes for collection mldn.students from metadata
2018-09-10T10:10:55.873+0800 finished restoring mldn.students (7 documents)
2018-09-10T10:10:55.873+0800 reading metadata for mldn.shops from D:\dump\mldn\shops.metadata.json
2018-09-10T10:10:56.028+0800 restoring mldn.shops from D:\dump\mldn\shops.bson
2018-09-10T10:10:56.055+0800 restoring indexes for collection mldn.shops from metadata
2018-09-10T10:10:56.142+0800 finished restoring mldn.shops (7 documents)
2018-09-10T10:10:56.142+0800 reading metadata for mldn.t_job_emps from D:\dump\mldn\t_job_emps.metadata.json
2018-09-10T10:10:56.254+0800 restoring mldn.students3 from D:\dump\mldn\students3.bson
2018-09-10T10:10:56.353+0800 restoring mldn.deptcol from D:\dump\mldn\deptcol.bson
2018-09-10T10:10:56.470+0800 restoring mldn.stucol from D:\dump\mldn\stucol.bson
2018-09-10T10:10:56.603+0800 restoring mldn.t_job_emps from D:\dump\mldn\t_job_emps.bson
2018-09-10T10:10:56.604+0800 no indexes to restore
2018-09-10T10:10:56.604+0800 finished restoring mldn.students3 (8 documents)
2018-09-10T10:10:56.605+0800 reading metadata for mldn.news from D:\dump\mldn\news.metadata.json
2018-09-10T10:10:56.770+0800 restoring mldn.news from D:\dump\mldn\news.bson
2018-09-10T10:10:56.774+0800 no indexes to restore
2018-09-10T10:10:56.775+0800 finished restoring mldn.t_job_emps (3 documents)
2018-09-10T10:10:56.778+0800 reading metadata for mldn.testCollection from D:\dump\mldn\testCollection.metadata.json
2018-09-10T10:10:56.779+0800 restoring indexes for collection mldn.news from metadata
2018-09-10T10:10:56.785+0800 no indexes to restore
2018-09-10T10:10:56.787+0800 finished restoring mldn.stucol (999 documents)
2018-09-10T10:10:56.787+0800 reading metadata for mldn.dept from D:\dump\mldn\dept.metadata.json
2018-09-10T10:10:56.949+0800 [############............] mldn.deptcol 135KB/268KB (50.4%)
2018-09-10T10:10:57.043+0800 finished restoring mldn.news (5 documents)
2018-09-10T10:10:57.043+0800 reading metadata for mldn.phones from D:\dump\mldn\phones.metadata.json
2018-09-10T10:10:57.359+0800 restoring mldn.testCollection from D:\dump\mldn\testCollection.bson
2018-09-10T10:10:57.512+0800 restoring mldn.dept from D:\dump\mldn\dept.bson
2018-09-10T10:10:57.638+0800 no indexes to restore
2018-09-10T10:10:57.639+0800 finished restoring mldn.dept (3 documents)
2018-09-10T10:10:57.639+0800 no indexes to restore
2018-09-10T10:10:57.639+0800 finished restoring mldn.testCollection (5 documents)
2018-09-10T10:10:57.640+0800 restoring mldn.phones from D:\dump\mldn\phones.bson
2018-09-10T10:10:57.645+0800 restoring indexes for collection mldn.phones from metadata
2018-09-10T10:10:57.853+0800 finished restoring mldn.phones (0 documents)
2018-09-10T10:10:57.882+0800 [########################] mldn.deptcol 268KB/268KB (100.0%)
2018-09-10T10:10:57.882+0800 no indexes to restore
2018-09-10T10:10:57.883+0800 finished restoring mldn.deptcol (3976 documents)
2018-09-10T10:10:57.883+0800 done
查询备份的数据库文件的bson的数据
bsondump 路径/文件.bson
D:\Program Files\MongoDB\Server\4.0\bin>bsondump.exe D:\dump\mldn\dept.bson
{"_id":{"$oid":"5b87456dc90a95d56862f56b"},"deptno":10.0,"dname":"测试部门","loc":"北京"}
{"_id":{"$oid":"5b8748a8c90a95d56862f56c"},"deptno":11.0,"dname":"开发部门","loc":"天津"}
{"_id":{"$oid":"5b874a41c90a95d56862f56e"},"deptNo":11.0,"deptName":"test"}
2018-09-10T10:01:30.935+0800 3 objects found
版权声明: 原创文章,如需转载,请注明出处。 https://blog.csdn.net/lwx356481/article/details/82585221