查看: 3286|回复: 0

mongodb的备份与恢复

[复制链接]
论坛徽章:
65
生肖徽章2007版:龙
日期:2012-04-26 14:03:05生肖徽章2007版:龙
日期:2012-04-26 14:03:05生肖徽章2007版:龙
日期:2012-04-26 14:03:05生肖徽章2007版:龙
日期:2012-04-26 14:03:05生肖徽章2007版:龙
日期:2012-04-26 14:03:05生肖徽章2007版:龙
日期:2012-04-26 14:03:05蓝锆石
日期:2012-04-26 13:39:45开发板块每日发贴之星
日期:2012-04-26 14:03:05ERP板块每日发贴之星
日期:2012-04-26 14:03:05行业板块每日发贴之星
日期:2012-04-26 14:03:05
跳转到指定楼层
1#
发表于 2012-2-29 11:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
mongodb提供了两个命令来备份(mongodump )和恢复(mongorestore )数据库。

1.备份(mongodump )

用法 :
[root@web3 3]# mongodump --help
options:
--help                   produce help message
-v [ --verbose ]         be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
-h [ --host ] arg        mongo host to connect to ("left,right" for pairs)
-d [ --db ] arg          database to use
-c [ --collection ] arg  collection to use (some commands)
-u [ --username ] arg    username
-p [ --password ] arg    password
--dbpath arg             directly access mongod data files in the given path,
instead of connecting to a mongod instance - needs
to lock the data directory, so cannot be used if a
mongod is currently accessing the same path
--directoryperdb         if dbpath specified, each db is in a separate
directory
-o [ --out ] arg (=dump) output directory

例子:

[root@web3 ~]# mongodump -h 192.168.1.103 -d citys -o /backup/mongobak/3
connected to: 192.168.1.103
DATABASE: citys  to     /backup/mongobak/3/citys
citys.building to /backup/mongobak/3/citys/building.bson
13650 objects
citys.system.indexes to /backup/mongobak/3/citys/system.indexes.bson
1 objects

备份出来的数据是二进制的,已经经过压缩。比实际数据库要小很多,我的数据库显示占用了260多M,备份后只有2M。

2.恢复(mongorestore )

用法:
[root@web3 3]# mongorestore --help
usage: mongorestore [options] [directory or filename to restore from]
options:
--help                  produce help message
-v [ --verbose ]        be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
-h [ --host ] arg       mongo host to connect to ("left,right" for pairs)
-d [ --db ] arg         database to use
-c [ --collection ] arg collection to use (some commands)
-u [ --username ] arg   username
-p [ --password ] arg   password
--dbpath arg            directly access mongod data files in the given path,
instead of connecting to a mongod instance - needs to
lock the data directory, so cannot be used if a
mongod is currently accessing the same path
--directoryperdb        if dbpath specified, each db is in a separate
directory
--drop                  drop each collection before import
--objcheck              validate object before inserting

--drop参数可以在导入之前把collection先删掉。

例子:

[root@web3 3]# mongorestore -h 127.0.0.1 --directoryperdb /backup/mongobak/3/         
connected to: 127.0.0.1
/backup/mongobak/3/citys/building.bson
going into namespace [citys.building]
13667 objects
/backup/mongobak/3/citys/system.indexes.bson
going into namespace [citys.system.indexes]
1 objects

另外mongodb还提供了mongoexport 和 mongoimport 这两个命令来导出或导入数据,导出的数据是json格式的。也可以实现备份和恢复的功能。
例:
mongoexport -d mixi_top_city_prod -c building_45 -q '{ "uid" : "10832545" }' > mongo_10832545.bson
mongoimport -d mixi_top_city -c building_45 --file mongo_10832545.bson

此文章转自http://hi.baidu.com/farmerluo/bl ... 85633832fa1c3f.html

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表