适用范围:MongoDB单机模式。
可备份本机或者远程服务器,MongoDB文件磁盘占用较大,备份后使用7zip压缩,7zip支持多线程,速度和压缩率相当高,2.9GB样本实测压缩后大小为263MB,压缩率为8%,CPU使用率高达600%-700%(8核心),另外备份失败或压缩失败都会发出邮件提醒(邮件配置此处不列出)
#!/bin/bash sourcepath='/usr/local/mongodb2.6.6/bin' targetpath='/usr/backup/mongodb' nowtime=$(date "+%Y-%m-%d") backup() { echo "[$(date "+%Y-%m-%d %H:%M:%S")]===========backup start=================" if ${sourcepath}/mongodump -h 127.0.0.1 -o ${targetpath}/mongodb_${nowtime} then echo -e "\033[41;37m ===========backup successfully================= \033[0m" return 1 else echo -e "\033[41;37m ===========backup failure================= \033[0m" echo -e "MongoDB使用mongodump工具备份失败,请登陆服务器查看日志\n日志路径:/usr/backup/backup.log" | mail -v -s "MongoDB备份" name@domain.com fi } compress() { if [ $? -eq 1 ] then echo -e "\033[41;37m ===========现在开始打包压缩================= \033[0m" cd /usr/backup/mongodb if 7za a mongodb_${nowtime}.7z mongodb_${nowtime}/ then echo -e "\033[41;37m [$(date "+%Y-%m-%d %H:%M:%S")]===========压缩完成,现在删除原文件夹================= \033[0m" rm -rf mongodb_${nowtime}/ echo -e "\033[41;37m [$(date "+%Y-%m-%d %H:%M:%S")]===========执行完毕,脚本退出================= \033[0m" else echo -e "MongoDB备份成功,但打包失败,请登陆服务器查看日志\n日志路径:/usr/backup/backup.log" | mail -v -s "MongoDB备份" name@domain.com fi fi } backup compress