Monthly Archives: August 2013

Shell Script to check system load and send e-mail

This script can check the sytem load with cronjob and send email or even SMS if the load is too high. Anyway I preffer using monit. Tested with ubuntu.

#!/bin/bash
#Load Monitor - sends email when the sytem load reach 5.00, and sms when reach 9.
#first {print$10} check for the last 5 min load, replace with 9 to check for 1 min and 11 for 15 min.

LOAD=`uptime | tr ' ' ' ' | awk '{print$10}' | tr '.' ' ' | awk '{print$1}'`

if [ $LOAD -ge "2" ];
then
    echo "High Load: $LOAD" | mail -s "Load Alert" your@email.com
fi

if [ $LOAD -ge "5" ];
then
    echo "High Load: $LOAD" | mail -s "Load Alert" yourphonenumber@sms.mtel.net
fi

Shell backup script

Script to backup the web dorectory and mysql databases. Tested on ubuntu with cronjob every week.

#!/bin/bash
#=======================================================================================================================
# VPS Backup Script by Ivan Denkov
#=======================================================================================================================
DATE=$(date +"%Y-%m-%d")
MYSQLDUMP="$(which mysqldump)"
TAR="$(which tar)"
PASS="MySQLPSWD"
DIR="/home/backups"
DBS=`mysql -u root -h localhost -p$PASS -e"show databases"`
UPTIME=`uptime`
FREEHD=`df -h`

#This will remove files older than 35 days
find $DIR/* -mtime +35 -exec rm -rf {} \;

$TAR -cvf $DIR/Backup-$DATE.tar /var/www
for DATABASE in $DBS
do
if [ $DATABASE != "Database" ]; then
FILENAME=$DATABASE
$MYSQLDUMP -u root -h localhost -p$PASS $DATABASE > $DIR/$FILENAME.sql
$TAR -rvf $DIR/Backup-$DATE.tar $DIR/$FILENAME.sql
fi
done

#Remove dumped .sql in the folder
rm -rf $DIR/*.sql

sleep 40

#send me e-mail when done
echo "Email Body - Back Up completed at date $DATE, or maybe not!? Let see the statistic \n\n $UPTIME \n\n  $FREEHD" | mail -s "Email Subject - Back UP $DATE" your@email.com