MongoDB集群备份简介:

      基于mongodb 副本机+分片的架构进行全库备份。 

[root@bj02-ops-sto01 scripts]# cat mongodb_backup.sh 

#!/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/usr/local/mongodb/bin:/usr/local/mongodb/bin

export PATH

export LANG=en_US.utf8


Dbip="192.168.100.10"

Dport="30000"

Datetime=$(date +%Y-%m-%d_%H_%M_%S)

Logfile="/backup/mongodb/mongodbbak_10010/backmongo.log"

Dblist="$(echo 'show dbs'|/usr/local/mongodb/bin/mongo --host ${Dbip} --port ${Dport} |egrep -v "MongoDB|connecting|bye"|awk '{print$1}')"


echo -e "=======start for database backup `date +%F' '%T' '%u`=======\n"  >>$Logfile

backdbcmd(){

echo "$(date) backup $dbname" 

/usr/local/mongodb/bin/mongodump --host ${Dbip} --port ${Dport} --db ${dbname} --out /backup/mongodb/mongodbbak_10010/${Datetime}

echo -e "$(date) Done.\n"

}


for dbname in $Dblist

    do

      echo -e "--------$dbname--------\n" >>$Logfile

      backdbcmd >>$Logfile 2>&1

    done


恢复整个库:

需要指定上一级目录,并且删除config目录

/usr/local/mongodb/bin/mongorestore  --port=30000 --dir=/home/mongodb/2016-07-28_00_12_05 --writeConcern='{w:0,wtimeout:200}' --numParallelCollections=8 --numInsertionWorkersPerCollection=10 --stopOnError  -v --drop