#创建目录和文件
mkdir -p /opt/scripts/
cd /opt/scripts/
touch mysqldump_backup.sh
chmod 744 mysqldump_backup.sh
mkdir -p /data/dump_back/dump_sql/
#!/bin/sh
#lf
#20170807
#crontab -e
#00 2 * * * /opt/scripts/mysqldump_backup.sh
#
BASEDIR=/data/dump_back/dump_sql/
INFOLOG=/data/dump_back/info.log
MYSQL_BACKUP_EXPIRED_DAYS=7
mysqldumpDIR=/u01/app/mysql/db/bin/mysqldump
USER=backup
PASSWORD=backup_password
HOST=192.168.111.210
POST=3306
#服务状态回传url
BackPostUrl=0
#服务ID
T_ServerId=0
FileSize=0
DATE=`date +"%Y%m%d"`
mkdir -p ${BASEDIR}
rm -rf ${BASEDIR}${DATE}_back.sql
#--databases db1 db2 导出指定db
${mysqldumpDIR} -u${USER} -p${PASSWORD} -h${HOST} -P${POST} --master-data=2 --single-transaction --all-databases --opt --
hex-blob --routines --triggers --events >${BASEDIR}${DATE}_back.sql
if [ $? -eq 0 ];then
sync
cd ${BASEDIR}
tar -czvf ${DATE}.tar.gz ${DATE}_back.sql
FileSize=`du -sh ${DATE}.tar.gz | awk '{print $1}'`
rm -rf `date +"%Y%m%d"`_back.sql
sync
echo "`date +"%Y-%m-%d %H:%M:%S"` ok" >> ${INFOLOG}
else
echo "`date +"%Y-%m-%d %H:%M:%S"` error" >> ${INFOLOG}
fi
find ${BASEDIR} -maxdepth 1 -type f -mtime "+${MYSQL_BACKUP_EXPIRED_DAYS}" |xargs /bin/rm -fr
# less back.sql 查看change master
backinfo=`awk 'END {print}' ${INFOLOG}`
ZSIZE=`df -h / | awk '{print $4}' | sed -n '2p'`
#备份会回传备份状态和文件 磁盘大小
curl -d "T_ServerId=${T_ServerId}&BackUpInfo=${backinfo}&BackType=dump&FileSize=${FileSize}&ZSIZE=${ZSIZE}"
${BackPostUrl} >/dev/null 2>&1