Linux自动备份MySQL

1、编写Shell脚本

// 编写backuper.sh
[root@localhost ~]# cd /root/mysql/backup
[root@localhost backup]# vim backuper.sh
#备份目录
BACKUP_HOME=/root/mysql/backup
DB_USERNAME=用户名
DB_PASSWORD=密码
#当前日期
DATE=$(date +%Y%m%d_%H%M%S)
#查询所有数据库
DATABASES=$(mysql -u${DB_USERNAME} -p${DB_PASSWORD} -e "show databases" | grep -Ev "Database|information_schema")
#循环数据库进行备份
for db in $DATABASES
do
BACKUP_FILEDIR=$BACKUP_HOME/${db}
[ -d $BACKUP_FILEDIR ] || mkdir -p $BACKUP_FILEDIR
mysqldump -u${DB_USERNAME} -p${DB_PASSWORD} ${db} | gzip > $BACKUP_FILEDIR/${db}_$DATE.sql.gz
done

2、测试Shell脚本

[root@localhost backup]# bash backuper.sh

2、设置自定义任务

// 查看定时任务
[root@localhost backup]# crontab -l

// 设置定时任务
[root@localhost backup]# crontab -e
59 23 * * * /usr/bin/bash /root/mysql/backup/backuper.sh

你可能感兴趣的:(Linux,MySQL)