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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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/shor by the shell specified in theSHELLvariable 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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0