2

this is my cron entry:

*/5 * * * * /home/user/scripts/backupDB.sh && echo "Backup Successful: $(date)" >> /home/user/tmp/mybackup.log

This is the script that is called:

#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
date_now=`date +%Y_%m_%d_%H_%M`
dir_name="BU/db_backup"
tar_name="db_backup_${date_now}.tar.gz"
file_name="${dir_name}/${tar_name}"

if [ -z "$dir_name" ]; then
    mkdir $dir_name
fi

log() {
    echo $1
}

do_cleanup(){
#    rm -rf db_backup* 
    log 'cleaning up....'
}

do_backup(){
    log 'snapshotting the db and creating archive' && \
    mongodump -o ${HOME}/${dir_name} && tar -cjf ${HOME}/${tar_name} ${HOME}/${dir_name} || \
    log 'data backd up and created snapshot'

    log 'saving the backup archive in amazon S3' && \
    s3cmd put --acl-private ${HOME}/${tar_name} s3://bucket-name/db-backups/${tar_name} && \
    log 'data backup saved in amazon s3'
}


do_backup && do_cleanup

The thing is, this script works from the command line, but not from crontab. I took a look at Reasons why crontab does not work, and I have done everything that is mentioned there.

Is there anyway of running it via cron?

EDIT: I am currently running as su -c user Does that pose a problem?

theTuxRacer
  • 16,185
  • Did you put that && echo part in after it didn't work? I wouldn't put that in just in case. Did you put this in your system cron or your user cron? Can you explain how you're running it from the command line? is the script executable? – Martin Owens -doctormo- Feb 02 '11 at 17:19
  • Ill try removing the && echo from the crontab and try again. Its in the user cron. TO run it from the command line I type ./scripts/backupDB.sh. Also works with bash scripts/backupDB.sh Yes, the script is executable, and has -rwxr-xr-x permissions. It is owned by the user, and belongs to the same group as the user. I also tried running it as root, and it still didnt work. – theTuxRacer Feb 02 '11 at 17:24
  • If you want to echo something to verify the script is running, you could put the echo in the shell script itself to avoid issues on the command line. – pjmorse Feb 02 '11 at 19:56
  • if [ -z "$dir_name" ]; then mkdir $dir_name; fi This means: If the dir_name variable is empty or unset, run mkdir with no arguments. That's obviously not what you want. Just do: mkdir -p "$dir_name" || exit instead. How are you determining that the script does not run exactly? – geirha Feb 03 '11 at 01:55
  • @kaustubhp -- Any luck? I've tried to reproduce the problem (without the && trailer), but it work for me. Is this in your user crontab? (That's how I ran it, and the script was in my homedir.) Unless you're doing something special with the output, check /var/spool/mail . When I view the mail spool headers for my user, there are also helpful headers from cron: X-Cron-Env: <SHELL=/bin/sh> X-Cron-Env: <HOME=/home/user> X-Cron-Env: <PATH=/usr/bin:/bin> X-Cron-Env: <LOGNAME=user> – belacqua Feb 04 '11 at 18:06
  • 1
    @jgbelacqua I ran the script on the slave running CentOS, it worked :) The only conceivable diff is that I ran as root and tried as a regular user on Debian, and as a regular user on CentOS. – theTuxRacer Feb 07 '11 at 14:05

2 Answers2

1

Try changing your cron command to

bash -c '/home/user/scripts/backupDB.sh && echo "Backup Successful: $(date)" >> /home/user/tmp/mybackup.log'

I have found wrapping things in bash has helped setup environment values in the past. You might even be able to dump parts of your script as a result.

Oli
  • 293,335
-2

I think, there is a problem with script permissions. Have you tried giving executable permission to your sh file? I was facing same issue. When I run through command prompt using sh command, it ran successfully. But, in crontab, it was not running. Then, I have provided 777 permission to script and it worked through crontab. It seems the problem is with Linux.