mydb_`date +%d-%m-%Y`.sql.gz but when ...">

Backup database uses crontab with date function

I can use this command

mysqldump -u"root" myDB| gzip > mydb_`date +%d-%m-%Y`.sql.gz 

but when run in crontab

 * * * * * mysqldump -u"root" myDB| gzip > mydb_`date +%d-%m-%Y`.sql.gz 

(this error occurs by function date, when I delete it, crontab works well)

on ubuntu, this error occurs in the log file.

 ubuntu CRON[xxxx] (user) CMD(mysqldump -u"root" myDB| gzip > mydb_`date+) ubuntu CRON[xxxx] (CRON) error ( grandchild #5353 failed with exit status 2) ubuntu CRON[xxxx] (CRON) info (no MTA installed, discarding output) 
+6
source share
2 answers
Signs

% in the crontab command is converted to newlines, and all data after the first % sent to the stdin command. Replace each % with \% .

(And you only had 4 time fields: * * * * ; you need 5 (you later asked a question).

+13
source

From man 5 crontab :

The `` sixth '' field (the rest of the line) indicates the command that should be working.

The entire command line, up to a new line or% character, will be executed by / bin / sh or the specified shell in the SHELL variable of the crontab file.

The percent signs (%) in the command, if you do not escape with a backslash (), will be changed to newline characters and all data after the first% is sent to as standard input. It is not possible to split a single line command into multiple lines, for example, a shell ending with "\".

0
source

Source: https://habr.com/ru/post/896021/


All Articles