redis自動化安裝腳本

    因爲開始接觸redis的自動化運維,爲了方便後續工做的進行,寫了一個redis自動化安裝腳本,能夠實現安裝、配置、並可使用service redis start/stop控制服務的啓動和中止,參考博文http://blog.csdn.net/ludonqin/article/details/47211109redis

此腳本已經在Centos6.3環境下測試無誤,腳本內容以下:bash


#redis自動安裝腳本:運維

#pid文件:/var/redis/run/redis.pidide

#配置文件:/etc/redis/redis.conf函數

#日誌文件:/var/redis/log測試

#數據文件:/var/redis/dataspa


#!/bin/bash.net

Soft_dir="/home/redis"日誌

Download_URL="http://download.redis.io/redis-stable.tar.gz"blog

#3.2.9

Redis_Version="stable"

#此函數用於判斷是否爲root用戶

root_or_not(){

if [ `id -u` != 0 ] ; then

    echo -e "\e[1;44m  <====You are not root,Please login in root!====> \e[0m"

    exit 1

fi

}

#此函數用來使用戶選擇是否安裝redis

install_or_not(){

    read -p "Install redis, Input Y/y ; Do not install  redis,Input N/n:" INSTALL_OR_NOT

    case $INSTALL_OR_NOT in

    Y|y)

        install_redis

    ;;

    N|n)

        echo -e "\e[0;44m <====Stop  install  Redis====> \e[0m"

        exit 1

    ;;

    *)

        echo -e "\e[1;44m  Only Input  Y/y or N/n  \e[0m"

        install_or_not

    ;;

    esac

}

#此函數用來檢查上一個命令的執行狀態結果返回值,若執行未成功,則退出

check_result(){ 

    if [ $1 != 0 ];then

        echo -e "\e[1,44m  <==== Error,Exit install redis ====> \e[0m"

        exit 1

    fi

}

#此函數用於實現redis的安裝、配置、啓動中止

install_redis(){

[ ! -d ${Soft_dir} ] && mkdir ${Soft_dir}

cd ${Soft_dir}

check_result  $?

yum -y install  wget

check_result  $?

wget  $Download_URL

check_result  $?

tar zxvf redis-${Redis_Version}.tar.gz

check_result  $?

cd  redis-${Redis_Version}

check_result  $?

yum -y install gcc;yum -y install tcl

check_result  $?

make

check_result  $?

make  install

check_result  $?

mkdir /etc/redis

check_result  $?

mkdir  -p /var/redis/{data,log,run}

check_result  $?

cp redis.conf /etc/redis/

check_result  $?

echo -e "\e[1;44m ======Begin to Configure /etc/redis/redis.conf ======= \e[0m"

sed -i '150c pidfile /var/redis/run/redis.pid' /etc/redis/redis.conf

check_result  $?

sed -i '247c dir /var/redis/data' /etc/redis/redis.conf

check_result  $?

sed -i '163c logfile /var/redis/log/redis.log' /etc/redis/redis.conf

check_result  $?

sed -i '128c daemonize yes' /etc/redis/redis.conf

check_result  $?

sed -i '61c bind 0.0.0.0' /etc/redis/redis.conf

check_result  $?

echo -e "\e[1;44m ======Begin To Let Service Start While The Machine Online  ======= \e[0m"

cp /home/redis/redis-stable/utils/redis_init_script /etc/init.d/redis

check_result  $?

sed -i '10c PIDFILE=/var/redis/run/redis.pid' /etc/init.d/redis

check_result  $?

sed -i '11c CONF="/etc/redis/redis.conf"' /etc/init.d/redis

check_result  $?

chmod +x /etc/init.d/redis 

check_result  $?

service redis start

check_result  $?

echo -e "\e[1;44m ======  Success!!!!!!  ======= \e[0m"

}

root_or_not

install_or_not


                                                                                歡迎批評指正!

相關文章
相關標籤/搜索