Database Replication check script not running
- by Tarun
I'm trying to create a Database Replication checking script but I'm getting error while executing it.
Here is the script
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
#Server Name
Server="Test Server"
#My Sql Username and Password
User=root
Password="a"
#Maximum Slave Time Delay
Delay="60"
#File Path to store error and email the same
Log_File=/tmp/replicationcheck.txt
#Email Settings
Subject="$Server Replication Error"
Sender_Name=TestServer
Recipients="[email protected]"
#Mail Alert Function
mailalert(){
sendmail -F $Sender_Name -it <<END_MESSAGE
To: $Recipients
Subject: $Subject
$Message_Replication_Error
`/bin/cat $Log_File`
END_MESSAGE
}
#Show Slave Status
Show_Slave_Status=`echo "show slave status \G;" | mysql -u $User -p$Password 2>&1`
#Getting list of queries in mysql
$Show_Slave_Status | grep "Last_" > $Log_File
#Check if slave running
$Show_Slave_Status | grep "Slave_IO_Running: No"
if [ "$?" -eq "0" ]; then
Message_Replication_Error="$Server Replication error please check. The Slave_IO_Running state is No."
mailalert
exit 1
else
$Show_Slave_Status | grep "Slave_IO_Running: Connecting"
if [ "$?" -eq "0" ]; then
Message_Replication_Error="$Server Replication error please check. The Slave_IO_Running state is Connecting."
mailalert
exit 1
fi
fi
#Check if replication delayed
Seconds_Behind_Master=$Show_Slave_Status | grep "Seconds_Behind_Master" | awk -F": " {' print $2 '}
if [ "$Seconds_Behind_Master" -ge "$Delay" ]; then
Message_Replication_Error="Replication Delayed by $Seconds_Behind_Master."
mailalert
else
if [ "$Seconds_Behind_Master" = "NULL" ]; then
Message_Replication_Error="$Server Replication error please check. The Seconds_Behind_Master state is NULL."
mailalert
fi
fi