Don't talk nonsense, just go to the script
1. Automatically back up the database specified on mysql and keep the data for the specified number of days.
#!/usr/bin/env bash
dbName="xMyDB" #定义要备份的数据库名
xTime=`date "+%Y-%m-%d_%H:%M:%S"`
bakPath="/data/backup/${dbName}" #备份路径
saveDay=10 #设定保留多少天的数据
if [ ! -d $bakPath ];then
mkdir -p $bakPath
fi
if [ -f /data/backup/${dbName}/${dbName}-${xTime}.sql ];then
rm -rf /data/backup/${dbName}/${dbName}-${xTime}.sql
fi
# 备份zabbix的mysql数据库
mysqldump -u root -pMysqlMiMa $dbName > /data/backup/${dbName}/${dbName}-${xTime}.sql
if [ $? -eq 0 ];then
echo "$xTime dbname:$dbName backup_success" >> /data/backup/${dbName}/${dbName}.log
else
echo "$xTime dbname:$dbName backup_failed" >> /data/backup/${dbName}/${dbName}.log
fi
# 只保留$saveDay天的备份数据,在此之前的备份都删掉,以节约磁盘
find $bakPath -mtime +${saveDay} -type f -name \*.sql -exec rm -rf {} \;
#脚本注释:删除备份目录下指定提案书的文件,文件名后缀是.sql的所有文件
#还可以这么写find $bakPath -mtime +${saveDay} -type f -name \*.sql |xargs rm -f
2. Don't forget to add executable permissions to the script.
chmod +x /data/script/xAutoBackupMysqlForMydb.sh
3. Add the script to the scheduled task
crontab -e
Then write the plan below
#每天早上7.59点备份一次mysql
57 07 * * * sh /data/script/xAutoBackupMysqlForMydb.sh
--------------END------------------May 10, 2020 22:17:09------ ----------------------
-------------------Send a bowl of chicken soup as usual: rush! Waves! -------------------------------