#/bin/bash
#备份by_rsj数据库
#备份目录
BACKUP_ROOT=/home/mysql
BACKUP_FILEDIR=$BACKUP_ROOT/files
BACKUP_LOGDIR=$BACKUP_ROOT/logs
#当前日期
DATE=$(date +%Y-%m-%d)
#前1天日期
oldtime=$(date --date='1 day ago' +%Y-%m-%d)
######备份######
#查询所有数据库
#-uroot -p123456表示使用root账号执行命令,且root账号的密码为:123456,-Ev后面是要忽略的数据库
DATABASES=$(mysql -uroot -p'P9YOn^RDj3' -e "show databases" | grep -Ev "Database|sys|information_schema|performance_schema|mysql")
echo $DATABASES
#循环数据库进行备份
for db in $DATABASES
do
echo
echo ----------$BACKUP_FILEDIR/${db}_$DATE.sql.gz BEGIN----------
mysqldump -uroot -p'P9YOn^gDj3' --default-character-set=utf8 -q --lock-all-tables --flush-logs -E -R --triggers -B ${db} | gzip > $BACKUP_FILEDIR/${db}_$DATE.sql.gz
echo ----------$BACKUP_FILEDIR/${db}_$DATE.sql.gz COMPLETE----------
echo
done
echo "done"
cd /home/mysql/files
echo `rm -rf by_rsj_${oldtime}.sql.gz`