mongodb数据备份与恢复

MongoDB提供了备份和恢复的功能,分别是mongdump和mongorestore两个命令

先介绍下命令语法:

#mongodump -h dbhost -d dbname -o dbdirectory

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

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

-o:备份的数据存放位置,例如:/home,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放该数据库实例的备份数据。


#mongorestore -h dbhost -d dbname --directoryperdb dbdirectory

-h:MongoDB所在服务器地址

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

扫描二维码关注公众号,回复: 695849 查看本文章

--directoryperdb:备份数据所在位置,例如:/home/test,这里为什么要多加一个test,而不是备份时候的dump,读者自己查看提示吧!

--drop:恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用!

具体操作如下:

[root@AY1308021452325580baZ ~]# mongodump -h localhost -d test -o /home/
connected to: localhost
Mon Sep 16 17:25:28.143 DATABASE: test   to     /home/test
Mon Sep 16 17:25:28.146         test.system.indexes to /home/test/system.indexes.bson
Mon Sep 16 17:25:28.147                  2 objects
Mon Sep 16 17:25:28.147         test.test to /home/test/test.bson
Mon Sep 16 17:25:28.147                  1 objects
Mon Sep 16 17:25:28.147         Metadata for test.test to /home/test/test.metadata.json
Mon Sep 16 17:25:28.147         test.testCollection to /home/test/testCollection.bson
Mon Sep 16 17:25:28.148                  2 objects
Mon Sep 16 17:25:28.148         Metadata for test.testCollection to /home/test/testCollection.metadata.json
[root@AY1308021452325580baZ test]# mongorestore -h localhost -d test -directoryperdb /home/test --drop
connected to: localhost
Mon Sep 16 17:26:40.540 /home/test/testCollection.bson
Mon Sep 16 17:26:40.540         going into namespace [test.testCollection]
Mon Sep 16 17:26:40.540          dropping
2 objects found
Mon Sep 16 17:26:40.540         Creating index: { key: { _id: 1 }, ns: "test.testCollection", name: "_id_" }
Mon Sep 16 17:26:40.541 /home/test/test.bson
Mon Sep 16 17:26:40.541         going into namespace [test.test]
Mon Sep 16 17:26:40.541          dropping
1 objects found
Mon Sep 16 17:26:40.543         Creating index: { key: { _id: 1 }, ns: "test.test", name: "_id_" }

猜你喜欢

转载自stranger2008.iteye.com/blog/1942950