mysql 备份脚本,多数据库

先看备份效果,多个数据库,批量备份 

现在我们开始

  • 创建目录 ,数据,日志,脚本

/home/mysqlbak/data  

/home/mysqlbak/logs  

/home/mysqlbak/scripts

  • 话不多说直接开始  backup.sh
#!/bin/bash
#备份目录
BACKUP_ROOT=/home/mysqlbak
BACKUP_FILEDIR=$BACKUP_ROOT/data
#当前日期
DATE=$(date +%Y%m%d)
######备份######
#查询所有数据库
#-uroot -p123456表示使用root账号执行命令,且root账号的密码为:123456
DATABASES=$(mysql -uroot -p123456 -e "show databases" | grep -Ev "Database|sys|information_schema|performance_schema|mysql")
#循环数据库进行备份
for db in $DATABASES
do
echo
echo ----------$BACKUP_FILEDIR/${db}_$DATE.sql.gz BEGIN----------
mysqldump -uroot -p123456 --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 END----------
done
  • 自动清除计划
#10表示清除十天前的数据备份
echo ----------CLEAN BEGIN----------

find /home/mysqlbak/data -mtime +10 -name "*.gz" -exec rm -rf {} \;

echo ----------CLEAN COMPLETE----------
  •  添加自动计划 crontab -e
00 1 * * * /home/mysqlbak/scripts/backup.sh > /home/mysqlbak/logs/backup.log 2>&1

00 2 * * * /home/mysqlbak/scripts/backup_clean.sh > /home/mysqlbak/logs/backup_full_clean.log 2>&1

你可能感兴趣的:(数据库,mysql,sql)