Mongodb集群部署测试-备份与恢复

##########################################################################################

数据备份:可做到不停机备份。
原理:mongodump本身是一个客户端,去读取mongod。然后保存为bson格式。
16008是mongs的端口 bak是输出目录
./mongodump --host 127.0.0.1:16008  -o  bak 
显示如下执行结果:
connected to: 127.0.0.1:16008
all dbs
DATABASE: ISYDB  to     bak/ISYDB
        ISYDB.VIDEO to bak/ISYDB/VIDEO.bson
                95800/1162380   8%
                207500/1162380  17%
                229800/1162380  19%
                240900/1162380  20%
                263200/1162380  22%
                308100/1162380  26%
                352300/1162380  30%
                413300/1162380  35%
                430000/1162380  36%
                452200/1162380  38%
                463300/1162380  39%
                482900/1162380  41%
                496600/1162380  42%
                597100/1162380  51%
                658500/1162380  56%
                703200/1162380  60%
                756100/1162380  65%
                818500/1162380  70%
                859100/1162380  73%
                892900/1162380  76%
                904200/1162380  77%
                949300/1162380  81%
                983900/1162380  84%
                1012900/1162380 87%
                1082100/1162380 93%
                1145500/1162380 98%
                 1162384 objects
        ISYDB.system.indexes to bak/ISYDB/system.indexes.bson
                 1 objects
DATABASE: admin  to     bak/admin
DATABASE: config         to     bak/config
        config.version to bak/config/version.bson
                 1 objects
        config.system.indexes to bak/config/system.indexes.bson
                 14 objects
        config.settings to bak/config/settings.bson
                 1 objects
        config.chunks to bak/config/chunks.bson
                 169 objects
        config.shards to bak/config/shards.bson
                 3 objects
        config.mongos to bak/config/mongos.bson
                 2 objects
        config.lockpings to bak/config/lockpings.bson
                 7 objects
        config.locks to bak/config/locks.bson
                 2 objects
        config.databases to bak/config/databases.bson
                 2 objects
        config.collections to bak/config/collections.bson
                 1 objects
        config.changelog to bak/config/changelog.bson
                 1336 objects
##########################################################################################
备份恢复
./mongorestore --host  127.0.0.1:16008 --drop --directoryperdb bak

会打印恢复进程。恢复成功后数据只会存在一个节点上。需要重新均衡数据。
--drop代表在恢复前删除已有集合。否则会与现有集合合并
##########################################################################################
有可能回报很多的错误:

db.runCommand({addshard:"ip:port"}); 
{ "ok" : 0, "errmsg" : "hostalreadyused" }

基本都是由于configServer中的配置不对。删除configServer数据,重新配置mongos和config。基本就都可以决绝了