创建Redis集群 shell脚本 (之后再用ruby脚本初始化)

ip=10.x.xx.xxx
port=7799

redisPath=/home/weilingyan
confDir=/home/weilingyan/redis-4.0.6

mkRedisClusterDir () {
    [ -d $redisPath ] && rm -rf $redisPath
    mkdir $redisPath
    echo -e "请选择创建redis节点数量:"
    while [ 1 = 1 ]
    do
        read NO
        if  [[ -n "$NO" && "$NO" -gt 0 ]] ;then
            cd $redisPath
            while(( ${NO} >= 1))
            do
                mkdir redis$NO
                NO=$(($NO-1))
            done
            break
        else
            echo -e "输入有误,请输入正确的数字:"
        fi
    done
}
createRedisNode () {
    #redis目录名称
    [ ! -f $confDir/redis.conf  ] && echo "redis.conf不存在" && exit 1
    [ ! -f $confDir/src/redis-trib.rb  ] && echo "redis-trib.rb不存在" && exit 1
    mkRedisClusterDir
    for page in `ls $redisPath`
    do
        if [ -d $page ]
        then
            cp -rf $confDir/redis.conf $page
            sed -i "s/^bind 127.0.0.1/bind $ip/" $page/redis.conf
            sed -i "s/^daemonize no/daemonize yes/" $page/redis.conf
            sed -i "s/^# cluster-enabled yes/cluster-enabled yes/" $page/redis.conf
            sed -i "s/^port 6379/port $port/" $page/redis.conf
            sed -i "s/^save 900 1/# save 900 1/" $page/redis.conf
            sed -i "s/^save 300 10/# save 300 10/" $page/redis.conf
            sed -i "s/^save 60 10000/#save 60 10000/" $page/redis.conf
            echo $ip:$port >> $redisPath/nodes.info
            port=$(($port+1))
        fi
    done
    echo "redis.conf文件修改完成,启动redis节点"
    startNodes
    [ $? -eq 0 ] && echo "redis节点启动成功!"
}

startNodes () {
    for page1 in `ls .`
    do
        if [ -d $redisPath/$page1 ]
        then
            cd $redisPath/$page1
            redis-server redis.conf
        fi
    done
}
createRedisNode

你可能感兴趣的:(创建Redis集群 shell脚本 (之后再用ruby脚本初始化))