直接上脚本

#!/bin/bash
#
#利用mongodump进行mongodb数据库热备份
#
#
MONGO_PREFIX=mongodb_backup_
SOURCE_PATH=/usr/bin
TARGET_PATH=/data
NOW_TIME=$(date +%Y%m%d)
BACKUP_LOG="/data/db_backup.log"
DATABASE=""

# --------- 邮件配置 -----------
# 发送邮件?
MAIL_BACKUP=1
# smtp主机,SERVER[:PORT]
MAIL_SMTP_HOST=""
# stmp账号密码
MAIL_SMTP_USER=""

MAIL_SMTP_PASS=""
# 发件人,如果使用QQ邮箱,发件人要与上面MAIL_SMTP_USER一样
MAIL_SENDER=""
# 收件人,多个请用空格分开
MAIL_RECEVER=""
# 抄送,多个请用空格分开
MAIL_CC=""
# 邮件主题
MAIL_SUBJECT="Mongodb:[$DATABASE] backup - `date +%Y-%m-%d_%H:%M`"
# 邮件内容
MAIL_MESSAGE="Mongodb:[$DATABASE] backup - `date +%Y-%m-%d_%H:%M`"
#使用 TLS(yes|no)
MAIL_TLS=no

send_mail()
{
	sendEmail -f $MAIL_SENDER -t $MAIL_RECEVER -s $MAIL_SMTP_HOST -u "$MAIL_SUBJECT" -a "${TARGET_PATH}/${MONGO_PREFIX}${NOW_TIME}.tgz" -m "$MAIL_MESSAGE" -xu "$MAIL_SMTP_USER" -xp "$MAIL_SMTP_PASS" -cc "$MAIL_CC" >> $BACKUP_LOG
}

start()
{
	${SOURCE_PATH}/mongodump --host 127.0.0.1 --port 27017 --db ${DATABASE} --out ${TARGET_PATH}/${NOW_TIME}
}
execute()
{
	start;
	if [ $? -eq 0 ]
		then
		echo $NOW_TIME" back successfully!" >>$BACKUP_LOG;
		cd ${TARGET_PATH}
		tar zcf "${MONGO_PREFIX}${NOW_TIME}.tgz" "${NOW_TIME}";
		send_mail;
		rm -rf ${MONGO_PREFIX}${NOW_TIME}.tgz
	else
		echo $NOW_TIME" back failure!" >>$BACKUP_LOG;
	fi
	clear;
}

clear(){
	OUT_TIME=$(date -d '-7 days' "+%Y%m%d")
	if [ -d "${TARGET_PATH}/${OUT_TIME}/" ]
		then
		rm -rf "${TARGET_PATH}/${OUT_TIME}/"
		echo "=======${TARGET_PATH}/${OUT_TIME}/===删除完毕==" >>$BACKUP_LOG;
	fi
}

if [ ! -d "${TARGET_PATH}/${NOW_TIME}/" ]
	then
	mkdir ${TARGET_PATH}/${NOW_TIME}
fi
execute
echo "============== back end ${NOW_TIME} ==============" >>$BACKUP_LOG;