mysql_backup_start.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. service="/sbin/service"
  2. # 检查cron服务状态
  3. cron_status=$(service cron status)
  4. if grep -q "unrecognized" <<< "$cron_status"
  5. then
  6. echo "cron未安装"
  7. elif grep -q "is not running" <<< "$cron_status"
  8. then
  9. echo "正在启动 cron 服务"
  10. sudo service cron start
  11. elif grep -q "is running" <<< "$cron_status"
  12. then
  13. echo "cron已启动"
  14. else
  15. echo "未知状态"
  16. fi
  17. # 检查自启动状态
  18. if [ `systemctl list-unit-files cron.service | grep -c -e "cron.service\senabled"` -ne '0' ]
  19. then
  20. echo "开启启动已设置"
  21. else
  22. echo "正在设置开机启动"
  23. sudo systemctl enable cron
  24. fi
  25. CURRENT_DIR=$(cd "$(dirname "$0")";pwd)
  26. find_file="${CURRENT_DIR}/cron.conf"
  27. find_str="mysql_backup_run"
  28. crontab -l > ${find_file}
  29. task="0 4 * * * bash ${CURRENT_DIR}/mysql_backup_run.sh"
  30. # 检查记录中是否有mysql_backup_run条目数据
  31. if [ `grep -c "${find_str}" ${find_file}` -ne '0' ]
  32. then
  33. echo "已存在备份计划任务"
  34. echo "若需更新备份计划任务,请运行mysql_backup_stop.sh删除备份计划任务,再运行本脚本"
  35. else
  36. echo "正在添加备份计划任务"
  37. echo "$task" >> ${find_file}
  38. crontab ${find_file}
  39. fi
  40. echo "最终cron任务如下:"
  41. cat ${find_file}
  42. rm -f ${find_file}
  43. if [ `crontab -l | grep -c "$find_str"` -ne '0' ];then
  44. echo "启动成功!"
  45. else
  46. echo "启动失败!"
  47. fi