#!/bin/bash

#记录备份的时间

echo "-----"$(date +"%y-%m-%d %H:%M:%S")"-----" >> /data/backup_mysql/record.log

#定义变量

MYUSER=root

MYPASSWORD=29749716#@Heiniao
SOCKET=/data/mysql/mysql.sock
MYCMD="mysql -u $MYUSER -p$MYPASSWORD -S $SOCKET"
MYDUMP="mysqldump -u $MYUSER -p$MYPASSWORD -S $SOCKET"
BACKUPDIR=/data/backup_mysql

#正式备份

for db in `$MYCMD -e "show databases;"|sed '1,2d' | egrep -v "mysql|performance_schema"`

do
        $MYDUMP $db > ${BACKUPDIR}/${db}_$(date +%F).sql
done
        cd ${BACKUPDIR}
        tar czvf ${BACKUPDIR}/$(date +%F).tar.gz ./*_$(date +%F).sql
        rm -rf ${BACKUPDIR}/*.sql
       find ${BACKUPDIR} -mtime +30 -name "*.sql.tar.gz" -exec rm -rf {} \;