shell脚本一键安装redis集群

简介: 明天再写,上脚本

#!/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

  

原文地址:https://www.cnblogs.com/chenglee/p/10085792.html

时间: 2024-10-24 11:47:22

shell脚本一键安装redis集群的相关文章

shell 脚本实战笔记(6)--集群环境配置检测

1). 背景: 集群部署的时候, 需要一致的配置和环境设置. 对于虚拟机集群, 可以借助镜像拷贝, 复制和还原集群机器. 对与物理机集群而言, 则不一样, 如果机器一多, 多人去操作和配置, 对于成熟精干的团队还好, 对于不熟悉环境的小团队, 由于水平的参差不齐, 往往会导致不一致的环境. 因此无论如何, 写脚本进行自动化的配置和环境校验总是最佳实践. 2). 假设应用场景:*) 系统环境: 安装CDH5, 集群规模为16台机器, 每台机器16CPU, 内存16G, 2块SATA盘共500G,

shell 脚本实战笔记(3)--集群机器的时间同步设置

背景: 有些分布式服务(比如HBase服务), 依赖于系统时间戳, 如果集群各个节点, 系统时间不一致, 导致服务出现诡异的情况. 解决方案: 那如何同步集群各个节点之间的时间? 采用NTP(Network Time Protocol)方式来实现, 选择一台机器, 作为集群的时间同步服务器, 然后分别配置服务端和集群其他机器 1.NTP服务端 *) 安装ntp服务 yum install ntp *) 配置/etc/ntp.conf 这边采用本地机器作为时间的原点 注释server列表 #ser

安装redis 集群

安装redis集群 第一步:安装相关依赖包 1):安装redis-cluster依赖:redis-cluster的依赖库在使用时有兼容问题,在reshard时会遇到各种错误,请按指定版本安装. (1)确保系统安装zlib,否则geminstall会报(no such file to load -- zlib)     #download:zlib-1.2.6.tar       ./configure      make  make install   或者 yum install zlib (

Centos 6.4上面用Shell脚本一键安装mysql 5.6.15

Centos 6.4上面用Shell脚本一键安装mysql 5.6.15  #!/bin/bash if [ `uname -m` == "x86_64" ];then machine=x86_64 else machine=i686 fi mysqlBasedir=/storage/server/mysql mysqlDatadir=${mysqlBasedir}/data/ mysqlLogdir=/storage/log/mysql mysqlUser=mysql mysqlGr

Docker 环境下,一键安装Kafaka 集群

一.必要条件 1.1 安装docker 1.2 安装docker compose 二.一键安装kafka集群 1 version: '3.5' 2 services: 3 zookeeper: 4 image: wurstmeister/zookeeper ## 镜像 5 ports: 6 - "2181:2181" ## 对外暴露的端口号 7 kafka: 8 image: wurstmeister/kafka ## 镜像 9 volumes: 10 - /etc/localtime

Linux安装redis集群

linux安装单机版redis已经在另一篇文章说过了,下边来搞集群,环境是新浪云服务器: redis3.0以后开始支持集群. 前言:redis用什么做集群? 用一个叫redis-trib.rb的ruby脚本.redis-trib.rb是redis官方推出的管理redis集群的工具,集成在redis的源码src目录下(redis-xxx/src/).是基于redis提供的集群命令封装成简单.便捷.实用的操作工具.redis-trib.rb是redis作者用ruby完成的.所以redis集群需要先安

Linux离线安装redis集群

一.应用场景介绍 本文主要是介绍Redis集群在Linux环境下的安装讲解,联网环境安装较为简单,这里只说脱机的Linux环境下是如何安装的.因为大多数时候,公司的生产环境是在内网环境下,无外网,服务器处于脱机状态. 二.安装环境及工具 系统:CentOS7 工具:XShell5及Xftp5 安装包: Ruby-2.4.2 Rubygems-2.6.14 Redis-4.0.2(3.x版本才开始支持集群功能) openssl-1.1.0f 三.安装步骤 要搭建一个最简单的Redis集群,我们至少

Centos7安装Redis(集群模式)

环境:centos7.6 redis4.0模拟在一个Centos7下安装6个redis实例搭建集群 3主3从 安装wgetyum -y install wget 安装gccyum -y install gcc automake autoconf libtool make 下载rediscd /usr/local/softwarewget http://download.redis.io/releases/redis-4.0.0.tar.gz 解压tar -xzvf redis-4.0.0.tar

离线安装redis集群

redis集群需要安装如下组件: ruby rubygems redis ---------------------------------------------------------------------------------- 离线安装ruby 1:上传ruby-2.3.1.tar.gz至服务器: 2:tar -xzvf ruby-2.3.1.tar.gz: 3:cd ruby-2.3.1 4:./configure --prefix='/opt/ruby' 5:make 6:mak