1. 创建 Shell 脚本 backup.sh
PATH=$PATH:$HOME/bin backupPath="/home/_backup/fruits/" backupFiles=$backupPath"h_"$(date +%Y%m%d_%H%M)".zip" backupSqlFile=$backupPath"h_"$(date +%Y%m%d_%H%M)".sql" if !(test -d $backupPath) then mkdir $backupPath fi #备份数据库 dbUser="用户" dbPwd="密码" mysqldump -u$dbUser -p$dbPwd --default-character-set=utf8mb4 数据库名 > "$backupSqlFile" #备份文件 zip -r "$backupFiles" "站点文件根目录" #备份数据保留 30 天 find "$backupPath" -name 'h_*[sql,zip]' -type f -mtime +30 -exec rm -rf {} \;
2. 配置定时任务
sudo crontab -e
30 1 * * * /home/_backup/backup.sh
3. 重启 CRON 服务应用任务
sudo service cron restart