root crontab not executing

4

I'm setting up my Debian server to backup my databases with crontab, the mysqldump utility and gunzip.

For some reason, my crontab lines appear to fail, especially the crucial one:

15 2 * * * /usr/bin/mysqldump --user=root --password=XXX --all-databases | /bin/gzip > /backup/database_`date '+%d-%m-%Y'`.sql.gz

I've read several sujects about the possible origins of this behavior, but still I fail to see the reason why this crontab job still fails to create the file after I:

  • Used the root privileges: I use sudo crontab -e to edit the root crontab.
  • Used a whereis to find the complete paths of the commands I'm using, replacing for instance mysqldump with /usr/bin/mysqldump.
  • Checked that the whole line works under root: it does create an archive with today's date, filled with the mysqldump result (showing a warning because I'm using a password in the CLI, but I don't think that'd cause any problem with crontab, right?)

I suppose something is wrong in the way I configure this line in the crontab, but I cannot see it.

Apparently, the crontab is working properly because when I append the line * * * * * env > /backup/env.txt I do get a file that contains the env content in the /backup folder...

Would someone have a clue about this?

Thank you !

~Stéphane

Stephane.P

Posted 2016-06-15T08:34:24.447

Reputation: 63

Answers

2

The default path for cron is:

PATH=/usr/bin:/usr/sbin:.

The date utility is located in /bin/ so you would need to either:

  1. explicitly add this directory to the PATH for cron

    PATH=/bin/:/usr/bin:/usr/sbin:.
    15 2 * * * mysqldump --user=root --password=XXX --all-databases | gzip > /backup/database_$(date '+%d-%m-%Y').sql.gz
    

or

  1. provide the full path for the date command:

    15 2 * * * /usr/bin/mysqldump --user=root --password=XXX --all-databases | /bin/gzip > /backup/database_$(/bin/date '+%d-%m-%Y').sql.gz
    

I prefer the first option as the second method makes it too easy to make a mistake and forget to provide the full path for all commands (such as date in your question).

Anthony Geoghegan

Posted 2016-06-15T08:34:24.447

Reputation: 3 095

2

OK, I found what was not working for me:

By tail'ing the /var/log/syslog, I discovered that crontab has a line size limit! So, when reading the line, it was stopping there: ... $(date +' making the line "bug" at execution.

My solution was to move the job to a /root/backup.sh script and to edit the crontab using:

15 2 * * * /root/backup.sh

At least, now I can backup my data!!

I hope this solution will help others ;)

Stephane.P

Posted 2016-06-15T08:34:24.447

Reputation: 63