#!/bin/bash
DB_USER="root" #数据库用户名
DB_PASS="12345678" #数据库密码
BACK_DIR="/backup/dbbak" #备份目录
LogFile=/usr/dbbakup.log #日志记录保存文件
MYCMD="/usr/local/mysql/bin/mysql -u$DB_USER -p$DB_PASS -hlocalhost"
MYDUMP="/usr/local/mysql/bin/mysqldump -u$DB_USER -p$DB_PASS -hlocalhost"
dtime=`date +%m-%d-%H`
for database in `$MYCMD -e "show databases;"|sed '1,2d' |egrep -v "mysql|performance_schema|information_schema|sys"` #不需要备份的库
do
if [ ! -d $BACK_DIR/${database} ]; then
mkdir $BACK_DIR/${database} -p
fi
cd $BACK_DIR/${database} && $MYDUMP --single-transaction --set-gtid-purged=OFF ${database} > ${database}
dbakfile=${database}.$dtime.tar.gz
tar zcf $dbakfile ${database} --remove-files
echo ----------"$(date +"%y-%m-%d %H:%M:%S")"------------ >> $LogFile
find "$BACK_DIR/${database}" -ctime +3 -type f -name "*.gz" -print > /usr/deleted.txt
echo -e "delete files:
" >> $LogFile
#循环删除匹配到的文件
cat /usr/deleted.txt | while read LINE
do
rm -rf $LINE
echo $LINE>> $LogFile
done
done
echo "---------------------------------------------------------------" >> $LogFile