zoukankan      html  css  js  c++  java
  • MySQL使用crontab定时备份不执行问题

    在使用crontab定时备份数据库时,发现并没有执行备份命令.

    下面是定时备份的代码:

    30 1 * * * /usr/local/mysql/bin/mysqldump --defaults-extra-file=/usr/local/mysql/etc/my.cnf yxv9 | gzip > /root/sqldb/yw/ywv9_`date '+%m-%d-%Y'`.sql.gz
    
    

    但是直接如下面一样执行命令时,,却可以备份.

    /usr/local/mysql/bin/mysqldump --defaults-extra-file=/usr/local/mysql/etc/my.cnf yxv9 | gzip > /root/sqldb/yw/ywv9_`date '+%m-%d-%Y'`.sql.gz
    

    这到底怎么回事呢?

    通过查看crontab的执行日志竟然发现任务出错了!

    
    /bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
    /bin/sh: -c: line 1: syntax error: unexpected end of file
    
    

    从上面的出错信息可以看到,一个cmd被从中间截断了,脚本在 /bin/date +"之后就被截断了,出现了语法错误。

    原来是让%闹得.

    The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a new‐line or a "%" character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile. A "%" character in the command, unless escaped with a backslash (), will be changed into newline characters, and all data after thefirst % will be sent to the command as standard input.

    原来是 % 这个符号在crontab里面被翻译成了命令结束,所以当crontab读到%就吧后面的内容截取掉了,导致脚本执行失败。

    那么怎么解决呢?

    在%前面加上转移符号""即可,如下:

    30 1 * * * /usr/local/mysql/bin/mysqldump --defaults-extra-file=/usr/local/mysql/etc/my.cnf yxv9 | gzip > /root/sqldb/yw/ywv9_`date '+\%m-\%d-\%Y'`.sql.gz
    
  • 相关阅读:
    PTA考试几点注意事项
    网易云信在融合通信场景下的探索和实践之 SIPGateway 服务架构
    破旧立新,精准测试之道
    从 0 到 1 构建实时音视频引擎
    云信小课堂|如何实现音视频通话
    Python 回调函数实现异步处理
    数据结构--链表--约瑟夫问题
    Python 轻松实现ORM
    leetcode 递归编程技巧-链表算法题
    Tornado 初识
  • 原文地址:https://www.cnblogs.com/cnlihao/p/7201367.html
Copyright © 2011-2022 走看看