[[email protected] myfile]# cat mysqlback_zabbix.sh #!/bin/bashuser=rootdb=zabbix# password=123456server=172.16.9.129dir_home=/vol/myfile/mysql_$serverlog_file= $DIR _home/logs/mysqlback.logif [ ! -d $DIR _home ];then mkdir $DIR _home ;fiif [ ! -d $DIR _home/logs ];then mkdir $DIR _home/logs ;fidate= ' date +%f ' echo ' ******* begin********** ' >> $LOG _filedate >> $LOG _file# Automatically delete backup data two days ago date_two_ago= ' date -d ' 2 days ago ' +%f ' if [ -d $DIR _home/$DATE _two_ago ] ;then rm -rf $DIR _home/$DATE _two_ago >> $LOG _file 2>&1 if [ $? == 0 ];then echo "$DIR _home/ $DATE _two_ago removed successfully! " >> $LOG _file else echo "$DIR _home/$DATE _two_ago Delete failed! " >> $LOG _file fifi# start backing up file_name= $DIR _home/$DATEif [ ! -d $ file_name ];then mkdir $FILE _name ;fimysqldump -u$user $DB >> $LOG _ file 2>&1 > $FILE _name/$DB .sql if [ $? == 0 ];then echo "* * $DB * * Backup complete! ">> $LOG _file else #失败邮件报警 content=$ (cat $LOG _file | tail -2 | grep mysqldump) /usr/local/zabbix/share/zabbix/alertscripts/ sendmail.sh [email protected] "Mysqldump error" "$CONTENT" fiecho ' ******** **end********** ' >> $LOG _file
Senfmail.sh content:
[Email protected] myfile]# cat/usr/local/zabbix/share/zabbix/alertscripts/sendmail.sh#!/bin/bash# $ sendmail address# $ sendmail subject# $ content[email protected]send_mail=/usr/local/zabbix/share/zabbix/alertscripts/ Sendemail-v1.56/sendemailserver_mail=mail.benguo.cn$send_mail-f $sender-T "$"-S $server _mail-u "$" m "$ $" >> /dev/null
SendMail:sendemail-v1.56.tar.gz
Add to the scheduled task:
[Root[email protected] myfile]# crontab-e 1 * */vol/myfile/mysqlback_zabbix.sh #每天的1点1分自动执行备份脚本
MySQL backup script and backup failure alarm