0

I am getting an issue while running a cron job for exporting all databases on my Godaddy Shared hosting Cpanel account. But I have trouble finding the syntax error in below command.

Same command is working on my AWS Ec2.

mysql -N -ubackup -pt -e 'show databases' | while read dbname; do mysqldump -ubackup -p123 --complete-insert -N "$dbname" > /home/test/sqlbackups/'$(date +"%Y-%m-%d")-$dbname'.sql;done

Error Received on Mail:

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

1 Answers1

3

Crontab commands will have all unescaped occurrences of % replaced by newlines. This is from the crontab(5) manual on my system:

The command field (the rest of the line) is the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab. Percent signs (%) in the command, unless escaped with a backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

Your crontab command should look like

mysql -N -ubackup -pt -e 'show databases' | while read dbname; do mysqldump -ubackup -p123 --complete-insert -N "$dbname" > /home/test/sqlbackups/"$(date +"\%Y-\%m-\%d")-$dbname".sql;done

Here, I've also corrected the $(...) that was previously single-quoted (and thus not expanded by the shell).

In general, it's better to put all non-trivial cron jobs in their own scripts and then schedule these instead. That way you have more control over things like this and you are also able to choose the correct interpreter for any particular job (e.g. ksh or bash instead of sh). It additionally makes any subject lines in emails sent to you from the cron daemon more readable.

Kusalananda
  • 333,661