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?
&& 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 withbash 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.if [ -z "$dir_name" ]; then mkdir $dir_name; fi
This means: If thedir_name
variable is empty or unset, runmkdir
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?