1. #!/bin/bash
    2. # 定义函数Usage,输出脚本使用方法
    3. usage(){
    4. echo "Usage: redisServer -c command [-e]"
    5. echo ""
    6. echo "Optional Command:"
    7. echo " start:start redis service "
    8. echo " example: -c start [-e];[-e] is cluster-enabled"
    9. echo " stop:stop redis service "
    10. echo " example: -c stop "
    11. exit -1
    12. }
    13. # 安装redis
    14. install(){
    15. # 指定安装目录
    16. mkdir -p /usr/local/redis
    17. cd /usr/local/redis
    18. # 安装expect和make
    19. yum -y install expect
    20. yum -y install make
    21. # 从30机器上拉取安装包
    22. if [[ ! -f "/redis-5.0.3.tar.gz" ]]
    23. then
    24. expect<<-END
    25. set timeout 600
    26. spawn scp root@192.100.3.30:/openPlatform/redis-5.0.3.tar.gz /
    27. expect {
    28. "(yes/no)?" { send "yes\r"; exp_continue }
    29. "*assword:" { send "root123\r" }
    30. }
    31. expect
    32. END
    33. fi
    34. # 解压编译
    35. tar -xf /redis-5.0.3.tar.gz
    36. cd redis-5.0.3/src
    37. make
    38. make MALLOC=libc
    39. # 创建日志和数据保存目录
    40. mkdir /usr/local/redis/data
    41. cp /usr/local/redis/redis-5.0.3/redis.conf /usr/local/redis/redis-5.0.3/src/redis.conf
    42. mkdir /usr/local/redis/log
    43. # 修改redis配置文件
    44. sed -i 's/logfile ""/logfile \/usr\/local\/redis\/log\/redis.log/g' redis.conf
    45. sed -i 's/dir .\//dir \/usr\/local\/redis\/data/g' redis.conf
    46. sed -i 's/daemonize no/daemonize yes/g' redis.conf
    47. sed -i 's/bind 127.0.0.1/bind 0.0.0.0/g' redis.conf
    48. sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 15000/g' redis.conf
    49. sed -i 's/# cluster-replica-validity-factor 10/cluster-replica-validity-factor 10/g' redis.conf
    50. sed -i 's/# cluster-migration-barrier 1/cluster-migration-barrier 1/g' redis.conf
    51. # 开启防火墙,并开放6379和16379端口
    52. systemctl start firewalld
    53. firewall-cmd --zone=public --add-port=6379/tcp --permanent
    54. firewall-cmd --zone=public --add-port=16379/tcp --permanent
    55. firewall-cmd --reload
    56. # 创建软连接
    57. ln -f /usr/local/redis/redis-5.0.3/src/redis-cli /bin/redis-cli
    58. }
    59. # 启动redis服务,作为集群使用
    60. start(){
    61. filePath="/usr/local/redis/redis-5.0.3/src/redis.conf"
    62. name="/usr/local/redis/redis-5.0.3/src/redis-server"
    63. pid=`ps -ef | grep "$name"| grep -v grep| awk '{print $2}'`
    64. if [[ $pid ]]
    65. then
    66. kill -9 ${pid}
    67. fi
    68. if [[ -f "$filePath" ]]
    69. then
    70. rm -rf /usr/local/redis/data/*
    71. else
    72. install
    73. fi
    74. if [[ $clusterEnabled -eq 1 ]]
    75. then
    76. sed -i 's/# cluster-enabled yes/cluster-enabled yes/g' $filePath
    77. else
    78. sed -i 's/cluster-enabled yes/# cluster-enabled yes/g' $filePath
    79. fi
    80. # 启动redis服务
    81. $name $filePath
    82. }
    83. # 停止redis服务,作为新的节点
    84. stop(){
    85. name="redis-server"
    86. pid=`ps -ef | grep "$name"| grep -v grep| awk '{print $2}'`
    87. if [ $pid ]
    88. then
    89. kill -9 ${pid}
    90. fi
    91. if [[ -f "/usr/local/redis/redis-5.0.3/src/redis.conf" ]]
    92. then
    93. rm -rf /usr/local/redis/data/*
    94. fi
    95. }
    96. # 获取参数列表,选项后面的冒号表示该选项需要参数
    97. while getopts "c:e" arg
    98. do
    99. case $arg in
    100. c)
    101. #参数存在$OPTARG中
    102. command=$OPTARG
    103. ;;
    104. e)
    105. #参数存在$OPTARG中
    106. clusterEnabled=1
    107. ;;
    108. ?)
    109. # 当有不认识的选项的时候arg为?
    110. echo "unkonw argument"
    111. usage
    112. ;;
    113. esac
    114. done
    115. # 选择执行的指令
    116. case $command in
    117. "start")
    118. start
    119. ;;
    120. "stop")
    121. if [[ $clusterEnabled -ne 1 ]]
    122. then
    123. stop
    124. else
    125. echo "stop:extra operand:'-e'"
    126. usage
    127. fi
    128. ;;
    129. "")
    130. usage
    131. ;;
    132. *)
    133. echo "Error:No such command"
    134. usage
    135. ;;
    136. esac