Send Mail If do s.th Failed
This script is going to send an email if s.th go wrong. But in this example of script going to show the rsync the data from one place to other place, It will send mail if this rsync error.
=================== sendmail-if-do-s.th-failed.sh =======================
#!/bin/bash
# backup dirs using gnu/tar
tar -xzPf /home/vannak/Desktop/Documents.tar.gz /home/vannak/Desktop/Documents
# send an email if backup failed
if [ $? -ne 0 ]
then
mail -s "GNU/TAR Backup Failed" ken.vannakk@gmail.com <<EOF
GNU/Tar backup failed @ $(date) for $(hostname)
EOF
else
/usr/bin/logger "Backup done"
fi
# clean up
rm $_filename
=================== sendmail-if-do-s.th-failed.sh =======================
#!/bin/bash
#
# This Script is going to RSYNC "/path/of/our/critical/data/" to "/path/of/our/safe/place/"
#
# If any error script will send the email to "ken.vannakk@gmail.com"
# By: Vannak
# -a, --archive archive mode
# -v, --verbose increase verbosity
# -r, --recursive recurse into directories
# -z, --compress compress file data during the transfer
TIMESTAMP=`date +%y-%m-%d-%H%M`
MAIL_TO=ken.vannakk@gmail.com
SOURCES=/path/of/our/critical/data/
DEST=user@servername:/path/of/our/safe/place/
ERROR=/path/of/our/safe/place//error.log
ERROR1=/path/of/our/safe/place//error1.log
LOG=/path/of/our/safe/place/log-status.log
error=0
# Start SYNC
echo "Start Sync at: $TIMESTAMP " >> $LOG
mail -s Start-Sync ken.vannakk@gmail.com < $LOG
rsync -rvz $SOURCES $DEST 2> $ERROR
echo "SERVER IP: 123.345.567.789/24 " > $ERROR1
cat $ERROR >> $ERROR1
if test -s $ERROR;
then
error=1
fi
sleep 2
if [ "$error" = 1 ];
then
# Create email
echo "From: Vannak Ken <ken.vannakk@gmail.com>" >> /var/tmp/mail.txt
echo "Subject: ERROR VPS SYNC" >> /var/tmp/mail.txt
echo " " >> /var/tmp/mail.txt
echo "IF WE HAVE ERROR CHECK 123.345.567.789/24 in /path/of/our/safe/place/log-status.log" >> /var/tmp/mail.txt
echo "#############################################" >> /var/tmp/mail.txt
echo "Kind of error: " >> /var/tmp/mail.txt
echo "------------------------------------------------------------------------" >> /var/tmp/mail.txt
`/bin/cat $ERROR1 >> /var/tmp/mail.txt`
echo "#############################################" >> /var/tmp/mail.txt
echo "CHECK THE SERVER IP 123.345.567.789" >> /var/tmp/mail.txt
# Send Mail
mail -s Sync-Failed ken.vannakk@gmail.com < /var/tmp/mail.txt
echo "ERROR!!!!" >> $LOG
cat $ERROR >> $LOG
# Remove Error Log
rm /var/tmp/mail.txt
rm $ERROR
rm $ERROR1
fi
echo "FINISH BACKUPS at `date +%y-%m-%d-%H%M`" >> $LOG
mail -s Finished-Sync ken.vannakk@gmail.com < $LOG
# ========================= End ========================
No comments:
Post a Comment