Linux Tutorial | Matt Cooper - Open Source Support
Linux Tutorial | Matt Cooper - Open Source Support
Bash Scripting

Bash script to send public IP address to Email

This simple bash script checks your current public IP and compares it to the previous days public IP (depending on cron job). Email alert is sent if IP has changed.

Credit goes to Ronny Bull – http://ronnybull.com/2011/08/10/bash-dynamic-public-ip-address-monitor-script/

Pre-req – install curl and postfix

apt-get install curl
apt-get install postfix

Bash script:

Continue reading

May 16, 2015by Matt Cooper
FacebookTwitterPinterestGoogle +Stumbleupon
Bash Scripting, MySQL Tutorial

MySQL – remove duplicate rows leaving one copy – bash scripting

Simple bash script to remove duplicate rows from a table:

#!/bin/bash
echo "Remove duplicate records from tbl_duplicates"
mysql -uusername -ppassword database -e "DELETE n1 FROM tbl_duplicates n1, tbl_duplicates n2 WHERE n1.col_id > n2.col_id AND n1.col_duplicate = n2.col_duplicate;"

Terms mentioned:

tbl_duplicates – table to remove duplicates from
col_id – column ID
col_duplicates – a column which contains duplicate data
> – this keeps the row with the lowest id

April 9, 2014by Matt Cooper
FacebookTwitterPinterestGoogle +Stumbleupon
Bash Scripting, MySQL Tutorial

MySQL Master-Slave Replication – Bash Script Monitor

Simple script to verify that MySQL replication is working:

*Script is setup on slave server

#!/bin/bash
# Originally from http://lists.mysql.org/replication/1672
# Checks MySQL Replication status. Sends user(s) a notification of status
status=0
MasterHost="10.10.10.10"
SlaveHost="127.0.0.1"
emails="user@email.com" #multiple emails space separated
DownSubject="Replication status - Down"
GoodSubject="Replication status - Good"
GoodMessage="Everything regarding MySQL replication on $SlaveHost is good.nHave a great day!nn"

#Grab the lines for each and use Gawk to get the last part of the string(Yes/No)
SQLresponse=`mysql -u root --password=password dbname -e "show slave status G" |grep -i "Slave_SQL_Running"|gawk '{print $2}'`
IOresponse=`mysql -u root --password=password dbname -e "show slave status G" |grep -i "Slave_IO_Running"|gawk '{print $2}'`

if [ "$SQLresponse" = "No" ]; then
error="Replication on the slave MySQL server($SlaveHost) has stopped working.nSlave_SQL_Running: Non"
status=1
fi

if [ "$IOresponse" = "No" ]; then
error="Replication on the slave MySQL server($SlaveHost) has stopped working.nSlave_IO_Running: Non"
status=1
fi

# If the replication is not working
if [ $status = 1 ]; then
for address in $emails; do
echo -e $error | mail -s "$DownSubject" $address
echo "Replication down, sent email to $address"
done
fi

# If the replication is working fine
if [ $status = 0 ]; then
for address in $emails; do
echo -e $GoodMessage | mail -s "$GoodSubject" $address
echo "Replication is up, still sent email to $address"
done
fi
March 14, 2014by Matt Cooper
FacebookTwitterPinterestGoogle +Stumbleupon
Bash Scripting

Simple disk space bash script

The below bash script, when executed, will check the % of disk space usuage of the complete filesystem and email a specified email address if more than 80% full.

Pre-req – create fs_report.log inside /var/log/

i.e.

 
touch /var/log/fs_report.log 

Create a new bash script:

 
vim /path/to/script/diskspace.sh 

Replace the email address or modify the disk space below as needed:


#!/bin/bash
#
#Extract % use for the filesystem
#Send email warning to if the raid is 80% or more full
#Include Folder/File sizes of the filesystem directory contents
#
df -h | awk '{ print $5 }' | cut -d'%' -f1 | while read usep; do
if [ $usep -ge 80 ]; then
echo "***$(hostname) is $usep% FULL at $(date)" > /var/log/fs_report.log
echo "***Please remove unnecessary data from $(hostname)" >> /var/log/fs_report.log
echo "***Detailed Folder/File size for BackupServer content:" >> /var/log/fs_report.log
df -h >> /var/log/fs_report.log
mail -s "WARNING-bksvr2 $usep% FULL" backup@mydomain.com < /var/log/fs_report.log -- -f "backup@mydomain.com"
fi
done

*Make sure to make the script executable:


chmod +x diskspace.sh

To automate this script add a cron job to run it on a regular basis:


0 8 * * * /path/to/script/diskspace.sh

The above cron job will run the script at 8am everyday.

July 23, 2013by Matt Cooper
FacebookTwitterPinterestGoogle +Stumbleupon

About me

Hi, I'm Matt Cooper. I started this blog to pretty much act as a brain dump area for things I learn from day to day. You can contact me at: matt@linuxtutorial.co.uk

Recent Comments

  • Andrew on Export list of Amazon EC2 Instances to CSV
  • Matt Cooper on Proxmox – add a second hard drive to node for Backups
  • karis on Proxmox – add a second hard drive to node for Backups
  • Matt Cooper on Remote MySQL Database – Slow Connection
  • Matt Cooper on Bash script to send public IP address to Email

Categories

  • AdvancedTomato
  • Amazon AWS
  • Amazon Linux
  • Amazon S3
  • Apache
  • Apache Kafka
  • Backup Tutorial
  • Bash Scripting
  • Centos Tutorial
  • CloudFlare
  • Command line Tutorial
  • CPanel Troubleshooting
  • CPanel Tutorial
  • Debian Troubleshooting
  • Debian Tutorial
  • DKIM
  • Docker
  • EC2
  • ESXi
  • Faildows
  • Google Adwords
  • Google Analytics
  • Google Chromebook
  • Google Mail
  • graylog
  • IAM
  • imapsync
  • iRedmail Tutorial
  • ISPConfig3 Tutorial
  • Java
  • ldap
  • letsencrypt
  • MyDNS
  • MySQL Troubleshooting
  • MySQL Tutorial
  • Nest Install
  • Netbeans
  • Nginx
  • Nginx Troubleshooting
  • openssl
  • PCI Compliance
  • Percona
  • PHP
  • Plex Media Server Tutorial
  • Postfix
  • Proxmox Tutorial
  • Pure FTPd
  • Resourcespace Tutorial
  • Route 53
  • Rsync Tutorial
  • Security
  • Smoothwall Troubleshooting
  • Smoothwall Tutorial
  • SSH
  • tcpdump
  • Thunderbird
  • Tomcat Troubleshooting
  • Ubuntu Tutorial
  • Uncategorized
  • Unison
  • vmware
  • vzdump
  • WHM
  • Wireshark
  • Wordpress Troubleshooting
  • WordPress Tutorial

“See, you not only have to be a good coder to create a system like Linux, you have to be a sneaky bastard too." Linus Torvalds

© 2017 copyright www.linuxtutorial.co.uk // All rights reserved