Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with using $(date "+%Y-%m-%d_%H:%M:%S") in a cron job?

I am trying to sumbit a MySQL backup script through cpanel cron jobs panel, but I am receiving errors that I don't understand. I have tried the command on my Ubuntu

mysqldump -u(USERNAME) -p(PASSWORD) --all-databases | gzip > /home/MYHOMEDIR/myDBBckups/full_backup$(date "+%Y-%m-%d_%H:%M:%S").sql.gz

The error I am facing is:

---------- Email message ----------

From: Cron Daemon <root@MYSERVER>

Date: Thu, Jul 11, 2013 at 9:37 PM

Subject: Cron <MYUSERNAME@MYSERVER> mysqldump -u(USERNAME) -p(PASSWORD) --all-databases | 
gzip > /home/MYHOMEDIR/myDBBckups/full_backup$(date "+

To: [email protected]


/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'

/bin/sh: -c: line 1: syntax error: unexpected end of file 

---------- End of Email message ----------

When I remove the date command everything works fine but I don't want to override my backup every time.

With help of Colleagues, i fixed it like this:

mysqldump -u(USERNAME) -p(PASSWORD) --all-databases | gzip > /home/MYHOMEDIR/myDBBckups/full_backup$(date "+\%Y-\%m-\%d_\%H:\%M:\%S").sql.gz

many thanks.

like image 905
Mohsen Avatar asked Mar 23 '23 05:03

Mohsen


2 Answers

The % character in the command portion of a cron job is translated to a newline character. Either use \% to denote a % character, or put the date command into an external script that you invoke from cron.

like image 81
Keith Thompson Avatar answered Mar 25 '23 18:03

Keith Thompson


Cron interprets the % character.

Put the line in a file create_backup.sh and put the command bash /path/to/create_backup.sh in your crontab instead.

like image 40
Otto Allmendinger Avatar answered Mar 25 '23 19:03

Otto Allmendinger