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.

voretaq7
  • 79,345
  • 17
  • 128
  • 213
Xenor
  • 27
  • 1
  • 6
  • Your mysql don't require authentication? Is the script permission executable `chmod +x /backups/DBBackups.sh`? – Giorgio Previtera Jun 21 '13 at 08: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 '13 at 08:15
  • Work your way through the link above it will almost certainly help you solve your problem. – user9517 Jun 21 '13 at 08:16
  • Is your script working if you run it manually? – Pascal Schmiel Jun 21 '13 at 08:17
  • 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 '13 at 08: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 '13 at 08: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 '13 at 15:18
  • I see your point but where does it end? Pretty much every question one could ask could be answered via enough Googling. So it becomes a case of better to just stop people asking questions altogether. People ask questions for time saving, they don't want to read a book to answer it otherwise they'd not be on here. The worst part is those duplicate votes put people off answering questions yet I see nothing in that FAQ which solves my problem. So the system tells me to create a new question with the same post which seems silly. Last time I edited my question someone reversed it. – Xenor Jun 27 '13 at 09:43

1 Answers1

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
Sirch
  • 5,697
  • 4
  • 19
  • 36
  • 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 '13 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 '13 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 '13 at 10:39
  • Freaky. Spawn a new shell `/bin/sh` and cut and paste the line from cron... does it run? – Sirch Jun 21 '13 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 '13 at 10:54
  • I did what you said Sirch and got exactly the same line in the log. – Xenor Jun 21 '13 at 12:04
  • I thought I would respond to this again. This was correct, the problem is the path to MySQLDump was different to your path. Once I fixed that my script started working. I still don't see this answer on that other question mine is linked to but as I didn't link it that's for someone else to fix. – Xenor Jul 19 '13 at 07:51