新建脚本
1
vi /usr/local/mysql/mysql-backup.sh
备份脚本,以及删除 7 日前数据
1
2
3
4
5
6
7
db_user="root"
db_pwd="root"
db_name="test"
bak_dir="/usr/local/mysql/backup"
time="$(date +"%Y%m%d")"
mysqldump -u$db_user -p$db_pwd -h 127.0.0.1 $db_name > $bak_dir/${db_name}_$time.sql;
find $bak_dir -name "$db_name*.sql" -type f -mtime +7 -exec rm -rf {} \; > /dev/null 2>&1;
修改权限
1
chmod 777 /usr/local/mysql/backup/mysql-backup.sh
设置定时任务
1
crontab -e
每天两点执行
1
0 2 * * * /usr/local/mysql/backup/mysql-backup.sh
重启服务
1
service crond restart