Skip to content

Instantly share code, notes, and snippets.

@matetsu
Created December 28, 2012 03:54
Show Gist options
  • Save matetsu/4394290 to your computer and use it in GitHub Desktop.
Save matetsu/4394290 to your computer and use it in GitHub Desktop.
RedisのMasterホストを指定して、Slaveになるためのスクリプト。 要 redis-slaveof.py
#!/bin/sh
# redis-to-slave.sh
PROG_NAME=$0
HOSTNAME=`hostname -s`
REDIS_PORT=6379
logger -t ${PROG_NAME} "Start changing ${HOSTNAME} to redis slave..."
if [ "$#" != "1" ]; then
echo "FATAL: Invalid argument."
echo "Usage: ${0} redis_master_hostname"
exit 1
fi
MASTER_HOST=$1
logger -t ${PROG_NAME} "Change this host to slave."
# up to master
/usr/local/sbin/redis-slaveof.py ${MASTER_HOST} ${REDIS_PORT}
sed -i -e "s/^slaveof.*/slaveof ${MASTER_HOST} ${REDIS_PORT}/" /etc/redis/redis.conf
# setting for cluster
/sbin/service sentinel status > /dev/null 2>&1
if [ "$?" = "0" ]; then
logger -t ${PROG_NAME} "sentinel service is running. so stop service."
/sbin/service sentinel stop
fi
logger -t ${PROG_NAME} "Changing sentinel.conf file."
sed -e "s/__MASTER_HOST__/${MASTER_HOST}/g" /etc/redis/sentinel.conf.template > /etc/redis/sentinel.conf
logger -t ${PROG_NAME} "Start sentinel service..."
/sbin/service sentinel start
/sbin/service sentinel status > /dev/null 2>&1
if [ "$?" != "0" ]; then
logger -t ${PROG_NAME} "Failed to start sentinel service."
fi
/sbin/chkconfig sentinel on
logger -t ${PROG_NAME} "Finished to change ${HOSTNAME} to redis master."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment