1,设置副本集
#!/bin/bash #mongodb 进入client 并use admin cd /usr/mongodb/bin MongoDB='./mongo 192.168.1.7:37017' $MongoDB <<EOF use admin rsconf={ "_id" : "Job001", "members" : [ { "_id" : 0, "host" : "192.168.1.7:37017" } ] } rs.initiate(rsconf) rs.add("192.168.1.8:37017") rs.add("192.168.1.9:37017") exit; EOF
2,修改副本集里mongodb的优先级
#!/bin/bash #mongodb 进入client 并进入primary cd /usr/mongodb/bin MongoDB='./mongo 192.168.1.7:37017' $MongoDB <<EOF use MongoModelJobResume #rs.status() cfg=rs.conf() cfg.members[0].priority=99 cfg.members[1].priority=50 cfg.members[2].priority=30 rs.reconfig(cfg) rs.conf() exit; EOF
3,把一个表转到临时表,再拷贝回来,这样oplog中就会重新生成新的完整log(前提是oplog要足够大)
#!/bin/bash #mongodb 进入client 并进入primary cd /usr/mongodb/bin MongoDB='./mongo 192.168.1.7:37017' $MongoDB <<EOF use MongoModelJobResume rs.remove("192.168.1.8:37017") rs.remove("192.168.1.9:37017") db.tbJobResume.renameCollection("tbJobResumeOld") db.tbJobResumeOld.copyTo("tbJobResume") rs.add("192.168.1.8:37017") rs.add("192.168.1.9:37017") #db.tbJobResumeOld.drop() exit; EOF
4,按照日期生成mongodb日志
#!/bin/bash #mongodb client use admin cd /usr/mongodb/bin MongoDB='./mongo 192.168.1.7:37017' $MongoDB <<EOF use admin db.runCommand( { logRotate : 1 } ) exit; EOF
配合crontab 按每天的日期生成log
5,自动备份还原mongodb的脚本
================自动备份并且压缩============================
#!/bin/bash filename=`date +%Y%m%d%H` backmongodbFile=mongodb$filename.tar.gz cd /home/mongo/back/ /usr/mongodb/bin/mongodump -h 192.168.1.7 -port 37017 -d MongoDBAgent -o mongodb_dump/ /usr/mongodb/bin/mongodump -h 192.168.1.7 -port 37017 -d MongoDBBg -o mongodb_dump/ /usr/mongodb/bin/mongodump -h 192.168.1.7 -port 37017 -d MongoModelActor -o mongodb_dump/ tar czf $backmongodbFile mongodb_dump/ rm mongodb_dump -rf
================自动解压并还原数据=============================
#!/bin/bash filename='20150330013' backmongodbFile=mongodb$filename.tar.gz cd /home/mongo/back/ tar zxvf $backmongodbFile /usr/mongodb/bin/mongorestore -h 192.168.1.6 -port 37017 --drop -d MongoDBAgent mongodb_dump/MongoDBAgent /usr/mongodb/bin/mongorestore -h 192.168.1.6 -port 37017 --drop -d MongoDBBg mongodb_dump/MongoDBBg /usr/mongodb/bin/mongorestore -h 192.168.1.6 -port 37017 --drop -d MongoModelActor mongodb_dump/MongoModelActor rm mongodb_dump -rf