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
  • 80,749
Xenor
  • 27

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,885