您的位置:首页 > 数据库 > Mongodb

mongodb的备份与还原

2014-04-01 01:35 309 查看
用mongo自带的bin/mongodump和bin/mongorestore

备份和还原

# /usr/local/mongodb-2.4.8-master/bin/mongodump --help
Export MongoDB data to BSON files.

options:
--help                                produce help message
-v [ --verbose ]                      be more verbose (include multiple times
for more verbosity e.g. -vvvvv)
--version                             print the program's version and exit
-h [ --host ] arg                     mongo host to connect to ( <set
name>/s1,s2 for sets)
--port arg                            server port. Can also use --host
hostname:port
--ipv6                                enable IPv6 support (disabled by
default)
-u [ --username ] arg                 username
-p [ --password ] arg                 password
--authenticationDatabase arg          user source (defaults to dbname)
--authenticationMechanism arg (=MONGODB-CR)
authentication mechanism
--dbpath arg                          directly access mongod database files
in the given path, instead of
connecting to a mongod  server - needs
to lock the data directory, so cannot
be used if a mongod is currently
accessing the same path
--directoryperdb                      each db is in a separate directly
(relevant only if dbpath specified)
--journal                             enable journaling (relevant only if
dbpath specified)
-d [ --db ] arg                       database to use
-c [ --collection ] arg               collection to use (some commands)
-o [ --out ] arg (=dump)              output directory or "-" for stdout
-q [ --query ] arg                    json query
--oplog                               Use oplog for point-in-time
snapshotting
--repair                              try to recover a crashed database
--forceTableScan                      force a table scan (do not use
$snapshot)
示例备份:
# /usr/local/mongodb-2.4.8-master/bin/mongodump -h 192.168.119.46 -d adx -o /home/cai/
.bash_history   .bash_profile   conf.txt        hist            start.txt       xinetd.confbak
.bash_logout    .bashrc         .gnome2/        mongodb_bak/    .viminfo
[root@localhost cai]# /usr/local/mongodb-2.4.8-master/bin/mongodump -h 192.168.119.46 -d adx -o /home/cai/mongodb_bak
//-h 主机 -d 数据库 -o输出的目录
connected to: 192.168.119.46
Mon Mar 31 19:40:18.944 DATABASE: adx	 to 	/home/cai/mongodb_bak/adx
Mon Mar 31 19:40:18.985 	adx.system.indexes to /home/cai/mongodb_bak/adx/system.indexes.bson
Mon Mar 31 19:40:18.986 		 5 objects
Mon Mar 31 19:40:18.986 	adx.log.task to /home/cai/mongodb_bak/adx/log.task.bson
Mon Mar 31 19:40:19.026 		 1981 objects
Mon Mar 31 19:40:19.026 	Metadata for adx.log.task to /home/cai/mongodb_bak/adx/log.task.metadata.json
Mon Mar 31 19:40:19.026 	adx.log.show to /home/cai/mongodb_bak/adx/log.show.bson
Mon Mar 31 19:40:22.000 		Collection File Writing Progress: 1198000/7085824	16%	(objects)
Mon Mar 31 19:40:25.033 		Collection File Writing Progress: 2113400/7085824	29%	(objects)
Mon Mar 31 19:40:28.000 		Collection File Writing Progress: 2499100/7085824	35%	(objects)
Mon Mar 31 19:40:31.007 		Collection File Writing Progress: 2764300/7085824	39%	(objects)
Mon Mar 31 19:40:34.000 		Collection File Writing Progress: 3033600/7085824	42%	(objects)
Mon Mar 31 19:40:37.000 		Collection File Writing Progress: 4064900/7085824	57%	(objects)
Mon Mar 31 19:40:40.010 		Collection File Writing Progress: 4604800/7085824	64%	(objects)
Mon Mar 31 19:40:43.030 		Collection File Writing Progress: 4845600/7085824	68%	(objects)
Mon Mar 31 19:40:46.009 		Collection File Writing Progress: 5129200/7085824	72%	(objects)
Mon Mar 31 19:40:49.000 		Collection File Writing Progress: 5999700/7085824	84%	(objects)
Mon Mar 31 19:40:52.002 		Collection File Writing Progress: 6844900/7085824	96%	(objects)
Mon Mar 31 19:40:53.803 		 7085946 objects
Mon Mar 31 19:40:53.803 	Metadata for adx.log.show to /home/cai/mongodb_bak/adx/log.show.metadata.json
Mon Mar 31 19:40:53.804 	adx.log.click to /home/cai/mongodb_bak/adx/log.click.bson
Mon Mar 31 19:40:56.953 		Collection File Writing Progress: 10100/41340	24%	(objects)
Mon Mar 31 19:40:58.545 		 41340 objects
Mon Mar 31 19:40:58.545 	Metadata for adx.log.click to /home/cai/mongodb_bak/adx/log.click.metadata.json
Mon Mar 31 19:40:58.545 	adx.log.data to /home/cai/mongodb_bak/adx/log.data.bson
Mon Mar 31 19:41:01.000 		Collection File Writing Progress: 481500/7081875	6%	(objects)
Mon Mar 31 19:41:04.087 		Collection File Writing Progress: 812900/7081875	11%	(objects)
Mon Mar 31 19:41:07.045 		Collection File Writing Progress: 1281500/7081875	18%	(objects)
Mon Mar 31 19:41:10.010 		Collection File Writing Progress: 1902300/7081875	26%	(objects)
Mon Mar 31 19:41:13.032 		Collection File Writing Progress: 3107000/7081875	43%	(objects)
Mon Mar 31 19:41:16.008 		Collection File Writing Progress: 3803500/7081875	53%	(objects)
Mon Mar 31 19:41:19.000 		Collection File Writing Progress: 4224600/7081875	59%	(objects)
Mon Mar 31 19:41:23.250 		Collection File Writing Progress: 4263500/7081875	60%	(objects)
Mon Mar 31 19:41:26.265 		Collection File Writing Progress: 4282300/7081875	60%	(objects)
Mon Mar 31 19:41:29.052 		Collection File Writing Progress: 4675600/7081875	66%	(objects)
Mon Mar 31 19:41:32.164 		Collection File Writing Progress: 5291800/7081875	74%	(objects)
Mon Mar 31 19:41:35.000 		Collection File Writing Progress: 5733600/7081875	80%	(objects)
Mon Mar 31 19:41:37.363 		 7081875 objects
Mon Mar 31 19:41:37.363 	Metadata for adx.log.data to /home/cai/mongodb_bak/adx/log.data.metadata.json
Mon Mar 31 19:41:37.364 	adx.log.money_report_list to /home/cai/mongodb_bak/adx/log.money_report_list.bson
Mon Mar 31 19:41:37.364 		 8 objects
Mon Mar 31 19:41:37.364 	Metadata for adx.log.money_report_list to /home/cai/mongodb_bak/adx/log.money_report_list.metadata.json
# cd mongodb_bak/ #备份完的目录
mongodb_bak # ls
adx
mongodb_bak # cd adx/
adx # ls
log.click.bson           log.data.metadata.json               log.show.bson           log.task.metadata.json
log.click.metadata.json  log.money_report_list.bson           log.show.metadata.json  system.indexes.bson
log.data.bson            log.money_report_list.metadata.json  log.task.bson
adx # du -sh
8.3G    .

还原

# /usr/local/mongodb/bin/mongorestore --help
Import BSON files into MongoDB.

usage: /usr/local/mongodb/bin/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)
--version                             print the program's version and exit
-h [ --host ] arg                     mongo host to connect to ( <set
name>/s1,s2 for sets)
--port arg                            server port. Can also use --host
hostname:port
--ipv6                                enable IPv6 support (disabled by
default)
-u [ --username ] arg                 username
-p [ --password ] arg                 password
--authenticationDatabase arg          user source (defaults to dbname)
--authenticationMechanism arg (=MONGODB-CR)
authentication mechanism
--dbpath arg                          directly access mongod database files
in the given path, instead of
connecting to a mongod  server - needs
to lock the data directory, so cannot
be used if a mongod is currently
accessing the same path
--directoryperdb                      each db is in a separate directly
(relevant only if dbpath specified)
--journal                             enable journaling (relevant only if
dbpath specified)
-d [ --db ] arg                       database to use
-c [ --collection ] arg               collection to use (some commands)
--objcheck                            validate object before inserting
(default)
--noobjcheck                          don't validate object before inserting
--filter arg                          filter to apply before inserting
--drop                                drop each collection before import
--oplogReplay                         replay oplog for point-in-time restore
--oplogLimit arg                      include oplog entries before the
provided Timestamp (seconds[:ordinal])
during the oplog replay; the ordinal
value is optional
--keepIndexVersion                    don't upgrade indexes to newest version
--noOptionsRestore                    don't restore collection options
--noIndexRestore                      don't restore indexes
--w arg (=0)                          minimum number of replicas per write


还原示例:

# /usr/local/mongodb/bin/mongorestore -h 192.168.10.40 -d adx --drop mongodb_bak/adx
//-h 主机名或者ip -d 还原的数据库 --drop 删除已经导入的数据集
 connected to: 192.168.10.40
Mon Mar 31 23:21:43.714 mongodb_bak/adx/log.show.bson
Mon Mar 31 23:21:43.714 	going into namespace [adx.log.show]
Mon Mar 31 23:21:43.714 	 dropping
Mon Mar 31 23:21:46.003 		Progress: 47085159/5674443774	0%	(bytes)
Mon Mar 31 23:21:49.001 		Progress: 116730413/5674443774	2%	(bytes)
Mon Mar 31 23:21:52.036 		Progress: 170644327/5674443774	3%	(bytes)
Mon Mar 31 23:21:55.744 		Progress: 215644352/5674443774	3%	(bytes)
此处略去n个字。。。
7081875 objects found
Mon Mar 31 23:30:59.311     Creating index: { key: { _id: 1 }, ns: "adx.log.data", name: "_id_" }
Mon Mar 31 23:30:59.735 mongodb_bak/adx/log.task.bson
Mon Mar 31 23:30:59.735     going into namespace [adx.log.task]
Mon Mar 31 23:30:59.735      dropping
1981 objects found
Mon Mar 31 23:30:59.841     Creating index: { key: { _id: 1 }, ns: "adx.log.task", name: "_id_" }


还原导入完成
#########################

迷途小运维随笔

作者:john

转载请注明出处
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: