Correct format for mysqldump dynamic name in cron?

I have a crontab that configures these errors every time I try to do this. It works great in a shell. This is the format that I use when I try to automatically insert a date in the database backup file name. Does anyone know the syntax I need to use to get cron so that I can insert a date in the file name?

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

Thanks in advance!

+6
cron mysqldump
source share
2 answers

Something like this for the β€œcommand” of the crontab part:

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

What has changed from the OP is the exit from the date format:

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

(And I used backticks - but that should have made the most of the difference)

(Another solution would be to put your original command in a shell script and execute it from crontab instead of the command - it might be easier to read / write ^^)

+15
source share

The most typical reason for "working in the shell, but not in cron" is that the commands you are trying to execute are not in PATH .. The reason is that the shell called by cron aint loads the same files as your shell entrance.

Fix: add an absolute path to each command that you are trying to execute.

The second thing that I notice on your team. The syntax for running your date command does not seem very portable. Change this to be in backticks, or run the entire command in shellscript (also you can use it to set your path too) and run this script from cron ..

EDIT:

At the time of writing my original answer, my keyboard layout did not have backticks, so check what Pascal wrote.

And an example of what you could do with shellscript:

Copy the following command 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 /usr/local/bin/dumptable.sh in cron ..

0
source share

All Articles