来自网上,可以使用,注意先创建
/mysql/backup/files
/mysql/backup/logs
文件夹
#!/bin/bash
#备份目录
BACKUP_ROOT=/mysql/backup
BACKUP_FILEDIR=$BACKUP_ROOT/files
BACKUP_LOGDIR=$BACKUP_ROOT/logs
#当前日期
DATE=$(date +%Y%m%d)
######备份######
#查询所有数据库
#-uroot -p123456表示使用root账号执行命令,且root账号的密码为:123456,-Ev后面是要忽略的数据库
DATABASES=$(mysql -uroot -p'123456' -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'123456' --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"