1. 程式人生 > >shell腳本一鍵安裝redis集群

shell腳本一鍵安裝redis集群

make onf 權限 and nload clu 清理 完成 dev

簡介: 明天再寫,上腳本

#!/bin/bash
#------------------------------------------------------------------------------------------------------------------#
#|          Some people die at the age of 25 and don‘t bury themselves in the earth until they are 75             |#
#------------------------------------------------------------------------------------------------------------------#
#|                      $$$$ $$   $$ $$$$$$ $$    $$   $$$$$$          $$     $$$$$$ $$$$$$                       |#
#|                     $$    $$   $$ $$     $$ $$ $$  $$               $$     $$     $$                           |#
#|                    $$     $$$$$$$ $$$$$  $$  $$ $ $$  $$$$$$        $$     $$$$$  $$$$$                        |#
#|                     $$    $$   $$ $$     $$   $ $  $$     $$        $$     $$     $$                           |#
#|                      $$$$ $$   $$ $$$$$$ $$    $$   $$$$$ $$        $$$$$$ $$$$$$ $$$$$$                       |#
#------------------------------------------------------------------------------------------------------------------#
onversion="4.0.3"
offversion=`basename redis-*.tar.gz .tar.gz | awk -F ‘-‘ ‘{print$2}‘`
installdir=$(cd `dirname $0`; pwd)
cluster_name="redis-cluster"
port1="7001"
port2="7002"
confile1="redis-${port1}.conf"
confile2="redis-${port2}.conf"
nodes1="nodes-${port1}.conf"
nodes2="nodes-${port2}.conf"
logfile1="redis-${port1}.logs"
logfile2="redis-${port2}.logs"

function initize(){
    installdir=$(cd `dirname $0`; pwd)
}

function  checkroot(){
if [ $UID -ne  0 ]
  then
    echo "|----------------------------------------------------------------------------------------------------------------|"
	echo "|------------------------------------------[權限不足...請切換至root用戶]-----------------------------------------|"
    echo "|----------------------------------------------------------------------------------------------------------------|"
	exit;
fi
}

function judge(){
    echo
    offfile=`ls | grep redis-*.tar.gz`
    if [[ "$offfile" != "" ]]
    then
        echo "|----------------------------------------------------------------------------------------------------------------|"
        echo "|--------------------------------------------------[發現離線包]--------------------------------------------------|"
        echo "|----------------------------------------------------------------------------------------------------------------|"
        /usr/bin/sleep 3
        offinstall
    else
        echo "|----------------------------------------------------------------------------------------------------------------|"
        echo "|-------------------------------------------------[未發現離線包]-------------------------------------------------|"
        echo "|--------------------------------------------[開始判斷是否連接外網安裝]------------------------------------------|"
        /usr/bin/sleep 3
        network
    fi
}

function offinstall(){
    echo "|----------------------------------------------------------------------------------------------------------------|"
    echo "|--------------------------------------------------[離線包安裝中]------------------------------------------------|"
    echo "|----------------------------------------------------------------------------------------------------------------|"
    tar -zxvf redis-${offversion}.tar.gz >/dev/null 2>&1
    redis="redis-${offversion}"
    cd ${redis}/src && make >/dev/null 2>&1
    if [[ $? -ne 0 ]]; then
        echo "編譯出錯"
    else
        echo "|----------------------------------------------------------------------------------------------------------------|"
        echo "|--------------------------------------------------[編譯完成]----------------------------------------------------|"
        echo "|----------------------------------------------------------------------------------------------------------------|"
        /usr/bin/sleep 3
        intend
    fi
}

function network(){
    httpcode=`curl -I -m 10 -o /dev/null -s -w %{http_code}‘\n‘ http://www.baidu.com`
    net1=$(echo $httpcode | grep "200")
    if [[ "$net1" != "" ]]
    then
        echo "|----------------------------------------------------------------------------------------------------------------|"
        echo "|-----------------------------------------------------[聯網]-----------------------------------------------------|"
        echo "|-------------------------------------------------[準備聯網安裝]-------------------------------------------------|"
        echo "|----------------------------------------------------------------------------------------------------------------|"
        /usr/bin/sleep 3
        online
    else
        echo "|----------------------------------------------------------------------------------------------------------------|"
        echo "|-------------------------------------------[未聯網,無離線安裝包,準備退出]---------------------------------------|"
        echo "|----------------------------------------------------------------------------------------------------------------|"
        /usr/bin/sleep 3
        exit;
    fi
}
function online(){
    wget_v=`which wget`
    wget_vv=$(echo $wget_v | grep wget)
    if [[ "$wget_vv" != "" ]]
    then
        echo "|----------------------------------------------------------------------------------------------------------------|"
        echo "|--------------------------------------`wget -V |head -n 1`---------------------------------------|"
        echo "|----------------------------------------------------------------------------------------------------------------|"
        wget http://download.redis.io/releases/redis-${onversion}.tar.gz
        installon
    else
        echo "|----------------------------------------------------------------------------------------------------------------|"
        echo "|----------------------------------------[檢測到wget沒有安裝, 準備安裝wget]---------------------------------------|"
        echo "|----------------------------------------------------------------------------------------------------------------|"
        yum install wget -y
        echo "|----------------------------------------------------------------------------------------------------------------|"
        echo "|--------------------------------------`wget -V |head -n 1`---------------------------------------|"
        echo "|----------------------------------------------------------------------------------------------------------------|"
        wget http://download.redis.io/releases/redis-${onversion}.tar.gz
        installon
    fi
}

function installon(){
    echo "|----------------------------------------------------------------------------------------------------------------|"
    echo "|--------------------------------------------------[在線包安裝中]------------------------------------------------|"
    echo "|----------------------------------------------------------------------------------------------------------------|"
    tar -zxvf redis-${onversion}.tar.gz >/dev/null 2>&1
    redis="redis-${onversion}"
    cd ${redis}/src && make >/dev/null 2>&1
    if [[ $? -ne 0 ]]; then
        echo "編譯出錯"
    else
        echo "|----------------------------------------------------------------------------------------------------------------|"
        echo "|----------------------------------------------------[編譯完成]--------------------------------------------------|"
        echo "|----------------------------------------------------------------------------------------------------------------|"
        /usr/bin/sleep 3
        intend
    fi
}

function intend(){
    echo "|----------------------------------------------------------------------------------------------------------------|"
    echo "|-------------------------------------------------[開始遷移文件]-------------------------------------------------|"
    echo "|----------------------------------------------------------------------------------------------------------------|"
    mkdir -p ${installdir}/${cluster_name}/{logs,nodes,conf,bin}
    cp redis-cli redis-server ${installdir}/${cluster_name}/bin
    cp redis-trib.rb ${installdir}/${cluster_name}
    cp ../redis.conf ${installdir}/${cluster_name}/conf/${confile1}
    cp ../redis.conf ${installdir}/${cluster_name}/conf/${confile2}
    echo "|----------------------------------------------------------------------------------------------------------------|"
    echo "|-------------------------------------------------[數據遷移完成]-------------------------------------------------|"
    echo "|----------------------------------------------------------------------------------------------------------------|"
    /usr/bin/sleep 2
    echo "|----------------------------------------------------------------------------------------------------------------|"
    echo "|-------------------------------------------------[清理多余文件]-------------------------------------------------|"
    echo "|----------------------------------------------------------------------------------------------------------------|"
    #finish
    /usr/bin/sleep 2
    echo "|----------------------------------------------------------------------------------------------------------------|"
    echo "|-------------------------------------------------[配置快捷啟動]-------------------------------------------------|"
    echo "|----------------------------------------------------------------------------------------------------------------|"
    service
    /usr/bin/sleep 2
    echo "|----------------------------------------------------------------------------------------------------------------|"
    echo "|-------------------------------------------------[修改配置文件]-------------------------------------------------|"
    echo "|----------------------------------------------------------------------------------------------------------------|"
    configfile
    /usr/bin/sleep 3
    echo "|****************************************************************************************************************|"
    echo "|            WW             WW EEEEEEE LL     CCCCC   OOOOOO      MM      MM     EEEEEEE                         |"
    echo "|             WW    WWWW   WW  EE      LL    CC      OO    OO    MMMM    MMMM    EE                              |"
    echo "|              WW  WW WW  WW   EEEEE   LL   CC      OO      OO  MM  MM  MM  MM   EEEEE                           |"
    echo "|               WW W   W WW    EE      LL    CC      OO    OO  MM    M M     MM  EE                              |"
    echo "|                WW     WW     EEEEEEE LLLLLL CCCCC   OOOOOO  MM     MMM      MM EEEEEEE                         |"
    echo "|****************************************************************************************************************|"
}
function finish(){
    echo
    rm -rf ${installdir}/redis-*
}
function service(){
    cd ${installdir}/${cluster_name}
    echo "./bin/redis-server conf/${confile1}" > start.sh
    echo "./bin/redis-server conf/${confile2}" >> start.sh
    chmod +x start.sh
}
function configfile(){
    cd ${installdir}/${cluster_name}
    pathdir=`pwd`
    cd ${installdir}/${cluster_name}/conf
    #後臺
    sed -i ‘s/daemonize no/daemonize yes/‘ ${confile1} && sed -i ‘s/daemonize no/daemonize yes/‘ ${confile2}
    #端口
    sed -i ‘s/port 6379/port 7001/‘ ${confile1} && sed -i ‘s/port 6379/port 7002/‘ ${confile2}
    #日誌輸出文件
    logpath && logpath2
    #開啟集群
    sed -i ‘s/# cluster-enabled yes/cluster-enabled yes/‘ ${confile1} && sed -i ‘s/# cluster-enabled yes/cluster-enabled yes/‘ ${confile2}
    #集群數據文件
    clusterconf && clusterconf2
}
function logpath(){
    cd ${installdir}/${cluster_name}
    pathdir=`pwd`
    cd ${installdir}/${cluster_name}/conf
    logfileold=`cat "${confile1}" | grep logfile | awk -F ‘"‘ ‘{print$2}‘`
    logfilenew=${pathdir}/logs/${logfile1}
    if [ ! $logfileold ];then
        echo "is null"
        echo "start add data"
        sed -i ‘s/logfile ""/logfile "judgement"/‘ ${confile1}
        logfileold=`cat "${confile1}" | grep logfile | awk -F ‘"‘ ‘{print$2}‘`
        if [ ! $logfileold ];then
            echo "is null"
        else
            echo "not null"
            sed -i "s|$logfileold|$logfilenew|g" ${confile1}
        fi
        sed -i "s|$logfileold|$logfilenew|g" ${confile1}
    else
        echo "not null"
        echo "start add data"
        sed -i "s|$logfileold|$logfilenew|g" ${confile1}
    fi
}
function logpath2(){
    cd ${installdir}/${cluster_name}
    pathdir=`pwd`
    cd ${installdir}/${cluster_name}/conf
    logfileold=`cat "${confile2}" | grep logfile | awk -F ‘"‘ ‘{print$2}‘`
    logfilenew=${pathdir}/logs/${logfile2}
    if [ ! $logfileold ];then
        echo "is null"
        echo "start add data"
        sed -i ‘s/logfile ""/logfile "judgement"/‘ ${confile2}
        logfileold=`cat "${confile2}" | grep logfile | awk -F ‘"‘ ‘{print$2}‘`
        if [ ! $logfileold ];then
            echo "is null"
        else
            echo "not null"
            sed -i "s|$logfileold|$logfilenew|g" ${confile2}
        fi
        sed -i "s|$logfileold|$logfilenew|g" ${confile2}
    else
        echo "not null"
        echo "start add data"
        sed -i "s|$logfileold|$logfilenew|g" ${confile2}
    fi
}
function clusterconf(){
    cd ${installdir}/${cluster_name}
    pathdir=`pwd`
    cd ${installdir}/${cluster_name}/conf
    
    clusternumber=`cat -n ${confile1} | grep cluster-config-file | awk ‘{print$1}‘ | head -n 1`
    clusterconfnew="cluster-config-file ${pathdir}/nodes/${nodes1}"
    clusterNM=`cat -n ${confile1} | grep cluster-config-file | grep -v ‘#‘ | wc -l`
    if [ ${clusterNM} -ge 1 ];then
        number=`cat -n ${confile1} | grep cluster-config-file | grep -v ‘#‘ | awk ‘{print$1}‘ | head -n 1`
        sed -i "${number}d" ${confile1}
    else
        sed -i "${clusternumber}i ${clusterconfnew}" ${confile1}
    fi
}
function clusterconf2(){
    cd ${installdir}/${cluster_name}
    pathdir=`pwd`
    cd ${installdir}/${cluster_name}/conf
    
    clusternumber=`cat -n ${confile2} | grep cluster-config-file | awk ‘{print$1}‘ | head -n 1`
    clusterconfnew="cluster-config-file ${pathdir}/nodes/${nodes2}"
    clusterNM=`cat -n ${confile2} | grep cluster-config-file | grep -v ‘#‘ | wc -l`
    if [ ${clusterNM} -ge 1 ];then
        number=`cat -n ${confile2} | grep cluster-config-file | grep -v ‘#‘ | awk ‘{print$1}‘ | head -n 1`
        sed -i "${number}d" ${confile2}
    else
        sed -i "${clusternumber}i ${clusterconfnew}" ${confile2}
    fi
}
function main(){
checkroot
judge
}
main

  

shell腳本一鍵安裝redis集群