封尘网

让学习成为一种习惯!

脚本自动编译安装redis

Redis的安装比较简单,但是运维的目标是自动化,快速。通过编写脚本实现一键自动安装redis。

脚本内容:

#!/bin/bash
#====================================================
# Author: Swper    EMail:hz328.com
# Create Date: 2016-03-21
# Description:redis单机单实例一键安装脚本
#====================================================
# Filename: auto_install_redis.sh

# 安装时需联网

# 步骤
# 1.检查源码包是否存在,不存在就连网下载并安装redis
# 2.安装并配置redis
# 3.添加redis启动停止脚本
# 4.启动redis


#定义存放软件目录
soft="/root/soft"

#如果软件目录不存在则新建该目录
if [[ ! -e $soft ]]; then
    mkdir $soft
fi

#定义判断是否安装成功函数
function installIsOK(){
    if [[ $2 == 0 ]]; then
        echo "$1 install ......  OK !"
    else
        echo "$1 install ......  Failure!"
        exit 1
    fi
}

#进入软件目录
cd $soft


# 下载并安装redis [判断是否存在]
redis='redis-3.0.7'
redis_dir='/data/redis'

if [[ ! -f ${redis}.tar.gz ]]; then
    curl -LO http://download.redis.io/releases/${redis}.tar.gz
fi

tar zxf ${redis}.tar.gz
cd $redis
make PREFIX=${redis_dir} install
if [[ $? == 0 ]]; then
    installIsOK ${redis} 0
else
    installIsOK ${redis} 3
fi

# 配置redis
mkdir -p ${redis_dir}/{etc,run,log}
mkdir -p ${redis_dir}/data/6379
cp redis.conf ${redis_dir}/redis.conf
#cp ${redis_dir}/redis.conf ${redis_dir}/etc/redis_6379.conf

#生成配置文件
redis_6379="${redis_dir}/etc/redis_6379.conf"
cat >> ${redis_6379} << "EOF"
daemonize yes
pidfile /data/redis/run/redis_6379.pid
port 6379
#bind 127.0.0.1
timeout 300
loglevel notice
logfile /data/redis/log/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error no
rdbcompression yes
rdbchecksum no
dbfilename dump.rdb
dir /data/redis/data/6379
#slave-serve-stale-data yes
maxmemory 256mb
maxmemory-policy volatile-lru
maxmemory-samples 3
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 1024
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
EOF

# redis启动停止脚本

redis_start="/etc/init.d/redis"
cat >> ${redis_start} <<- "END"
#!/bin/bash
export PATH="/data/redis/bin:$PATH"
EXEC="/data/redis/bin/redis-server"
CLIEXEC="/data/redis/bin/redis-cli"
PIDFILE="/data/redis/run/redis_6379.pid"
CONF="/data/redis/etc/redis_6379.conf"
PORT="6379"

case "$1" in
    start)
        if [ -f $$PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed."
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running."
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $PORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped."
        fi
        ;;
    restart)
        $0 stop && $0 start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}" >&2
        exit 1
        ;;
esac
END


#增加可执行权限

chmod u+x ${redis_start}


启动redis

${redis_start} start
if [[ $? == 0 ]]; then
    echo "redis start ......  OK"
else
    echo "redis start ...... Failure"
fi

提醒:本文最后更新于 1357 天前,文中所描述的信息可能已发生改变,请谨慎使用。