AIWALY mysql生产标准备份脚本(更新于20190408)

[root@aws10-27-101-84-flb ~]# cat   /data/script/mysqldb_backup.sh
#!/bin/bash
MYSQLDBUSERNAME=root
MYSQLDBPASSWORD=Mya
MYSQBASEDIR=/usr/local/mysql
MYSQL=/usr/bin/mysql
MYSQLDUMP=/usr/bin/mysqldump
BACKDIR=/back/mysqldb/
DATEFORMATTYPE1=$(date +%Y-%m-%d)
DATEFORMATTYPE2=$(date +%Y%m%d%H%M%S)

#[ -d $MYSQBASEDIR ] && MYSQDATADIR=$MYSQBASEDIR/data || MYSQDATADIR=/var/lib/mysql
#[ -d $MYSQBASEDIR ] && MYSQDATADIR=$MYSQBASEDIR/data || MYSQDATADIR=/var/lib/mysql
MYSQDATADIR=/awdisk0006/dbdata/mysql/

[ -x $MYSQL ] || MYSQL=mysql
[ -x $MYSQLDUMP ] || MYSQLDUMP=mysqldump
[ -d ${BACKDIR} ] || mkdir -p ${BACKDIR}
[ -d ${BACKDIR}/${DATEFORMATTYPE1} ] || mkdir ${BACKDIR}/${DATEFORMATTYPE1}
DBLIST=`ls -p $MYSQDATADIR | grep / |tr -d /`
Dbnum=`ls -p $MYSQDATADIR | grep / |wc -l`
for DBNAME in $DBLIST
        do ${MYSQLDUMP} --user=${MYSQLDBUSERNAME} --password=${MYSQLDBPASSWORD} --routines --events --triggers --single-transaction --flush-logs --databases ${DBNAME} | gzip > ${BACKDIR}/${DATEFORMATTYPE1}/${DBNAME}-backup-${DATEFORMATTYPE2}.sql.gz
        [ $? -eq 0 ] && echo "${DBNAME} has been backuped successful" || echo "${DBNAME} has been backuped failed"
    /bin/sleep 5
done

let keepNum=Dbnum*30
fileNum=$(find ${BACKDIR} -mtime +1 -name "*.gz" | wc -l)
if test $[fileNum] -gt $[keepNum];then
     find ${BACKDIR} -maxdepth 1 -mtime +30 -name '*.gz' -exec rm -f {} \;
else
     echo "do no thing delete"
fi
文档更新时间: 2019-12-16 06:38   作者:月影鹏鹏