1

I created a cron job to do a daily backup, to store it in the directory /backups

This backups directory is located at the root [/] of the VPS server and is a separate backups partition provided by our VPS host. IE it's stored on a different disk.

So I created a little shell script to run backups and store them there.

#!/bin/sh
mysqldump --all-databases | gzip > /backups/dbbackup-`date +%Y-%m-%d`.sql.gz

This creates a .gz backup with the date attached. It works because when I run it I get what I want.

But I want this in a cron job. I edited crontab and set it to run at 9pm every night.

0 21 * * * /backups/DBBackups.sh

I check the cron log this morning and it ran,

Jun 20 21:00:01 973900 CROND[13279]: (root) CMD (/backups/DBBackups.sh)

But there is no file in the /backups directory.

Where am I going wrong here? Would it spit out an error somewhere that I'm not checking?

I'm still a beginner at this stuff. Later on I'd like to export a specific database by table because a 2gb .sql file is hard to work with should I ever need it. Plus I will probably hit some kind of max file size soon. But leave that for me. If anyone can help get this script running correctly that would be much appreciated.

8
  • Your mysql don't require authentication? Is the script permission executable chmod +x /backups/DBBackups.sh? Jun 21, 2013 at 8:08
  • By default, errors and any output from cron jobs go to the mail of the user they ran as. You can use mail as root or open /var/spool/mail/root with your favourite text editor/mail reader.
    – Ladadadada
    Jun 21, 2013 at 8:15
  • 1
    Any error should be mailed to the opwner of the cron job (root in this case). Since the script is running as root, then it's unlikely to be a permissions issue. First thing I'd try is supplying the full path for mysqldump and gzip in the script
    – symcbean
    Jun 21, 2013 at 8:22
  • 2
    Often a problem with cronjobs: Your PATH is not set - so the script won't find the mysqldump and gzip binary. Try to change your script to use the absolute paths for a test (e.g. mysqldump -> /usr/bin/mysqldump). With which mysqldump you can find out where it is installed (if it's on your regular path).
    – zero0
    Jun 21, 2013 at 8:31
  • 3
    We get so many people asking about cron jobs that we made a canonical Q&A for it - the vast majority are the same small set of issues. If you follow the link provided and work your way through the points in the answer, you will be educated on how to diagnose cron issues and with any luck you'll be able to fix your problem.
    – user9517
    Jun 21, 2013 at 15:18

1 Answer 1

2

The problem is with your DBbackups.sh script. When cron runs it likely has no path info, certainly not the one you are used to, as it doesnt spawn from your login shell, so you need to give absolute paths. DBbackups.sh should therefore be something like:

#!/bin/sh
BACKUPDIR="/backups"
MYSQLDUMP="/usr/local/mysql/bin/mysqldump"
GZIP="/bin/gzip"
DATE=`date +%Y-%m-%d`

${MYSQLDUMP} --all-databases | ${GZIP} > ${BACKUPDIR}/dbbackup-${DATE}.sql.gz

NB: As always, your paths may differ, check your paths to gzip and mysqldump with

which mysqldump
which gzip
7
  • Thanks. This could be part of the answer but it seems I'm still missing something. I edited in this code but I'm getting permission denied when I run the cron job. I set permission as Vilelm said above and it didn't change the output. What other permission could it be? The output doesn't specify. The permissions for DBBackups.sh are -rwxr-xr-x after running Vilelm's command.
    – Xenor
    Jun 21, 2013 at 10:34
  • What permission error? Do you have any logs that we could see. What user has this job been installed under?
    – Sirch
    Jun 21, 2013 at 10:37
  • I installed it under root. When the cron job runs I set the output to a log file. This line appears in the log file: /bin/sh: /backups/DBBackups.sh: Permission denied
    – Xenor
    Jun 21, 2013 at 10:39
  • Freaky. Spawn a new shell /bin/sh and cut and paste the line from cron... does it run?
    – Sirch
    Jun 21, 2013 at 10:44
  • And don't forget to check that the modes on /backups/DBBackups.sh allow execution: chmod 755 /backups/DBBackups.sh is a quick way to be sure.
    – MadHatter
    Jun 21, 2013 at 10:54

Not the answer you're looking for? Browse other questions tagged .