运行环境:
[root@node10 ~]# uname -a
Linux node10 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@node10 ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
1.多个数据库备份脚本
[root@node10 Mongodb_shell_scripts]# cat get_mongodb_dbs.sh
#!/bin/bash
/usr/bin/mongo 127.0.0.1:27017/admin -u admin -p admin123. << EOF
show dbs
exit;
EOF
[root@node10 Mongodb_shell_scripts]# cat backup_all_database.sh
#!/bin/bash
source /etc/profile
user="admin"
pass="admin123."
port="27017"
Cmd="/usr/bin/mongodump"
Bakdir=/root/backup_file
[ ! -d "$Bakdir" ]&& mkdir -p $Bakdir
dbs="$(/bin/bash $Bakdir/Mongodb_shell_scripts/get_mongodb_dbs.sh|sed '1,2d'|grep -v "local"|awk '{ print $1 }'|grep -v -E 'Implicit|MongoDB|admin|config')"
for dbname in $dbs
do
SUB_DIR="database_${dbname}_$(date +%F-%H)"
mkdir $Bakdir/Mongodb_data/$SUB_DIR -p
echo "$(date +%F_%H-%M-%S)-Start to Backup dbname:$dbname ....">>$Bakdir/mongodb_back_schame_database.log
/usr/bin/mongodump -h 127.0.0.1 -u $user -p $pass -d $dbname --gzip --authenticationDatabase admin --archive="$Bakdir/Mongodb_data/$SUB_DIR/$dbname.archive"
echo "$(date +%F_%H-%M-%S)-Backup dbname:$dbname success!!!">>$Bakdir/mongodb_back_schame_database.log
sleep 1
done
echo "$(date +%F_%H-%M-%S)- Backup Mongodb DATA success!!!">>$Bakdir/mongodb_back_schame_database.log
find $Bakdir/Mongodb_data/* -type d -mtime +30 -exec rm -rf {} \; &>/dev/null
分库分集合备份脚本:
[root@node10 Mongodb_shell_scripts]# cat get_collections_mongodb.sh
#!/bin/bash
/usr/bin/mongo 127.0.0.1:27017/admin -u admin -p admin123. << EOF
use $1
show collections
exit;
EOF
[root@node10 Mongodb_shell_scripts]# cat get_mongodb_dbs.sh
#!/bin/bash
/usr/bin/mongo 127.0.0.1:27017/admin -u admin -p admin123. << EOF
show dbs
exit;
EOF
[root@node10 Mongodb_shell_scripts]# cat backup_collections_per_database.sh
#!/bin/bash
source /etc/profile
user="admin"
pass="admin123."
port="27017"
Cmd="/usr/bin/mongodump"
Bakdir=/root/backup_file
[ ! -d "$Bakdir" ]&& mkdir -p $Bakdir
dbs="$(/bin/bash $Bakdir/Mongodb_shell_scripts/get_mongodb_dbs.sh|sed '1,2d'|grep -v "local"|awk '{ print $1 }'|grep -v -E 'Implicit|MongoDB|admin|config')"
Dump="$Cmd -u$user -p$pass --port=$port -d"
for dbname in $dbs
do
SUB_DIR="collections_${dbname}_$(date +%F-%H)"
mkdir $Bakdir/Mongodb_data/$SUB_DIR -p
collections="$(/bin/bash $Bakdir/Mongodb_shell_scripts/get_collections_mongodb.sh $dbname |sed '1,2d'|grep -v "local"|awk '{ print $1 }'|grep -v -E 'Implicit|MongoDB|switched')"
for coll_names in $collections
do
echo "$(date +%F_%H-%M-%S)-Start to Backup dbname:$dbname -->Table:$coll_names ....">>$Bakdir/mongodb_back_schame_collections.log
/usr/bin/mongoexport -h 127.0.0.1 -u $user -p $pass -d $dbname -c $coll_names --type json -o "$Bakdir/Mongodb_data/$SUB_DIR/$dbname_$coll_names.json" --authenticationDatabase admin
echo "$(date +%F_%H-%M-%S)-Backup dbname:$dbname -->Table:$coll_names success!!!">>$Bakdir/mongodb_back_schame_collections.log
sleep 1
done
done
echo "$(date +%F_%H-%M-%S)- Backup Mongodb DATA success!!!">>$Bakdir/mongodb_back_schame_collections.log
find $Bakdir/Mongodb_data/* -type d -mtime +30 -exec rm -rf {} \; &>/dev/null
以上脚本已部署在生产环境系统上面,可以正常使用。