linux定时任务配置mysql数据库备份脚本

 创建备份后保存路径:

mkdir -p /usr/local/mysql/backupDataFiles

 进入目录:

cd /usr/local/mysql/backupDataFiles

 编写数据库备份脚本:backupData.sh

#!/bin/bash
baseDir="/usr/local/mysql/backupDataFiles";
cd "$baseDir";
echo "start backup data ..."
#原sql文件
#/bin/nice -n 19 /usr/local/mysql/bin/mysqldump -u root -p'password' ecdsdb > /usr/local/mysql/backupDataFiles/mysqldb_$(date +%Y%m%d).sql;
#压缩包
/bin/nice -n 19 /usr/local/mysql/bin/mysqldump -u root -p'password' ecdsdb | gzip > /usr/local/mysql/backupDataFiles/mysqldb_$(date +%Y%m%d).sql.gz;
echo "end backup data !!!"
cd "$baseDir";
oldDate=`date --date='8 day ago' +%Y%m%d`;
# delete oldfiles
echo `rm -rf mysqldb_$oldDate*`;
echo "***end***"
cd "$baseDir";

 赋权:

chmod 775 backupData.sh

 配置定时任务:

crontab -e

每天凌晨1点执行:

0 1 * * *  /usr/local/mysql/backupDataFiles/backupData.sh

刷新定时任务:

service crond reload

End      2022年12月4日

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