Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pegasuskim/75750df87ef2e3cf77949fd46d4e3098 to your computer and use it in GitHub Desktop.
Save pegasuskim/75750df87ef2e3cf77949fd46d4e3098 to your computer and use it in GitHub Desktop.
Shell script to migrate a VIP to the actual redis master when managing them with redis-sentinel
#!/bin/sh
_DEBUG="on"
DEBUGFILE=/tmp/sentinel_failover.log
VIP='10.125.125.30'
MASTERIP=${6}
MASK='24'
IFACE='eth0'
MYIP=$(ip -4 -o addr show dev ${IFACE}| grep -v secondary| awk '{split($4,a,"/");print a[1]}')
DEBUG () {
if [ "$_DEBUG" = "on" ]; then
$@
fi
}
set -e
DEBUG date >> ${DEBUGFILE}
DEBUG echo $@ >> ${DEBUGFILE}
DEBUG echo "Master: ${MASTERIP} My IP: ${MYIP}" >> ${DEBUGFILE}
if [ ${MASTERIP} = ${MYIP} ]; then
if [ $(ip addr show ${IFACE} | grep ${VIP} | wc -l) = 0 ]; then
sudo /sbin/ip addr add ${VIP}/${MASK} dev ${IFACE}
DEBUG echo "sudo /sbin/ip addr add ${VIP}/${MASK} dev ${IFACE}" >> ${DEBUGFILE}
sudo /usr/sbin/arping -q -c 3 -A ${VIP} -I ${INTERFACE}
fi
exit 0
else
if [ $(ip addr show ${IFACE} | grep ${VIP} | wc -l) != 0 ]; then
sudo /sbin/ip addr del ${VIP}/${MASK} dev ${IFACE}
DEBUG echo "sudo /sbin/ip addr del ${VIP}/${MASK} dev ${IFACE}" >> ${DEBUGFILE}
fi
exit 0
fi
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment