mysql备份脚本完成后且发送信息到邮箱

  1. 云栖社区>
  2. 博客>
  3. 正文

mysql备份脚本完成后且发送信息到邮箱

技术小阿哥 2017-11-27 17:25:00 浏览542
展开阅读全文

#!/bin/sh

#name: mysqlfullbackup.sh

#PS:mysql database full backup

#Write by:i.Stone


#use mysqldump --help get more detail

scriptsDir='pwd'

#mysqlDir=/var/lib/mysql

mysqlDir=/usr/bin/mysql

user=root

passPWD=123456

dataBackupDir=/tmp/mysqlback

eMailFile=$dataBackupDir/email.txt

eMail=mail@163.com

logFile=$dataBackupDir/mysqlbackup.log

DATE="$(date -I)"


echo "" > $eMailFile

echo $(date +"%y-%m-%d %H:%M:%S") >> $eMailFile

cd $dataBackupDir

dumpFile=mysql.$DATE.sql

GZDumpFile=mysql.$DATE.sql.tar.gz


/usr/bin/mysqldump  -u $user -p$passPWD --opt --default-character-set=utf8 --extended-insert=false --triggers -R --hex-blob --all-databases --flush-logs --delete-master-logs -x > $dumpFile


if [[ $? == 0 ]];then

  tar zcf $GZDumpFile $dumpFile >> $eMailFile 2>&1

  echo "BackupFilename:$GZDumpFile" >> $eMailFile

  echo "DataBase Backup Success!" >> $eMailFile

  rm -f $dumpFile


#delete daily backup files.

cd $dataBackupDir/daily

rm -rf *


#Delete old backup files(mtime>2)

$scriptsDir/rmBackup.sh


#Move Backup Files To Backup Server

$scriptsDir/rsyncBackup.sh

  if (( !$? ));then

    echo "Move Backup Files To Backup Server Success!" >> $eMailFile

  else

    echo "Move Backup Files To Backup Server Fail!" >> $emailFile

  fi

else

  echo "DataBase Backup Fail!" >> $eMailFile

fi


echo "------------------------------------------------------------" >> $logFile

cat $eMailFile >> $logFile

cat $eMailFile | mail -s "Mysql Backup" $eMail



本文转自 zhuxtqw 51CTO博客,原文链接:http://blog.51cto.com/1054054/1703246,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论
技术小阿哥
+ 关注