0

I have an issue with a script, which works fine except that it only sends email when I run it directly from the shell but when called during crontab it doesn't send the emails.

There are 2 scripts the main script which does a backup and it calls another script which sends the email. the backup script dump the summary of the operation in a file and reads the file again when to pass it to the email sender script to send. Based on how ssmtp works that's the architecture I have come up with.

I needed to change the subject based on the success or failure of the operation. I have been using my gmail thus using the ssmtp library to send the email.

How to make the backup script send the email when called by crontab schedule?

#!/bin/bash

# File system Parameters
#=========================================================================
TIMESTAMP=`date +%F-%H.%M.%S`
TODAYS_DATE=`date +%Y-%m-%d`

BACKUP_PATH="/mnt/backups/mongo"
SCRIPT_PATH="/root/script"
FILE_NAME="mongodb-$TIMESTAMP"

# Email Parameters
#=========================================================================
TEMPLATE_FILE="/tmp/template.txt"
EMAIL_SUBJECT_CONTEXT="Xylo"
EMAIL_RECIPIENT="myeamil"
EMAIL_SENDER="DB man"
#=========================================================================

BACKUP_PATH=$BACKUP_PATH/$TODAYS_DATE


echo; echo "files will be geneated in  $BACKUP_PATH" | tee $TEMPLATE_FILE
#=========================================================================

[ ! -d  $BACKUP_PATH ] && mkdir -p $BACKUP_PATH || : 

if [ -d "$BACKUP_PATH" ]; then
    cd $BACKUP_PATH

    TMP_BACKUP_DIR="$BACKUP_PATH/mongodb-$TIMESTAMP"

    echo; echo "=> $(date +%F-%H.%M.%S) Backing up Mongo Server: $MONGO_HOST:$MONGO_PORT" | tee -a $TEMPLATE_FILE; echo -n ' ' ;


    if [ "$MONGOUSERNAME" != "" -a "$MONGOPASSWORD" != "" ]; then
        $MONGODUMP_PATH --host $MONGO_HOST:$MONGO_PORT -u $MONGOUSERNAME -p $MONGOPASSWORD --out $TMP_BACKUP_DIR >> /dev/null
    else 
        $MONGODUMP_PATH --host $MONGO_HOST:$MONGO_PORT --out $TMP_BACKUP_DIR >> /dev/null
    fi

    echo; echo "=> $(date +%F-%H.%M.%S) Mongo Server: $MONGO_HOST:$MONGO_PORT  backup files generated"  | tee -a $TEMPLATE_FILE; echo -n ' ';

    if [ -d "$TMP_BACKUP_DIR" ]; then

        if [ "$FILE_NAME" == "" ]; then

            FILE_NAME = "mongodb-$TIMESTAMP"
        fi

        echo; echo "=> $(date +%F-%H.%M.%S) dumping file into  $FILE_NAME.tar.gz"  | tee -a $TEMPLATE_FILE; echo -n ' ';

        $TAR_BIN_PATH --remove-files -czf $FILE_NAME.tar.gz $TMP_BACKUP_DIR >> /dev/null

        if [ -f "$FILE_NAME.tar.gz" ]; then

            echo "=> $(date +%F-%H.%M.%S) successfully generated file $FILE_NAME.tar.gz of size: `du -sh $FILE_NAME.tar.gz` " | tee -a $TEMPLATE_FILE; echo;

            echo; echo "=> $(date +%F-%H.%M.%S) Uploading $FILE_NAME.tar.gz to $S3_BUCKET_NAME on s3" | tee -a $TEMPLATE_FILE; echo -n ' ';

                s3cmd put $FILE_NAME.tar.gz s3://$S3_BUCKET_NAME/$S3_BUCKET_PATH/$FILE_NAME.tar.gz

                filename=s3://$S3_BUCKET_NAME/$S3_BUCKET_PATH/$FILE_NAME.tar.gz
        count=$(s3cmd ls ${filename} | awk "\$4 == \"${filename}\" { print \$4 }" | wc -l)
#            if [[ $? -eq 0 ]]; then
         if [ $count -eq 0 ]; then

                echo "!!!!=> File $FILE_NAME.tar.gz could not be added to s3" | tee -a $TEMPLATE_FILE
                /bin/sh "$SCRIPT_PATH/s3_backup_email_sender.sh" "$EMAIL_RECIPIENT" "$EMAIL_SENDER" "$EMAIL_SUBJECT_CONTEXT BACKUP FAILURE [s3 Upload]" "$(cat $TEMPLATE_FILE)"
            else

               echo "=> $(date +%F-%H.%M.%S) File $FILE_NAME.tar.gz has successfully be added to s3" | tee -a $TEMPLATE_FILE
                           /bin/sh "$SCRIPT_PATH/s3_backup_email_sender.sh" "$EMAIL_RECIPIENT" "$EMAIL_SENDER" "$EMAIL_SUBJECT_CONTEXT BACKUP SUCCESSFUL" "$(cat $TEMPLATE_FILE)"

            fi
            if [ -d "$TMP_BACKUP_DIR" ]; then
                    rm -rf "$TMP_BACKUP_DIR"
            fi
         else

            echo "!!!=> Failed to create backup file: $BACKUP_PATH/$FILE_NAME.tar.gz"  | tee -a $TEMPLATE_FILE; echo;
            /bin/sh "$SCRIPT_PATH/s3_backup_email_sender.sh" "$EMAIL_RECIPIENT" "$EMAIL_SENDER" "$EMAIL_SUBJECT_CONTEXT BACKUP FAILURE [Failed to gzip $FILE_NAME.tar.gz]" "$(cat $TEMPLATE_FILE)"
        fi

    else
        echo; echo "!!!=> Failed to backup mongoDB or Nothing to Back up"  | tee -a $TEMPLATE_FILE; echo; 
        /bin/sh "$SCRIPT_PATH/s3_backup_email_sender.sh" "$EMAIL_RECIPIENT"  "$EMAIL_SENDER"  "$EMAIL_SUBJECT_CONTEXT BACKUP FAILURE [Nothing to back up]" " $(cat $TEMPLATE_FILE)"

    fi
 else
     echo "!!!=> Failed to create backup path: $BACKUP_PATH" | tee -a $TEMPLATE_FILE
     /bin/sh "$SCRIPT_PATH/s3_backup_email_sender.sh" "$EMAIL_RECIPIENT" "$EMAIL_SENDER" "$EMAIL_SUBJECT_CONTEXT BACKUP FAILURE [Failed to create backup folder]" "$(cat $TEMPLATE_FILE)"
 fi

this is the sending script

#!/bin/bash
Now=$(date +"%Y-%m-%d")
SSMTP="$(which ssmtp)"


TEMPLATE="/tmp/s3_backup_mail.txt"

echo "To: $1" > $TEMPLATE
echo "From: $2" >> $TEMPLATE
echo "Subject: $3" >> $TEMPLATE
echo " " >> $TEMPLATE
echo  "$4" >> $TEMPLATE

$SSMTP $1 < $TEMPLATE
#rm $TEMPLATE
1

A couple of gotchas when running something from crontab:

  • One's path in crontab is more limited than when at a console. This can be fixed by making sure the scripts have a line like PATH=$PATH:/usr/bin:/usr/local/bin near the beginning of the script
  • Scripts with "bashism"s in them have a hard time running from cron. The easiest way to fix this is to make sure the very first line of the scripts is the line #!/bin/bash by itself. More explanation and discussion of bashisms: http://mywiki.wooledge.org/Bashism
  • Hello Thanks or the insight. so basically my fault was not having the #!/bin/bash for the email sending script if I understood well. – black sensei Feb 2 '14 at 11:22
  • Hello I have tried couple of things for example adding #!/bin/bash to the email sending script. I have also used the source command to call the email script(either source "$SCRIPT_PATH/s3_backup_email_sender.sh" or "./$SCRIPT_PATH/s3_backup_email_sender.sh") I have used , I have changed the sh at the beginning to /bin/sh "$SCRIPT_PATH/s3_backup_email_sender.sh" and still no success – black sensei Feb 5 '14 at 9:59
  • #!/bin/bash has to be the very first line. If you want more help, please edit the question and post the entire scripts here. – samiam Feb 5 '14 at 17:12
  • I have pasted it in the main question. thank you and both scripts have #!/bin/bash – black sensei Feb 8 '14 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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