1

I have the below crontab configuration ;

45 9 * * * sh /home/renko/mysql.backup.sh > /home/renko/mysql_back_log.log

And my script is as below ;

#!/bin/bash
### Backup Folder dir ###
BAK="/var/www/backup/mysql/"
GZIP="$(which gzip)"
### FTP SERVER Login Stuff###
FTPU="someuser1"
FTPP="somepass1"
FTPS="someftpsite1"
FTPUU="someuser2"
FTPPP="somepass2"
FTPSS="someftpsite2"
NOW=$(date +"%d-%m-%Y")


[ ! -d $BAK ] && mkdir -p $BAK || /bin/rm -f $BAK/*

mysqldump -u root -h localhost --all-databases | gzip -9 > /var/www/backup/renko_backup_$NOW.sql.gz

lftp -u $FTPU,$FTPP -e "mput /var/www/backup/renko_backup_$NOW.sql.gz; quit" $FTPS >  /home/renko/mysql_back_log.log
lftp -u $FTPUU,$FTPPP -e "cd some_dir;mput /var/www/backup/renko_backup_$NOW.sql.gz; quit" $FTPSS >  /home/renko/mysql_back_log.log
mutt -s "database backup" [email protected]  -a /var/www/backup/renko_backup_$NOW.sql.gz >  /home/renko/mysql_back_log.log
mutt -s "Sunucu yedekleme" [email protected] -a /var/www/backup/renko_backup_$NOW.sql.gz >  /home/renko/mysql_back_log.log

If I use the mysql.backup.sh command as below;

sudo sh /home/renko/mysql.backup.sh

the script works perfectly... However for some reason crontab doesn't make this script work. I can't find the log for crontab errors. Oh and before you ask I added the job in crontab via ;

sudo crontab -e

and restarted cron service via;

sudo service cron restart

I can't seem to find the problem. I am using Ubuntu Server with kernel 2.6.38-8. Any advice and pointing to any source would be welcomed.

3
  • how about 45 9 * * * /bin/sh /home/renko/mysql.backup.sh > /home/renko/mysql_back_log.log ?
    – Tommy
    Oct 18, 2011 at 7:14
  • you can try this * */1 * * * sh /home/renko/mysql.backup.sh > /home/renko/mysql_back_log.log 2>&1 check the log after 2 hours to whether it runs (assume your backup need 2 hrs)
    – Tommy
    Oct 18, 2011 at 7:19
  • @Tommy I'm sorry to say both did not work :(
    – JohnRoach
    Oct 18, 2011 at 7:45

2 Answers 2

2

A couple of things to check. You don't need the sh as commands in a crontab are passed to a shell anyway. Your particular problem can probably be fixed using the -c flag e.g.

45 9 * * * sh -c "/home/renko/mysql.backup.sh > /home/renko/mysql_back_log.log"

This tells sh to read the commands from the command string rather than stdin.

Normally you would use

45 9 * * * /home/renko/mysql.backup.sh > /home/renko/mysql_back_log.log

Update 1:

I think part of your problem is that you are redirecting the output of the script in your crontab and you are then redirecting the output of various commands to the same file. Remove the output redirection to the log file from within the script.

Update 2: From the comments.

Check that your user has permissions to write to the directory containing the log file. Also check that your user has at least x permission on the directories in the path to your log file.

9
  • I tried what you have told me however it did not work. I have tried both versions :( and no cigar.
    – JohnRoach
    Oct 18, 2011 at 7:38
  • are you sure you reset the server clock to 9:45 ?
    – Tommy
    Oct 18, 2011 at 7:44
  • no what i did is that i changed the cron clock to 10:45 which was a few moments ago. Its now "Tue Oct 18 10:48:39 EEST 2011 " according to the "date" command. Just for extra information : I have set the cron job to "45 10 * * * /home/renko/mysql.backup.sh > /home/renko/mysql_back_log.log"
    – JohnRoach
    Oct 18, 2011 at 7:49
  • Put some echo commands in it to debug it.
    – user9517
    Oct 18, 2011 at 7:57
  • All of your redirections are > they should be >>.
    – user9517
    Oct 18, 2011 at 8:04
1

While we're checking stuff, could we go lowest-common-denominator? Put in crontab

* * * * *  /bin/touch /tmp/nose

If you could first do a /bin/touch /tmp/nose ; ls -al /tmp/nose ; rm /tmp/nose so we can verify /tmp exists and is world-writeable (you'd be surprised), then we can at least say whether cron is the issue, or something peculiar to your script.

Edit: OK, that's progress. Now, can we address the timing of your script? Could you add

45 9 * * *  /bin/touch /tmp/toes

to your crontab? That way, we can confirm that cron thought that things that should go off at that time, like your script, really were supposed to go off.

3
  • yeah it is world readable... So I am expecting my nose to be touched every 1 minute... And the result is : YAY! I can touch my nose... It seems the problem is in my script?? Could you please proof read my script if it won't be much of a bother? Because I can't find what's wrong with it... After all it does work when I call it as "sudo sh /home/renko/mysql.backup.sh"
    – JohnRoach
    Oct 18, 2011 at 7:57
  • See edit above.
    – MadHatter
    Oct 18, 2011 at 9:51
  • Thank you for your answer. I just found the problem. It was permission problem. @Iain has helped me solve it. Thank you again.
    – JohnRoach
    Oct 18, 2011 at 9:55

You must log in to answer this question.

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