[root@spider129 myfile]# cat mysqlback_zabbix.sh #!/bin/bash USER=root DB=zabbix #PASSWORD=123456 SERVER=172.16.9.129 DIR_HOME=/vol/myfile/mysql_$SERVER LOG_FILE=$DIR_HOME/logs/mysqlback.log if [ ! -d $DIR_HOME ];then mkdir $DIR_HOME ;fi if [ ! -d $DIR_HOME/logs ];then mkdir $DIR_HOME/logs ;fi DATE=`date +%F` echo ‘**********BEGIN**********‘ >>$LOG_FILE date >>$LOG_FILE #自动删除两天前的备份数据 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删除成功!" >> $LOG_FILE else echo "$DIR_HOME/$DATE_TWO_AGO删除失败!" >> $LOG_FILE fi fi #开始备份 FILE_NAME=$DIR_HOME/$DATE if [ ! -d $FILE_NAME ];then mkdir $FILE_NAME ;fi mysqldump -u$USER $DB >>$LOG_FILE 2>&1 >$FILE_NAME/$DB.sql if [ $? == 0 ];then echo "**$DB**备份完成!">>$LOG_FILE else #失败邮件报警 CONTENT=$(cat $LOG_FILE | tail -2 | grep mysqldump) /usr/local/zabbix/share/zabbix/alertscripts/sendmail.sh xxxx@benguo.cn "mysqldump ERROR" "$CONTENT" fi echo ‘**********END**********‘ >>$LOG_FILE
senfmail.sh内容:
[root@spider129 myfile]# cat /usr/local/zabbix/share/zabbix/alertscripts/sendmail.sh #!/bin/bash # $1 sendmail address # $2 sendmail subject # $3 content sender=zabbix@benguo.cn send_mail=/usr/local/zabbix/share/zabbix/alertscripts/sendEmail-v1.56/sendEmail server_mail=mail.benguo.cn $send_mail -f $sender -t "$1" -s $server_mail -u "$2" -m "$3" >>/dev/null
sendmail下载地址:sendEmail-v1.56.tar.gz
添加到计划任务里面:
[root@spider129 myfile]# crontab -e 01 1 * * * /vol/myfile/mysqlback_zabbix.sh #每天的1点1分自动执行备份脚本
原文地址:http://pynliu.blog.51cto.com/5027391/1618951