mysql备份监控脚本

mysql备份监控脚本

cat monitor_binlog_backup.sh 
#!/bin/bash
#author: royoy
#date: 20170609
#desc: monitor for all scripts of mysqlbinlog

#monitor log file
monitor_log_file="/data/backup/binlog/mysqlbinlog_monitor.log"

#backup log file
back_log_file="/data/backup/binlog/backup.log"

#plat info
plat_info=(
'sc_xtmlygame_backup_binlog 10.14.11.121 /data/backup/binlog/sc/xtmlygame/'
'sc_passpt_backup_binlog 10.14.11.17 /data/backup/binlog/sc/passpt/'
'hc_xtmlygame_backup_binlog 10.1.10.50 /data/backup/binlog/hc/xtmlygame/'
'hc_passpt_backup_binlog 10.1.10.40 /data/backup/binlog/hc/passpt/'
'c99_backup_binlog 10.150.0.230 /data/backup/binlog/99c/xtmlygame/'
'jy_backup_binlog 10.6.10.9 /data/backup/binlog/jyc/xtmlygame/'
'jjly_backup_binlog 10.15.0.11 /data/backup/binlog/jjly/xtmlygame/'
'jx_backup_binlog 10.11.186.200 /data/backup/binlog/jxc/xtmlygame/')

#check binlog
check_binlog(){
	cd $dir_binlog && newest_binlog="`ls -l mysql-bin.* |awk 'END{print $NF}'`"	
	[ "`date +"%F %H:%M"`" == "`stat -c %y "$newest_binlog" |awk -F":" '{print $1":"$2}'`" ] || return 9
}

#auto fix module
auto_fix_module(){
	scripts_pid="`ps -ef |grep "$script_name" |awk '!/grep/{print $2}'`"
	mysqlbinlog_pid="`ps -ef |grep 'mysqlbinlog' | grep "host=${ip}" |awk '{print $2}'`"
	echo "script_pid is $scripts_pid, mysqlbinlog_pid is $mysqlbinlog_pid" >> ${monitor_log_file}
	echo "`date +"%F %T"` start to try to fix..." >> ${monitor_log_file}
	[ ${scripts_pid} ] && {
		tmp_var1=${scripts_pid}; echo "try to kill $tmp_var1" >>${monitor_log_file}
		kill ${scripts_pid} && sleep 5 && echo "kill $tmp_var1 finished" >> ${monitor_log_file}
	}
	[ ${mysqlbinlog_pid} ] && {
		tmp_var2=${mysqlbinlog_pid}; echo "try to kill $tmp_var2" >> ${monitor_log_file}
		kill ${mysqlbinlog_pid} && sleep 3 && echo "kill $tmp_var2 finished" >> ${monitor_log_file}
	}
	echo "`date +"%F %T"` try to start mysqlbinlog daemon script..." >> ${monitor_log_file}
	/usr/bin/nohup /home/royoy/${script_name}.sh >> ${back_log_file} 2>&1 &
	sleep 3; check_binlog && \
	echo "`date +"%F %T"` fix finished, ${dir_binlog}${newest_binlog} rsync now is OK" >> ${monitor_log_file} \
	|| echo "`date +"%F %T"` fix failed" >> ${monitor_log_file}
}

#
for i in "${plat_info[@]}"
do

	echo "${i[@]}"  | while read script_name ip dir_binlog
	do
		[ ${script_name} -a ${ip} -a ${dir_binlog} ] && {
			check_binlog || {
				sleep 10 ;check_binlog && exit || sleep 10 ; check_binlog && exit || sleep 10; check_binlog && exit
				echo "------------------------------------------------------------------------------------------------------------------" >> ${monitor_log_file}
				echo "`date +"%F %T"` check info : ${dir_binlog}${newest_binlog} not rsync OR not have latest data" >> ${monitor_log_file}
				#auto_fix_module
			}
		} 
	done
done

#end

注: 备份监控脚本中,"auto_fix_module" 函数注释未使用,要使用直接取消注释即可