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

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
  • 299,380
-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.