Eu tenho um problema com um script, que funciona bem, exceto que ele só envia e-mail quando eu o executo diretamente do shell, mas quando chamado durante o crontab ele não envia os e-mails.
Existem 2 scripts o script principal que faz um backup e chama outro script que envia o email. o script de backup despeja o resumo da operação em um arquivo e lê o arquivo novamente quando passá-lo ao script de remetente do email a ser enviado. Com base em como o ssmtp funciona, essa é a arquitetura que eu criei.
Eu precisava mudar de assunto com base no sucesso ou fracasso da operação. Eu tenho usado o meu Gmail, usando a biblioteca ssmtp para enviar o email.
Como fazer o script de backup enviar o email quando chamado por 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 "\ == \"${filename}\" { print \ }" | 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
este é o script de envio
#!/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