由于开始接触redis的自动化运维,为了方便后续工作的进行,写了一个redis自动化安装脚本,可以实现安装、配置、并可以使用service redis start/stop控制服务的启动和停止,参考博文http://blog.csdn.net/ludonqin/article/details/47211109

此脚本已经在Centos6.3环境下测试无误,脚本内容如下:


#redis自动安装脚本:

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

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

#日志文件:/var/redis/log

#数据文件:/var/redis/data


#!/bin/bash

Soft_dir="/home/redis"

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

#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


                                                                                欢迎批评指正!