mongodb是目前最流行的nosql数据库,其自身也提供了备份与恢复命令 。具体程序为mongodump和mongorestore 。
一、mongodump备份
mongodump的具体用法可以查看帮助:
[root@web20 ~]# /App/mongodb/bin/mongodump -h ERROR: required parameter is missing in 'host' 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 (/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 --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 if dbpath specified, each db is in a separate directory --journal enable journaling -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)
<br />
帮助信息上已经写的很明了了 ,具体导出备份命令为:
mongodump -h dbhost -d dbname -o dbdirectory
<br />
-h 表示mongodb server地址,
-d 表示需要备份的数据名
-o 为备份数据存放的路径
如果设置了用户名密码还要使用-u和-p参数 ,如果想要导出单独库下的一个表,再增加-c参数 。具体用法同mysqldump有类似之处 。
注:如现在导出的库名为361way ,导出的路径为/opt ,则导出后会在/opt目录下有一个361way命名的目录 ,里面是由表名命名的json和bson文件 ,具体组成类似于表结构和表数据 。而导出的内容和mysql 略有不同,mysqldump导出的是一个sql文件而不是目录 。
二、mongorestore恢复
mongorestore具体用法类以于mongodump,不过参数上略有差异,具体用法为:
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) --version print the program's version and exit -h [ --host ] arg mongo host to connect to (/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 --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 if dbpath specified, each db is in a separate directory --journal enable journaling -d [ --db ] arg database to use -c [ --collection ] arg collection to use (some commands) --objcheck 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 exclude oplog entries newer than provided timestamp (epoch[:ordinal]) --keepIndexVersion don't upgrade indexes to newest version --noOptionsRestore don't restore collection options --noIndexRestore don't restore indexes --w arg (=1) minimum number of replicas per write
<br />
一般用法为:
mongorestore -h dbhost -d dbname --directoryperdb dbdirectory
<br />
–directoryperdb:备份数据所在位置 ,例如上例中,我们将361way库备份到了 /opt 下,这里恢复的时候使用的就是/opt/361way ,此处是和备份略有区别的 。
–drop:恢复的时候,先删除当前数据,然后恢复备份的数据
三、与其他备份恢复工具的对比
mongodb自带的备份工具还有bsondump、mongoexport,恢复工具还有mongoimport ,几者之间的具体区别是:
bsondump、mongoexport、mongodump备份工具的对比:
1、bsondump可以指定备份的格式为json和debug模式,这个命令虽然附带,但很少用到 ;
2、mongoexport 可以导出json或csv格式的文件,可以指定查询过滤器或指定输出的域,不过此工具导出的json,csv可能对某些数据类型不兼容,因此可能不能全部数据导出,mongodump就可以全部兼容 ;
3、mongodump支持过滤 ,而且在导出速度和压缩率方面mongodump是最快最好的 。所以,若无csv或debug等特殊格式的备份需求,一般都使用 mongodump 作为备份工具 。
mongorestore与mongoimport 恢复工具的对比:
1、 mongoimport 可以接受json,csv,tsv格式的文件,每行为一个对象 。同mongoexport一样,其在恢复过程中同样存在兼容性的问题,所以有恢复不完整的概率 ;
2、mongorestore,速度较慢,比mongoimport慢2.5倍左右,但是根据mongodump导出的数据,可以完整导入数据。在restore过程中,索引根据之前dump的结果重新创造。