mongodb的备份与恢复
2011-04-27 14:16
281 查看
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
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
相关文章推荐
- 通过mongodump和mongorestore实现Mongodb备份和恢复
- mongodb 的数据备份与恢复
- mongodb备份与恢复
- mongodb 学习笔记 07 -- 数据备份、恢复
- MongoDB的备份(mongodump)与恢复(mongorestore)
- MongoDB数据库的文件备份恢复以及文件导入导出
- mongodb备份与恢复
- MongoDB-备份和恢复
- MongoDB 备份(mongodump)与恢复(mongorestore)
- MongoDB的备份和恢复
- Windows 下 MongoDB的备份(mongodump)与恢复(mongorestore)
- mongodb的备份与恢复
- linux 下mongoDB备份与恢复
- MongoDB初试备份及恢复
- Windows或Linux系统中备份和恢复MongoDB数据的教程
- mongodb的备份与恢复
- mongoDB中备份和恢复数据库的命令
- 使用mongoDB必须要知道的备份和恢复数据库的命令
- mongodb备份恢复学习1
- MongoDB 导出导入备份恢复数据实例