目录
2. 全局参数配置文件:backup_monitor_wf.conf
一、备份状态监控提醒功能树形图
(备份监控机器)xx.xx.1.49:/home/wufei/monitor/ ├── bin │ └──backup_monitor_wf.sh ├── conf │ └──backup_monitor_wf.conf ├── html │ ├── monitor_backup_info_20180912.html │ └── monitor_backup_info_20180913.html └── log ├── backup_monitor_wf.log ├── monitor_backup_info_20180912.log └── monitor_backup_info_20180913.log |
1. bin/backup_monitor_wf.sh:执行脚本
2. conf/backup_monitor_wf.conf:全局参数配置文件(数据库连接用户、密码、库名、表名、IP、端口号、HTML文件保存路径、log保存路径、收信人邮箱地址、抄送人邮箱地址、文件保存天数、等.....)
3. html/monitor_backup_info_[yyyymmdd].html:邮件正文内容
4 . log/backup_monitor_wf.log:日志文件
5. log/monitor_backup_info_[yyyymmdd].log:缓存日志
二、邮件提醒
1. 接入公司邮件系统
2. 配置mutt发送邮件
服务器已经默认安装了msmtp。
2.1 新建~/.msmtprc
account default host 发送邮件服务器名称 port 25 from 发信人 auth plain user 发信人邮箱账号 password 发信人邮箱密码 logfile ~/.msmtp.log 日志存放位置 |
2.2 新建~/.muttrc
(或:cp /etc/Muttrc ~/.muttrc)
set sendmail="/usr/bin/msmtp" msmtp 安装位置 (可以使用which msmtp获取) set use_from=yes set realname="XX" 发信人名称 set from="XX" 发信人邮箱账号 set envelope_from=yes set charset="utf-8" |
2.3 修改.msmtprc的权限
# chmod 0600 .msmtprc
否则会提示:
“.msmtprc: contains secrets and therefore must have no more than user read/write permissions”
2.4 测试
# echo "测试test" | mutt -s "test" wufei@xxxxx.com # mutt wufei@xxxxx.com -s 'test send mail' |
3. 自动发邮件
3.1 加入计划任务,每天0:10会自动发送前一天的备份报告
# MySQL备份状态监控提醒 10 0 * * * /bin/bash /home/wufei/monitor/bin/backup_monitor_wf.sh -o crontab >> /home/wufei/monitor/log/backup_monitor_wf.log 2>&1 |
3.2 当每天MY数据库备份策略出现failed时会立即告警发送邮件。
三、功能介绍
1. bash backup_monitor_wf.sh:直接获取当天最新的备份状态信息,然后直接在窗口展示。
2. bash backup_monitor_wf.sh -d [yyyy-mm-dd]:-d指定日期(有日期校验功能哦,仅限于合法date,当然未来的合法date也是不行的,备份还是有保存天数的,超过了保质期也是查询不到的),然后直接在窗口打印改天备份信息,如果该天的详细信息已经存在,会直接读取缓存日志,不会再去麻烦数据库。
3. bash backup_monitor_wf.s -o email:-o email将指定以发送邮件的形式获取当天最新备份状态信息,获取当天信息是不会查询缓存日志的。
4. bash backup_monitor_wf.sh -d [yyyy-mm-dd] -o email:同理,通过发邮件的形式获取指定日期的备份状态信息。
5. bash backup_monitor_wf.sh -o crontab:-o crontab计划任务专用参数,当然也可以命令行直接运行,在该参数下再指定日期是无效的,永远只会查询昨天的信息,而且永远不会查询缓存日志,还会重新生成缓存日志。
四、信息展示
2018-09-13MySQL备份状态信息
发件人:MySQL备份状态监控提醒<jiankongbj@xxxxx.com>
收件人:wufei<wufei@xxxxx.com>
时间:2018年9月14日(周五) 00:10
大小:7.6KB
XXXX MySQL 备份状态信息:
日期 | 备份IP | 端口号 | 备份实例 | 预定备份时间 | 备份模式 | 保存地址IP | 备份保存路径 | 一周备份日期 | 备份保存天数 | 备份开始时间 | 备份结束时间 | 备份状态 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
2018-09-13 | xx.xx.0.70 | 3220 | xxxx_bim | 01:10:00 | full | 10.20.5.16 | /home/backup_mysql_wufei/backup/mysql_xxxx_bim_3220/full_2018-09-13_01-10-01_4 | 1,2,3,4,5,6,7 | 6 | 2018-09-13 01:10:00 | 2018-09-13 01:14:03 | success |
2018-09-13 | xx.xx.5.72 | 3215 | xxxx_im | 02:10:00 | full | 10.20.5.16 | /home/backup_mysql_wufei/backup/mysql_xxxx_im_3215/full_2018-09-13_02-10-02_4 | 1,3,5 | 6 | 2018-09-13 02:10:00 | 2018-09-13 02:29:18 | success |
五、监控脚本
1. 执行脚本:backup_monitor_wf.sh
#!/bin/bash
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Program : MySQL 备份监控脚本 #
# Author : wufei@xxxxx.com #
# Description : backup moitor functions #
# Date : 2018-09-12 #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# 更新时间:2018-11-26;更新人:WUFEI;更新内容:增加是否需要备份判断,对is_backup=0的备份库进行监控
function usage(){
echo "parameter error occurs."
echo "$0 -d [backup_date] -o [output_from]"
echo "-d : Enter a date, example 1993-09-02. The default is today."
echo "-o : Output mode, provide two parameters log and email. The default is direct printing."
}
if [ $# -gt 4 ];then
usage;
exit;
fi
# 读取配置文件中的所有变量,设置为全局变量
# 配置文件
conf_file='/home/wufei/monitor/conf/backup_monitor_wf.conf'
# 获取MySQL备份信息数据库连接用户
m_user=`sed '/^m_user=/!d;s/.*=//' ${conf_file}`
# 获取MySQL备份信息数据库连接密码
m_passwd=`sed '/^m_passwd=/!d;s/.*=//' ${conf_file}`
# 获取MySQL备份信息数据库
db_name=`sed '/^db_name=/!d;s/.*=//' ${conf_file}`
# 获取MySQL备份策略表
tb_policy=`sed '/^tb_policy=/!d;s/.*=//' ${conf_file}`
# 获取MySQL备份信息表
tb_info=`sed '/^tb_info=/!d;s/.*=//' ${conf_file}`
# 获取MySQL备份信息数据库IP
m_host=`sed '/^m_host=/!d;s/.*=//' ${conf_file}`
# 获取MySQL备份信息数据库端口
m_port=`sed '/^m_port=/!d;s/.*=//' ${conf_file}`
# 获取HTML文件保存路径
m_html_dir=`sed '/^m_html_dir=/!d;s/.*=//' ${conf_file}`
# 获取日志文件保存路径
m_log_dir=`sed '/^m_log_dir=/!d;s/.*=//' ${conf_file}`
# 获取收信人邮箱
m_to_email=`sed '/^m_to_email=/!d;s/.*=//' ${conf_file}`
# 获取抄送人邮箱
m_to_email_other=`sed '/^m_to_email_other=/!d;s/.*=//' ${conf_file}`
# 获取log文件与html文件保存天数(默认与备份保存天数一致)
m_remain_days=`sed '/^m_remain_days=/!d;s/.*=//' ${conf_file}`
# 备份日期
backup_date=`date +%F`
backup_time=`date +%H-%M-%S`
backup_week_day=`date +%u`
backup_d=`date +%Y%m%d`
backup_dt=`date +%y%m%d_%H%M%S`
# 获取传递参数项
declare monitor_date
declare monitor_output
while getopts "d:o:" arg
do
case ${arg} in
d)
monitor_date="${OPTARG}"
;;
o)
monitor_output="${OPTARG}"
;;
?)
{ usage; exit 1; }
;;
esac
done
function check_date(){
dt="${1}"
len_dt=${#dt}
dt=${dt//-/}
len_dt2=${#dt}
p=`echo $((${len_dt}-${len_dt2}))`
if [ 2 -eq ${p} ]
then
tt=`date -d "$1" +"%Y-%m-%d"`
date_judge=$?
else
if [ 8 -eq ${len_dt2} ]
then
tt=`date -d "$1" +"%Y-%m-%d"`
date_judge=$?
else
date_judge=1
fi
fi
return ${date_judge}
}
# 校验-d参数
if [ -z ${monitor_date} ]
then
monitor_date=${backup_date}
monitor_week=${backup_week_day}
monitor_d=${backup_d}
if [ ${monitor_week} -eq 0 ];then
monitor_week=`expr ${monitor_week} + 7`
fi
else
check_date "${monitor_date}"
if [ ${date_judge} -eq 1 ]
then
{ usage; exit 1; }
else
monitor_date=${monitor_date}
monitor_week=`date -d "${monitor_date}" +%w`
monitor_d=`date -d "${monitor_date}" +%Y%m%d`
if [ ${monitor_week} -eq 0 ];then
monitor_week=`expr ${monitor_week} + 7`
fi
fi
fi
# 校验-o参数
if [[ -z ${monitor_output} || ${monitor_output} = 'log' ]]
then
monitor_output_judge=0
elif [ ${monitor_output} = 'email' ]
then
monitor_output_judge=1
elif [ ${monitor_output} = 'crontab' ]
then
monitor_output_judge=2
else
{ usage; exit 1; }
fi
# MySQL备份信息数据库连接
function mysql_conn01(){
mysql -u${m_user} -p${m_passwd} -h${m_host} -P${m_port} -D${db_name} -N -e "${1}"
}
function mysql_conn02(){
mysql -u${m_user} -p${m_passwd} -h${m_host} -P${m_port} -D${db_name}
}
# 判断日期,避免重复操作
function monitor_date_judge(){
t1=`date -d "${monitor_date}" +%Y%m%d`
t2=`date -d "${backup_date}" +%Y%m%d`
p=`expr ${t2} - ${t1}`
if [ ${p} -eq 0 ]
then
# monitor_date_judge_num=0时说明查询的事当天最新信息
monitor_date_judge_num=0
elif [[ ${p} -gt 0 && ${p} -lt 7 ]]
then
# monitor_date_judge_num=1时说明可以先检查是否有历史查询信息,如果没有再查询数据库
monitor_date_judge_num=1
else
# monitor_date_judge_num=2时说明超出已存在备份日期
monitor_date_judge_num=2
fi
return ${monitor_date_judge_num}
}
# 查询备份信息并输出到log文件
function set_backup_info_to_log(){
sql01="select if(info.backup_date is null,'No Backup',info.backup_date) as '日期',inet_ntoa(policy.ip) as '备份IP',policy.port as '端口号',policy.db as '备份实例',policy.begintime as '预定备份时间',policy.mode as '备份模式',inet_ntoa(policy.dest_ip) as '保存地址IP',info.dest_dir as '备份保存路径',policy.weekday as '一周备份日期',policy.remain_days as '备份保存天数',info.begintime as '备份开始时间',info.endtime as '备份结束时间',info.backup_status as '备份状态' from db_backup_policy as policy left join db_backup_info as info on policy.id=info.policy_id and info.backup_date='${monitor_date}' where find_in_set(${monitor_week},policy.weekday)>0 and policy.is_backup=0 \G;"
echo "${sql01}" | mysql_conn02 > ${m_log_dir}/monitor_backup_info_${monitor_d}.log
}
# 读取log文件中的内容
function get_backup_info_from_log(){
# 判断日期
monitor_date_judge
judge_date_num=${monitor_date_judge_num}
backup_info_log=${m_log_dir}/monitor_backup_info_${monitor_d}.log
if [ ${judge_date_num} -eq 0 ]
then
# 查询当天备份信息并输出到log文件
set_backup_info_to_log
# 展示
cat ${backup_info_log}
elif [ ${judge_date_num} -eq 1 ]
then
if [ ! -f "${backup_info_log}" ];then
# 查询该天备份信息并输出到log文件
set_backup_info_to_log
cat ${backup_info_log}
else
cat ${backup_info_log}
fi
else
echo "备份保存最大期限为一周,您已超出查询范围,或者您输入的日期属于未来!"
fi
}
# 查询备份信息并输出到html文件
function set_backup_info_to_html(){
sql02="select concat('<tr><td>',if(info.backup_date is null,'No Backup',info.backup_date),'</td>') as '<tr><th>日期</th>',concat('<td>',inet_ntoa(policy.ip),'</td>') as '<th>备份IP</th>',concat('<td>',policy.port,'</td>') as '<th>端口号</th>',concat('<td>',policy.db,'</td>') as '<th>备份实例</th>',concat('<td>',policy.begintime,'</td>') as '<th>预定备份时间</th>',concat('<td>',policy.mode,'</td>') as '<th>备份模式</th>',concat('<td>',inet_ntoa(policy.dest_ip),'</td>') as '<th>保存地址IP</th>',concat('<td>',if(info.dest_dir is null,'NULL',info.dest_dir),'</td>') as '<th>备份保存路径</th>',concat('<td>',policy.weekday,'</td>') as '<th>一周备份日期</th>',concat('<td>',policy.remain_days,'</td>') as '<th>备份保存天数</th>',concat('<td>',if(info.begintime is null,'NULL',info.begintime),'</td>') as '<th>备份开始时间</th>',concat('<td>',if(info.endtime is null,'NULL',info.endtime),'</td>') as '<th>备份结束时间</th>',concat('<td>',if(info.backup_status is null,'NULL',info.backup_status),'</td></tr>') as '<th>备份状态</th></tr>' from db_backup_policy as policy left join db_backup_info as info on policy.id=info.policy_id and info.backup_date='${monitor_date}' where find_in_set(${monitor_week},policy.weekday)>0 and policy.is_backup=0;"
backup_info_html=${m_html_dir}/monitor_backup_info_${monitor_d}.html
# 生成HTML页面
echo "<!DOCTYPE html>" > ${backup_info_html}
echo "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">" >> ${backup_info_html}
echo "<html>" >> ${backup_info_html}
echo "<head>" >> ${backup_info_html}
echo "<title>Database Monitoring Status</title>" >> ${backup_info_html}
echo "</head>" >> ${backup_info_html}
echo "<style>" >> ${backup_info_html}
echo "table {" >> ${backup_info_html}
echo "width:100%;" >> ${backup_info_html}
echo "text-align:center;" >> ${backup_info_html}
echo "border-collapse:collapse;" >> ${backup_info_html}
echo "border-spacing:0;" >> ${backup_info_html}
echo "overflow-x: auto;" >> ${backup_info_html}
echo "}" >> ${backup_info_html}
echo "table td {" >> ${backup_info_html}
echo "word-break: break-all;" >> ${backup_info_html}
echo "word-wrap:break-word;" >> ${backup_info_html}
echo "border-right:1px solid #939598;" >> ${backup_info_html}
echo "border-bottom:1px solid #939598;" >> ${backup_info_html}
echo "font:500 14px Arial;" >> ${backup_info_html}
echo "white-space:nowrap;" >> ${backup_info_html}
echo "min-width: 100px;" >> ${backup_info_html}
echo "}" >> ${backup_info_html}
echo "</style>" >> ${backup_info_html}
echo "<body style=\"font-size:16px\">" >> ${backup_info_html}
echo "<p>HIGO MySQL 备份状态信息:</p>" >> ${backup_info_html}
echo "<table border=\"1\">" >> ${backup_info_html}
# 通过SQL循环插入表格及内容
echo "${sql02}" | mysql_conn02 >> ${backup_info_html}
echo "</table>" >> ${backup_info_html}
echo "</body>" >> ${backup_info_html}
echo "</html>" >> ${backup_info_html}
}
# 获取HTML文件并通过邮件发送
function get_backup_info_from_html(){
# 判断日期
monitor_date_judge
judge_date_num=${monitor_date_judge_num}
backup_info_html=${m_html_dir}/monitor_backup_info_${monitor_d}.html
if [ ${judge_date_num} -eq 0 ]
then
# 查询当天备份信息并输出到html文件
set_backup_info_to_html
# 发邮件
email_subject="${monitor_date}MySQL备份状态最新信息"
mutt ${m_to_email} -c ${m_to_email_other} -s ${email_subject} -e 'set content_type="text/html"' < ${backup_info_html}
elif [ ${judge_date_num} -eq 1 ]
then
if [ ! -f "${backup_info_html}" ];then
# 查询该天备份信息并输出到html文件
set_backup_info_to_html
# 发邮件
email_subject="${monitor_date}MySQL备份状态信息"
mutt ${m_to_email} -s ${email_subject} -e 'set content_type="text/html"' < ${backup_info_html}
else
# 发邮件
email_subject="${monitor_date}MySQL备份状态信息"
mutt ${m_to_email} -s ${email_subject} -e 'set content_type="text/html"' < ${backup_info_html}
fi
else
echo "备份保存最大期限为一周,您已超出查询范围,或者您输入的日期属于未来!"
fi
}
# 删除备份保存天数以前的无效log、html文件
function delete_log_and_html_file(){
find ${m_log_dir} -type f -mtime +${m_remain_days} | xargs rm -f
find ${m_html_dir} -type f -mtime +${m_remain_days} | xargs rm -f
}
# 每天计划任务
function scheduling_send_email_to_dba(){
# 获取昨天相关时间值
monitor_yesterday=`date +%F -d -1day`
monitor_date=${monitor_yesterday}
monitor_week=`date -d "${monitor_date}" +%w`
if [ ${monitor_week} -eq 0 ];then
monitor_week=`expr ${monitor_week} + 7`
fi
monitor_d=`date -d "${monitor_date}" +%Y%m%d`
# 查询备份信息并输出到log文件
backup_info_log=${m_log_dir}/monitor_backup_info_${monitor_d}.log
set_backup_info_to_log
# 查询备份信息并输出到html文件
backup_info_html=${m_html_dir}/monitor_backup_info_${monitor_d}.html
set_backup_info_to_html
# 删除备份保存天数以前的无效log、html文件
delete_log_and_html_file
# 发邮件
email_subject="${monitor_date}MySQL备份状态信息"
mutt ${m_to_email} -c ${m_to_email_other} -s ${email_subject} -e 'set content_type="text/html"' < ${backup_info_html}
}
function main(){
if [ ${monitor_output_judge} -eq 0 ]
then
get_backup_info_from_log
elif [ ${monitor_output_judge} -eq 1 ]
then
get_backup_info_from_html
elif [ ${monitor_output_judge} -eq 2 ]
then
scheduling_send_email_to_dba
else
echo "parameter error occurs."
usage
exit
fi
}
main
2. 全局参数配置文件:backup_monitor_wf.conf
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Program : MySQL 配置文件 #
# Author : wufei@xxxxx.com #
# Date : 2018-09-12 #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# MySQL备份信息数据库连接用户
m_user=backup_wf
# MySQL备份信息数据库连接密码
m_passwd=xxxxx
# MySQL备份信息数据库
db_name=wf_dbbackup
# MySQL备份策略表
tb_policy=db_backup_policy
# MySQL备份信息表
tb_info=db_backup_info
# MySQL备份信息数据库IP
m_host=xx.xx.1.49
# MySQL备份信息数据库端口
m_port=3303
# HTML文件保存路径
m_html_dir=/home/wufei/monitor/html
# 日志文件保存路径
m_log_dir=/home/wufei/monitor/log
# 添加收信人邮箱
m_to_email=wufei@xxxxx.com
# 添加抄送人邮箱(多人以逗号分隔)
m_to_email_other=xxxx@xxxxx.com
# log文件与html文件保存天数(默认与备份保存天数一致)
m_remain_days=6