欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

mysql数据库自动备份,并发送邮件通知

程序员文章站 2022-04-25 20:57:19
...

一前提系统环境:centos6.1脚本作用:备份指定服务器上的指定数据库,并通过scp传送到另一台备份主机,然后发送备份是否成功邮件到指定邮箱思路:首先在备份脚本

一 前提

系统环境:centos6.1

脚本作用:备份指定服务器上的指定数据库,并通过scp传送到另一台备份主机,然后发送备份是否成功邮件到指定邮箱

思路:首先在备份脚本中把备份报告导出为一个db_backup.log文件,然后邮件脚本会把这个log内容作为邮件内容发送给指定用户,也就实现了邮件通知

说明:如果需要备份到远程主机,需要首先做好ssh密钥认证,scp要使用到


二 备份脚本

备份脚本用法:如果需要备份某个数据库,在脚本后面添加一条备份命令,像下面这样

backup_db 10.90.5.50 dbname mysql 123456 innodb
函数名 mysql所在IP 所要备份的数据库名 连接数据库用户名 密码 数据库引擎类型

#!/bin/bash #name:backup_db.sh #Purpose: backup mysql database #Author: xxx #Date: 2014-08-26 #useage:backup_db IP databasename user password dbtype #example:backup_db 10.90.5.50 dbname mysql 123456 innodb #备份时间 DATE=`date -d"today" +%Y%m%d_%H%M%S` #前一个时间 DATE2=`date -d"1 month ago" +%Y%m%d_%H%M%S` #备份目录 HOMEDIR=/data/dbdata_backup MYSQLDUMP='/usr/local/mysql/bin/mysqldump' #删除上一次备份报告 rm -f $HOMEDIR/db_backup.log #################################################################function############################################################################ #backup function backup_db () { if [ $# -ne 5 ];then echo "Usage: backupdb IP dbname user password dbtype" echo "Please try again" exit 1 fi IP=$1 DBNAME=$2 USER=$3 PASSWORD=$4 DBTYPE=$5 echo "--------------------------start backup $IP $DBNAME db-------------------------------------" >> $HOMEDIR/db_backup.log echo "start date: `date +%Y%m%d_%H%M%S`" >> $HOMEDIR/db_backup.log if [ $DBTYPE = "innodb" ];then $MYSQLDUMP -u$USER -p$PASSWORD -h$IP --single-transaction --flush-logs --routines --events --master-data=2 "$DBNAME" > $HOMEDIR/$DBNAME/${DBNAME}_$DATE.sql if [ $? -ne 0 ];then echo "$IP $DBNAME $DBTYPE db backup is not success ,please check it out!" >> $HOMEDIR/db_backup.log else echo "$IP $DBNAME $DBTYPE db backup is success!" >> $HOMEDIR/db_backup.log fi else $MYSQLDUMP -u $USER -p$PASSWORD -h$IP -l --flush-logs --routines --events --master-data=2 "$DBNAME" > $HOMEDIR/$DBNAME/${DBNAME}_$DATE.sql if [ $? -ne 0 ];then echo "$IP $DBNAME $DBTYPE db backup is not success ,please check it out!" >> $HOMEDIR/db_backup.log else echo "$IP $DBNAME $DBTYPE db backup is success!" >> $HOMEDIR/db_backup.log fi fi echo "End date: `date +%Y%m%d_%H%M%S`" >> $HOMEDIR/db_backup.log gzip $HOMEDIR/$DBNAME/${DBNAME}_$DATE.sql SIZE=`du -sh $HOMEDIR/$DBNAME/${DBNAME}_$DATE.sql.gz | awk '{print $1}'` echo "the file size is: $SIZE" >> $HOMEDIR/db_backup.log echo "--------------------------end backup $IP $DBNAME db-------------------------------------" >> $HOMEDIR/db_backup.log #delete the backup files which were create two month ago rm -f $HOMEDIR/$DBNAME/${DBNAME}_$DATE2.sql.gz } #scp function : scp the backup file to another server as different place backup scp_function () { if [ $# -ne 1 ];then echo "Usage: scp_function dbname" echo "Please try again" exit 1 fi DBNAME=$1 scp -P52814 -i /home/lj/.ssh/id_rsa $HOMEDIR/$DBNAME/${DBNAME}_$DATE.sql.gz lj@10.90.5.50:/data/dbbackup/ if [ $? -ne 0 ];then echo "$DBNAME db backup data transfer is not success ,please check it out!" >> $HOMEDIR/db_backup.log else echo "$DBNAME db backup data transfer is success!" >> $HOMEDIR/db_backup.log fi } ##########################################################################function################################################################## #使用perl命令执行邮件脚本,,以发送备份报告,其中的sendmail.pl为下面发邮件脚本 perl /home/scripts/sendmail.pl


三 发送邮件脚本