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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM