ideqta e mysql high availability i dynamic dns
ako nqkoi mu e interesno... ima kakvo da se dorazviva
#!/bin/sh
mysql_admin=`which mysqladmin`
mysql_user="checker"
HOSTS[0]="192.168.0.12"
HOSTS[1]="192.168.0.13"
HOSTS[2]="192.168.0.14"
HA_HOST="mysql.home.box"
HA_HOST_UPDATE_KEY="/etc/dnssec/Kmysql.home.box.
+157+40039.key"
# checks target if service: 1-up,0-down
function check_target() {
result=0
target=$1
if [ -n $target ]; then
# ping test method
count=$(ping -W1 -c1 $target | grep 'received' |
awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 1 ]; then
result=1
fi
# mysql test method
# mysqladmin -h${HA_HOST} -u${mysql_user} ping
&>/dev/null
# if [ $? -eq 0 ]; then
# result=1
# fi
fi
echo $result
}
# update dynamic dns record: 1-success,0-fail
function update_dns() {
result=0
hostip=$1
if [ -n $hostip ]; then
update_cmd="update delete ${HA_HOST}. A\nupdate add
${HA_HOST}. 300 A $hostip\nsend"
echo -e $update_cmd | nsupdate -k
${HA_HOST_UPDATE_KEY} &>/dev/null
if [ $? == 0 ]; then
result=1
fi
fi
echo $result
}
### Main ###
# get ip address of the ha_host
HA_HOST_IP=$(host $HA_HOST | grep -v "not found" | awk '{
print $4; }')
if [ -z $HA_HOST_IP ]; then
echo "error: cannot resolve hostname $HA_HOST"
# alert about the problem
exit
fi
# check if "alive"
target=$HA_HOST_IP
if [ $(check_target $target) == 0 ]; then
echo "$HA_HOST[$HA_HOST_IP] is down!"
NEW_HA_HOST_IP=0
# check each backup host
for host in ${HOSTS[@]}
do
if [ $host != $HA_HOST_IP ]; then
if [ $(check_target $host) -eq 1 ]; then
NEW_HA_HOST=$host
break
fi
fi
done
# update dns record to new found backup
if [ $NEW_HA_HOST != 0 ]; then
if [ $(update_dns $NEW_HA_HOST) -eq 1 ]; then
echo "update successful for $NEW_HA_HOST"
else
echo "update failed for $NEW_HA_HOST"
fi
else
echo "no new host found"
# alert about the problem
fi
fi
|