mongo数据备份及恢复脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongo数据备份及恢复脚本相关的知识,希望对你有一定的参考价值。
#!/bin/bash
time="$(date +"%Y.%m.%d")"
id=`echo "show dbs;"|/usr/local/mongod3.2/bin/mongo 10.124.156.251:27018 --shell|grep ‘A[0-9]\{13\}‘|awk -F ‘ ‘ ‘{print $1}‘`
for i in $id
do
/usr/local/mongod3.2/bin/mongodump --host="10.124.156.251" --port=27018 -d $i -o /home/mongodb.bak/mongoM1/tmp/$i.dump &> /dev/null
done
cd /home/mongodb.bak/mongoM1
tar zcf $time.mongodbM1_27018.dump.tar.gz tmp/ 2> /tmp/mongo.error.log && mv $time.mongodbM1_27018.dump.tar.gz tar && rm -rf tmp/* &> /dev/null
id=`echo "show dbs;"|/usr/local/mongod3.2/bin/mongo 10.124.156.243:30001 --shell|grep ‘A[0-9]\{13\}‘|awk -F ‘ ‘ ‘{print $1}‘`
for i in $id
do
/usr/local/mongod3.2/bin/mongodump --host="10.124.156.243" --port=30001 -d $i -o /home/mongodb.bak/mongoM2/tmp/$i.dump &> /dev/null
done
cd /home/mongodb.bak/mongoM2
tar zcf $time.mongodbM2_30001.dump.tar.gz tmp/ 2> /tmp/mongo.error.log && mv $time.mongodbM2_30001.dump.tar.gz tar && rm -rf tmp/* &> /dev/null
id=`echo "show dbs;"|/usr/local/mongod3.2/bin/mongo 10.124.156.224:27019 --shell|grep ‘A[0-9]\{13\}‘|awk -F ‘ ‘ ‘{print $1}‘`
for i in $id
do
/usr/local/mongod3.2/bin/mongodump --host="10.124.156.224" --port=27019 -d $i -o /home/mongodb.bak/mongoM3/tmp/$i.dump &> /dev/null
done
cd /home/mongodb.bak/mongoM3
tar zcf $time.mongodbM3_27019.dump.tar.gz tmp/ 2> /tmp/mongo.error.log && mv $time.mongodbM3_27019.dump.tar.gz tar && rm -rf tmp/* &> /dev/null
id=`echo "show dbs;"|/usr/local/mongod3.2/bin/mongo 10.124.156.217:30000 --shell|grep ‘A[0-9]\{13\}‘|awk -F ‘ ‘ ‘{print $1}‘`
for i in $id
do
/usr/local/mongod3.2/bin/mongodump --host="10.124.156.217" --port=30000 -d $i -o /home/mongodb.bak/mongoM4/tmp/$i.dump &> /dev/null
done
cd /home/mongodb.bak/mongoM4
tar zcf $time.mongodbM4_30000.dump.tar.gz tmp/ 2> /tmp/mongo.error.log && mv $time.mongodbM4_30000.dump.tar.gz tar && rm -rf tmp/* &> /dev/null
恢复:
#!/bin/bash
dir=/usr/local/src/mongodbM4_30000_HW_dump
for i in $dir/*; do
mongorestore -h 192.168.131.133 --port 27018 --drop $i
done
以上是关于mongo数据备份及恢复脚本的主要内容,如果未能解决你的问题,请参考以下文章
恢复 mongo 数据库但 mongo shell 不显示它
mongodb数据库恢复 mongo数据库无法启动恢复 mongodb数据库断电数据恢复