Updated Scripts to send emails via Office 365 SMTP

This commit is contained in:
Matthew McKinnon 2017-04-04 23:49:53 +10:00
parent fd02560118
commit b65dab60b6
5 changed files with 27 additions and 75 deletions

View File

@ -5,10 +5,10 @@
# Description: # Description:
# This script will email a logwatch report # This script will email a logwatch report
MAILTO="support@comprofix.com" MAIL="support@comprofix.com"
SMTP=mail.comprofix.com O365_SMTP=$(grep SMTP office365.conf | awk -F'=' '{print $2}')
THISSERVER=$(hostname -f) O365_USER=$(grep USER office365.conf | awk -F'=' '{print $2}')
MAILFROM="$(hostname)@$(dnsdomainname)" O365_PASS=$(grep PASS office365.conf | awk -F'=' '{print $2}')
#Check if removed-but-not-purged #Check if removed-but-not-purged
test -x /usr/share/logwatch/scripts/logwatch.pl || exit 0 test -x /usr/share/logwatch/scripts/logwatch.pl || exit 0
@ -16,4 +16,4 @@ test -x /usr/share/logwatch/scripts/logwatch.pl || exit 0
#execute #execute
/usr/sbin/logwatch > /tmp/logwatch /usr/sbin/logwatch > /tmp/logwatch
sendemail -o tls=no -s $SMTP -t $MAILTO -f "$THISSERVER <$MAILFROM>" -u "[$THISSERVER] Logwatch" -m "$(cat /tmp/logwatch)" -q sendemail -o tls=auto -s "$O365_SMTP" -xu "$O365_USER" -xp "$O365_PASS" -t "$MAIL" -f "$MAIL" -u "[$THISSERVER] Logwatch" -m "$(cat /tmp/logwatch)" -q

View File

@ -64,46 +64,20 @@ check_return() {
} }
send_error_email() { send_error_email() {
sendemail -f "$THISSERVER <$MAILFROM>" -t $MAILTO -u "[$THISSERVER] There was an error whilst running $0" -s $SMTP sendemail -o tls=auto -s "$O365_SMTP" -xu "$O365_USER" -xp "$O365_PASS" -t "$MAIL" -f "$MAIL" -u "[$THISSERVER] There was an error whilst running $0" -m "Hello,
"Hello,
Whilst running the update script ($0) on $THISSERVER there was a problem. Whilst running the update script ($0) on $THISSERVER there was a problem.
[ERROR] "$1" failed to run [ERROR] "$1" failed to run
The server has the following network interfaces configured ${SERVERADDS[@]}.
Please log in via ssh (e.g. ssh root@$(hostname -f)) and check the log file: Please log in via ssh (e.g. ssh root@$(hostname -f)) and check the log file:
vim $LOGFILE vim $LOGFILE
Regards." Regards." -q
} }
# IP Address stuff
declare -a IPADDR
declare -a NICINTERFACE
declare -a SERVERADDS
index=0
for i in $( ifconfig | grep 'inet addr' | awk '{print $2}'| sed 's#addr:##g' );
do
IPADDR[$index]=$i
let "index += 1"
done
index=0
for i in $( ifconfig | grep 'eth' | awk '{print $1}' );
do
SERVERADDS[$index]="$i ${IPADDR[$index]}"
let "index += 1"
done
# End IP Address stuff
startlogging startlogging
apt-get clean > /dev/null apt-get clean > /dev/null
@ -134,8 +108,6 @@ Packages have been downloaded onto $THISSERVER.
$PACKAGES_TO_BE_UPGRADED $PACKAGES_TO_BE_UPGRADED
The server has the following network interfaces configured ${SERVERADDS[@]}.
To update the server log in via ssh (e.g. ssh root@$(hostname -f)) and run the following command: To update the server log in via ssh (e.g. ssh root@$(hostname -f)) and run the following command:
apt-get upgrade apt-get upgrade
@ -144,7 +116,7 @@ See the logfile for more info: vim $LOGFILE
Regards. " >/tmp/servermail.msg Regards. " >/tmp/servermail.msg
sendemail -o tls=auto -s "$O365_SMTP" -xu "$O365_USER" -xp "$O365_PASS" -t "$MAIL" -f "$MAIL" -u "[$THISSERVER] server may need some updates applied" -m "$(cat /tmp/servermail.msg)" sendemail -o tls=auto -s "$O365_SMTP" -xu "$O365_USER" -xp "$O365_PASS" -t "$MAIL" -f "$MAIL" -u "[$THISSERVER] server may need some updates applied" -m "$(cat /tmp/servermail.msg)" -q
echo "$(date) [MESSAGE] Packages need updating email sent to $MAILTO" >> $LOGFILE echo "$(date) [MESSAGE] Packages need updating email sent to $MAILTO" >> $LOGFILE

View File

@ -12,10 +12,15 @@
# Login to mysql with your root user. # Login to mysql with your root user.
# #
# CREATE USER 'dbbackup'@'localhost' IDENTIFIED BY 'PASSWORD'; # CREATE USER 'dbbackup'@'localhost' IDENTIFIED BY 'PASSWORD';
# GRANT LOCK TABLES, SELECT, SHOW VIEW, RELOAD, REPLICATION CLIENT, EVENT, TRIGGER ON *.* TO 'backup'@'localhost'; # GRANT LOCK TABLES, SELECT, SHOW VIEW, RELOAD, REPLICATION CLIENT, EVENT, TRIGGER ON *.* TO 'dbbackup'@'localhost';
MAIL="support@comprofix.com"
O365_SMTP=$(grep SMTP office365.conf | awk -F'=' '{print $2}')
O365_USER=$(grep USER office365.conf | awk -F'=' '{print $2}')
O365_PASS=$(grep PASS office365.conf | awk -F'=' '{print $2}')
SUBJECT="$(hostname -f) Database Backup Completed $BAKDATE" SUBJECT="$(hostname -f) Database Backup Completed $BAKDATE"
MAILTO="support@comprofix.com"
BAKDATE=$(date +%Y%m%d) BAKDATE=$(date +%Y%m%d)
DBUSER='dbbackup' DBUSER='dbbackup'
DBPASS='EWFfP3GZsqr427Yj' DBPASS='EWFfP3GZsqr427Yj'
@ -38,5 +43,8 @@ for db in $databases; do
done done
cat /tmp/dbbackup.msg | mail -r "$MAILTO" -s "$SUBJECT" "$MAILTO" sendemail -o tls=auto -s "$O365_SMTP" -xu "$O365_USER" -xp "$O365_PASS" -t "$MAIL" -f "$MAIL" -u "$SUBJECT" -m "$(cat /tmp/dbbackup.msg)"
#Use Below to use systems postfix or local MTA
#cat /tmp/dbbackup.msg | mail -s "$SUBJECT" "$MAIL"
rm -fr /tmp/dbbackup.msg rm -fr /tmp/dbbackup.msg

View File

@ -5,11 +5,14 @@
# Description: # Description:
# This script will email when diskspace is high. # This script will email when diskspace is high.
MAILTO="support@comprofix.com" MAIL="support@comprofix.com"
SMTP=mail.comprofix.com O365_SMTP=$(grep SMTP office365.conf | awk -F'=' '{print $2}')
O365_USER=$(grep USER office365.conf | awk -F'=' '{print $2}')
O365_PASS=$(grep PASS office365.conf | awk -F'=' '{print $2}')
LOGFILE="/var/log/diskalert.log" LOGFILE="/var/log/diskalert.log"
THISSERVER=$(hostname -f) THISSERVER=$(hostname -f)
MAILFROM="$(hostname)@$(dnsdomainname)"
startlogging() { startlogging() {
echo $DASHES2 >> $LOGFILE echo $DASHES2 >> $LOGFILE
@ -59,7 +62,7 @@ See the logfile for more info: vim $LOGFILE
Regards, " >/tmp/diskalertmail.msg Regards, " >/tmp/diskalertmail.msg
sendemail -o tls=no -s $SMTP -t $MAILTO -f "$THISSERVER <$MAILFROM>" -u "[$THISSERVER] is running out of disk space" -m "$(cat /tmp/diskalertmail.msg)" sendemail -o tls=auto -s "$O365_SMTP" -xu "$O365_USER" -xp "$O365_PASS" -t "$MAIL" -f "$MAIL" -u "[$THISSERVER] is running out of disk space" -m "$(cat /tmp/diskalertmail.msg)" -q
echo "$(date) [MESSAGE] Running out of disk space email sent to $MAILTO" >> $LOGFILE echo "$(date) [MESSAGE] Running out of disk space email sent to $MAILTO" >> $LOGFILE
fi fi

View File

@ -1,31 +0,0 @@
#!/bin/bash
# Script Name: sentora_www-backup.sh
# Author Name: Matt McKinnon
# Date: 7th June 2016
# Description:
# This script will:
# Backup your public_html folder under /var/sentora/hostdata/DOMAIN_NAME/public_html
BACKUPDIR='/BACKUP/www/'
BAKDATE=$(date +%Y%m%d)
HOSTDATA="/var/sentora/hostdata"
MAILTO="support@comprofix.com"
SUBJECT="$(hostname -f) sentora sites backup"
rotate_backups() {
find $BACKUPDIR -type f -mtime +7 -exec rm -frv {} \;
}
rotate_backups
WEBPATH=$(find $HOSTDATA/* -maxdepth 0 -type d | awk -F/ '{print $5}')
for SITES in $WEBPATH; do
tar -zcf $BACKUPDIR/$SITES-$BAKDATE.tar.gz -P $HOSTDATA/$SITES >> /tmp/sentora_backup.msg
echo "$SITES-$BAKDATE.tar.gz - $SITES backup successfull" >> /tmp/sentora_backup.msg
done
cat /tmp/sentora_backup.msg | mail -r "$MAILTO" -s "$SUBJECT" "$MAILTO"
rm -fr /tmp/sentora_backup.msg