tags:

views:

602

answers:

2

I have a crontab set up that errors out every time I attempt to do it. It works fine in the shell. It's the format I'm using when I attempt to automatically insert the date into the filename of the database backup. Does anyone know the syntax I need to use to get cron to let me insert the date into the filename?

mysqldump -hServer -uUser -pPassword Table | gzip > 
/home/directory/backups/table.$(date +"%Y-%m-%d").gz

Thanks in advance!

A: 

The most typical reason for "works in shell but not in cron" is that commands you try to execute are not in PATH.. Reason is that shell invoked from cron aint loading same files as your login shell.

Fix: add absolute path to each command you try to execute.

Second thing i notice in your command. Syntax for running your date command looks like its not very portable. Change that to be in backticks, or run put your whole command to shellscript (also, you can use it to set your path too) and execute that script from cron..

EDIT:

During the writing my original reply my keyboard layout didnt have backticks so check what Pascal wrote.

And example of what you could do with a shellscript:

Copy following to /usr/local/bin/dumptable.sh

#!/bin/sh
/usr/bin/mysqldump --host=HOST --user=USER --password=PASSWORD DATABASE TABLE | /bin/gzip > /tmp/table.`/bin/date +"\%Y-\%m-\%d"`.gz

and then put the the /usr/local/bin/dumptable.sh into cron..

rasjani
+2  A: 

Hi,

What about something like this for the "command" part of the crontab :

mysqldump --host=HOST --user=USER --password=PASSWORD DATABASE TABLE | gzip > /tmp/table.`date +"\%Y-\%m-\%d"`.gz

What has changed from OP is the escaping of the date format :

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

(And I used backticks -- but that should do much of a difference)

(Other solution would be to put your original command in a shell-script, and execute this one from the crontab, instead of the command -- would probably be easier to read/write ^^)

Pascal MARTIN
As a side note : I almost always use long options, so the command is easier to understand/modify by someone who does not often use command line tools
Pascal MARTIN