5

I am configuring Cron to backup my sql automatically. However I think that Cron has some issues and it's not working well.

This is the command I am running:

mysqldump --opt -Q -uhereisthename -p'hereisthepasswordwithstrangecharactersthatmustbeescaped' databasename | gzip > /home2/username/backups/backupnamefolder/backupdbwebsitename.`date +"%Y-%m-%d"`.gz

When I run it via SSH it works fine and generates the backup. However if I run it via Cron, I get the following error:

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

Anybody can suggest what's wrong?

Pikk
  • 2,343
  • 6
  • 25
  • 41

1 Answers1

5

Cron treats % as a special character (meaning "new line", hence the references to lines 0 and 1 in the error message). You need to escape it:

date "+\%Y-\%m-\%d"

By the way, the posix $( ) syntax is generally better than backticks - it allows nested commands.

joews
  • 29,767
  • 10
  • 79
  • 91
  • Hi, the Shell I used is Bash and it works there. I have done as you said. I cron the command is now `bash -c "mysqldump --opt -Q -uhereisthename -p'hereisthepasswordwithstrangecharactersthatmustbeescaped' databasename | gzip > /home2/username/backups/backupnamefolder/backupdbwebsitename.`date +"%Y-%m-%d"`.gz"` But still I get exactly the same two lines of error starting with `/bin/sh: -c: line` – Pikk Oct 12 '13 at 21:56
  • Sorry, I realised what was wrong shortly after posting - see my edited answer. – joews Oct 12 '13 at 21:57
  • Hi, thanks. I have tried both versions. With `(date +"%Y-%m-%d")` and with ``date +"\%Y-\%m-\%d"`` give the same error. :( – Pikk Oct 12 '13 at 22:16
  • Maybe because I have the % sign in the middle of the password? – Pikk Oct 12 '13 at 22:19
  • It works! I had to \ escape also the % in the password. I thought the backticks will do it. But they don't. Thank you. – Pikk Oct 12 '13 at 22:36