该脚本支持自动备份,自动上传到FTP服务器,备份失败自动发送报警邮件
#!/bin/sh
#Create by JIANGLEI.YU ON 20160421
#使用测脚本需要先安装mutt邮件发送工具。
#使用使用自动化安装。详细请参考 http://yujianglei.blog.51cto.com/7215578/1765938
#Define MySQL Environments
Hostname='192.168.0.141'
Port='3306'
Username='root'
Password='123456'
Mysqldump=$(which mysqldump)
Gzip=$(which gzip)
Destinaion_dir='/home/bak/database/data'
Timestamp=$(date +%Y%m%d%H%M%S)
Database='virtual'
Alarm_User="15011254881@139.com"
File_Name="$Timestamp-$Database.sql.gz"
#File_Name="20160421110453-virtualsdk.sql.gz"
#Define Ftp Environments
Ftp_Host='192.168.0.136'
Ftp_Port='21'
Ftp_User='yujianglei'
Ftp_Pass='yujianglei'
RPM=$(which rpm)
function check_result(){
if [ $? != 0 ];then
echo "Backup Database Failed" | mutt -s "$Database Backup Database Failed" ${Alarm_User}
exit 1
else
echo "Backup Database Successful" | mutt -s "$Database Backup Database Successful" ${Alarm_User}
fi
}
function bakcup(){
[ ! -d ${Destinaion_dir} ] && mkdir -p ${Destinaion_dir}
Cmd="$Mysqldump -h$Hostname -p$Port -u$Username -p$Password"
Parameter="--flush-privileges --opt -R -E --triggers --single-transaction -F"
$Cmd $Parameter $Database | gzip >${Destinaion_dir}/${File_Name}
check_result $?
}
function upload_ftp(){
$RPM -q ftp
[ $? != 0 ] && yum install ftp -y
ftp -n ${Ftp_Host} ${Ftp_Port} <
user ${Ftp_User} $Ftp_Pass
lcd ${Destinaion_dir}
cd database
put ${File_Name}
quit
END
check_result $?
exit 0
}
bakcup
upload_ftp