Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Mysql backup scripts not running as root crontab - help

I'm trying to run a crontab as root but the script is not running successfully. I can see in syslog that the script is executing but it does not finish.

Mar 28 04:01:01 ubuntumaster CRON[16812]: (root) CMD home/wayne/scripts/mysql_backup.sh)

Running the script at the command line works great... here's the script:

MyUSER="root" # USERNAME
MyPASS="devan13" # PASSWORD
MyHOST="localhost" # Hostname

# Linux bin paths, change this if it can not be autodetected via which command
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"

# Backup Dest directory, change this if you have someother location
DEST="/home/wayne/backup"

# Main directory where backup will be stored
MBD="$DEST/mysql"

# Get hostname
HOST="$(hostname)"

# Get data in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y")"

# File to store current backup file
FILE=""
# Store list of databases
DBS=""

# DO NOT BACKUP these databases
IGGY="thisdatabaseschema thatdatabase herdata mydata"     
[ ! -d $MBD ] && mkdir -p $MBD || :

# Only root can access it!
$CHOWN 0.0 -R $DEST
$CHMOD 0600 $DEST

# Get all database list first
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"

for db in $DBS
do
skipdb=-1
if [ "$IGGY" != "" ];
then
for i in $IGGY
do
[ "$db" == "$i" ] && skipdb=1 || :
done
fi

if [ "$skipdb" == "-1" ] ; then
FILE="$MBD/$db.$HOST.$NOW.gz"
# do all inone job in pipe,
# connect to mysql using mysqldump for select mysql database
# and pipe it out to gz file in backup dir :)
$MYSQLDUMP --quick -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE
fi
done

Thanks, Wayne

share|improve this question
2  
Could you share also the crontab definition you've done ? –  Benoit Mar 28 at 14:09
2  
I hope your mysql password is not actually 'devan13'. If so, you'll probably want to change it now that you've posted it in a public forum. –  roadmr Mar 28 at 14:26
2  
I suggest adding a shebang (#!/bin/bash) as the first line of your script. –  roadmr Mar 28 at 14:27
    
Whenever you need to debug things like this, you should add echo statements at different points in your script, then in the crontab redirect output to a log file script.sh > /path/to/log 2>&1 that way you can find where it is getting stuck. –  terdon Mar 28 at 14:35
    
Thanks for letting me know about the password :) –  Wayne Gregori Mar 30 at 0:52

5 Answers 5

Unless you've skipped some characters when writting the syslog output in your question, it looks like you've made a typo in your CRON command by forgetting the / in front of the command that should looks like :

/home/wayne/scripts/mysql_backup.sh
share|improve this answer

Ensure to run:

sudo crontab -e
(or crontab -e as root)

And add the desired script:

*/15 * * * * /bin/bash /home/wayne/scripts/mysql_backup.sh

This will initiate the cron every 15 minutes, obviously you may want to change that.

share|improve this answer

My money's on Benoit for having the truly correct answer here, but as soon as you can verify that your script executes you will likely find some additional errors.

In short, make sure you also check your environment! You're half way there by using variables that define the location of the commands (MYSQL="$(which mysql)") but which mysql just reads your PATH to determine its output. which in a script like this doesn't do anything, you might as well just write mysql directly when the command is used, or hardcode the location for mysql in the variable instead of using which to try and populate it for you.

On a test Ubuntu x64 12.04.3 install, * * * * * env > test.out in a crontab shows that the PATH is /usr/bin:/bin. If one of your commands does not exist within that PATH, then it will not execute properly!

As a final suggestion, add set -x to the top of your bash script (assuming it is bash) to debug what the script is actually doing.

share|improve this answer
    
Thanks for the direction... my guess is that it is a path problem. Thank OpenSource community! love you guys/gals –  Wayne Gregori Mar 30 at 0:55

Problem solved. The cron job was running each night but not completing. The problem was with the script. The cron shell did not interpret "==" as equal, i.e. in the cron shell "1==1" was evaluated as false. However, when I ran the program at the command line it worked. I read somewhere that the bash shell understands == or = to mean equal... but the bourne shell does not interpret "==" as equal... I had to replace my "==" with "=" and it worked fine. Thanks for the help. Wayne

share|improve this answer

i use this script and call it a day "easy cronjob handler"

http://codecanyon.net/item/the-easycronjobhandler/6296537

share|improve this answer
1  
This may be a useful tool, but your post does not answer the question. –  Jos Apr 25 at 14:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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