I managed to setup a cronjob:

* 13 * * * date=`date +\%d-\%m-\%Y-\%s`; mysqldump -u root -pPassword1 db121 > /home/backup/xbackup_$date.sql; gzip /home/backup/xbackup_$date.sql

I am hoping the above will take a mysqldump everyday at 10pm and then save into the /home/backup directory. However, this has not worked for me.

Is the above entry correct in the crontab? If so, why wouldn't it take a backup as the command works fine when I execute it myself.

Thanks all for any help

Update

I was reading the centos manual for cron jobs and it says:

Users other than root can configure cron tasks by using the crontab utility.

Surley, this isn't true?

link|improve this question

On a side note: Rather then putting a plain password in there put in a mysql conf file, set the proper permissions (only readable by the correct user), and call it with --defaults-file=/path/to/file – Wrikken Mar 31 '11 at 21:46
One step at a time! It sounds like a great idea, but I can barley setup cron jobs! – Abs Mar 31 '11 at 22:25
Regarding your very last question: Users don't gain any special privileges by running something through cron. The command is still run as if the user had executed it, it just runs at a scheduled time/interval. So users other than root can configure tasks, they just get their own separate crontab (by default they live in /var/spool/cron/username, but it is better not to know that and just use crontab -e :)) – Alex Apr 1 '11 at 13:39
feedback

migrated from stackoverflow.com Apr 1 '11 at 13:03

This question came from our site for professional and enthusiast programmers.

3 Answers

up vote 3 down vote accepted

From the crontab(5) man page:

   The "sixth" field (the rest of the line) specifies the  command  to  be
   run.   The  entire  command  portion  of the line, up to a newline or %
   character, will be executed by /bin/sh or by the shell specified in the
   SHELL  variable  of  the  cronfile.   Percent-signs (%) in the command,
   unless escaped with backslash (\), will be changed into newline charac-
   ters,  and  all  data  after the first % will be sent to the command as
   standard input.
link|improve this answer
Thanks for the answer. I escaped the %. However, no backup file was produced still! What else could be wrong. I updated my question with the edited command. I did restart crond, just incase. – Abs Mar 31 '11 at 22:24
@Abs: check syslog and daemon.log, and make sure to set SHELL to a shell that understands the constructs that you use. You assume it's bash, but do you know? – STATUS_ACCESS_DENIED Mar 31 '11 at 22:32
I've checked all logs that I can think of. I don't have a daemon.log, but I do have a cron log though? Not much in there. – Abs Apr 1 '11 at 9:54
feedback

Most likely the commands are not in your $PATH, set in in the crontab or give the full paths to the command, e.g. /usr/bin/mysqldump

link|improve this answer
I have changed this command to use the full path. However, the /usr/bin/ path is in my path already within crontab: PATH=/sbin:/bin:/usr/sbin:/usr/bin. – Abs Apr 1 '11 at 9:44
feedback

I would do this

date='date +\%d-\%m-\%Y-\%s'

0 22 * * * mysqldump -u root -pPassword1 db121 > /home/backup/xbackup_`$date`.sql; gzip /home/backup/xbackup_`$date`.sql
link|improve this answer
I also tried this, but this didn't work as it didn't produce a file unfortunately. I have no idea what is going, it should be working! – Abs Apr 1 '11 at 14:02
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.