直接上shell了。

#!/bin/bash

#------------------------------------------------------------------------------------------------------------------#
#| Some people die at the age of 25 and don't bury themselves in the earth until they are 75 |#
#------------------------------------------------------------------------------------------------------------------#
#| $$$$ $$ $$ $$$$$$ $$ $$ $$$$$$ $$ $$$$$$ $$$$$$ |#
#| $$ $$ $$ $$ $$ $$ $$ $$ $$ $$ $$ |#
#| $$ $$$$$$$ $$$$$ $$ $$ $ $$ $$$$$$ $$ $$$$$ $$$$$ |#
#| $$ $$ $$ $$ $$ $ $ $$ $$ $$ $$ $$ |#
#| $$$$ $$ $$ $$$$$$ $$ $$ $$$$$ $$ $$$$$$ $$$$$$ $$$$$$ |#
#------------------------------------------------------------------------------------------------------------------#
#| redis-cluster install |#
#| 2018.12.10 |#
#| blog http://www.cnblogs.com/chenglee/ |#
#------------------------------------------------------------------------------------------------------------------#
onversion="4.0.3"
offversion=`basename redis-*.tar.gz .tar.gz | awk -F '-' '{print$2}'`
installdir=$(cd `dirname $0`; pwd)
cluster_name="redis-cluster"
number=6
numstaring=700
#------------------------------------------------------------------------------------------------------------------#
#| 说 明 |#
#------------------------------------------------------------------------------------------------------------------#
#| 此脚本分有离线安装和联网安装的功能, 联网安装的用法就是直接运行这个脚本 sh ***.sh /或者./***.sh |#
#| 而离线安装, 就是把安装包和这个脚本放在想安装在的目录, 然后执行sh ***.sh /或者./***.sh即可,而无论选择哪一 |#
#| 方式,都是包是否存在的问题,包在=离线,包不在=联网,下面说一下上面的全局变量的解释。 |#
#| onversion 这个是在联网安装的时候需要制定的版本,默认4.0.3,如果需要安装5.0.0的, 修改此参数。 |#
#| offversion 这个不用管它,这个是离线安装识别离线包版本的。 |#
#| cluster_name 这个就是安装集群的文件夹名字,就是把集群装在这个文件夹里面。 |#
#| number 这个就比较重要了, 节点。一台机器需要安装的节点,我这里是6,安装6个节点,可修改。 |#
#| numstaring 这个是端口的前缀,比如我这里安装6个节点,端口自动分配为7001,7002,7003..., 如果需要修改成 |#
#| 6001,6002...这些端口,修改此属性为600便可。 |#
#------------------------------------------------------------------------------------------------------------------#
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 "|----------------------------------------------------------------------------------------------------------------|"
cd ${installdir}
redis="redis-${onversion}"
cd ${redis}/src
if [ -f "redis-server" ];then
echo "|----------------------------------------------------------------------------------------------------------------|"
echo "|-------------------------------------------------[准备分配数据]-------------------------------------------------|"
echo "|----------------------------------------------------------------------------------------------------------------|"
else
echo "|----------------------------------------------------------------------------------------------------------------|"
echo "|-------------------------------------------------[失败重新编译]-------------------------------------------------|"
echo "|----------------------------------------------------------------------------------------------------------------|"
/usr/bin/sleep 2
make >/dev/null 2>&1
fi
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}
for((i=1;i<=${number};i++));
do
port=${numstaring}${i}
confile="redis-${numstaring}${i}.conf"
cp ../redis.conf ${installdir}/${cluster_name}/conf/${confile}
done 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-*
}
#------------------------------------------------------------------------------------------------------------------#
#| service 复制多份redis.conf conf 目录 |#
#| configfile 对整改的内容做个整合 |#
#| background 后台运行 |#
#| updateport 端口 |#
#| logpath 日志输出目录文件 logs 目录 |#
#| colony 开启集群模式 |#
#| clusterconf 集群数据输出目录文件 nodes目录 |#
#| trib 创建集群 连接所有节点 redis-cluster.log |#
#------------------------------------------------------------------------------------------------------------------#
function service(){
cd ${installdir}/${cluster_name}
for((i=1;i<=${number};i++));
do
port=${numstaring}${i}
confile="redis-${numstaring}${i}.conf"
echo "./bin/redis-server conf/${confile}" >> start.sh
done
chmod +x start.sh
}
function configfile(){
cd ${installdir}/${cluster_name}
pathdir=`pwd`
cd ${installdir}/${cluster_name}/conf
for((i=1;i<=${number};i++));
do
port=${numstaring}${i}
confile="redis-${numstaring}${i}.conf"
#后台
background
#端口
updateport
#日志输出文件
logpath
#开启集群
colony
#集群数据文件
clusterconf
done
}
function background(){
cd ${installdir}/${cluster_name}/conf
for((i=1;i<=${number};i++));
do
confile="redis-${numstaring}${i}.conf"
sed -i 's/daemonize no/daemonize yes/' ${confile}
done
}
function updateport(){
cd ${installdir}/${cluster_name}/conf
for((i=1;i<=${number};i++));
do
port=${numstaring}${i}
confile="redis-${numstaring}${i}.conf"
sed -i "s|port 6379|port ${port}|g" ${confile}
done
}
function colony(){
cd ${installdir}/${cluster_name}/conf
for((i=1;i<=${number};i++));
do
confile="redis-${numstaring}${i}.conf"
sed -i 's/# cluster-enabled yes/cluster-enabled yes/' ${confile}
done
}
function logpath(){
cd ${installdir}/${cluster_name}
pathdir=`pwd`
cd ${installdir}/${cluster_name}/conf
for((i=1;i<=${number};i++));
do
port=${numstaring}${i}
confile="redis-${numstaring}${i}.conf"
logfileold=`cat "${confile}" | grep logfile | awk -F '"' '{print$2}'`
logfile="redis-${numstaring}${i}.logs"
logfilenew=${pathdir}/logs/${logfile}
if [ ! $logfileold ];then
sed -i 's/logfile ""/logfile "judgement"/' ${confile}
logfileold=`cat "${confile}" | grep logfile | awk -F '"' '{print$2}'`
if [ ! $logfileold ];then
echo
else
sed -i "s|$logfileold|$logfilenew|g" ${confile}
fi
sed -i "s|$logfileold|$logfilenew|g" ${confile}
else
sed -i "s|$logfileold|$logfilenew|g" ${confile}
fi
done
}
function clusterconf(){
cd ${installdir}/${cluster_name}
pathdir=`pwd`
cd ${installdir}/${cluster_name}/conf
for((i=1;i<=${number};i++));
do
port=${numstaring}${i}
confile="redis-${numstaring}${i}.conf"
nodes="nodes-${numstaring}${i}.conf"
clusternumber=`cat -n ${confile} | grep cluster-config-file | awk '{print$1}' | head -n 1`
clusterconfnew="cluster-config-file ${pathdir}/nodes/${nodes}"
clusterNM=`cat -n ${confile} | grep cluster-config-file | grep -v '#' | wc -l`
if [ ${clusterNM} -ge 1 ];then
number=`cat -n ${confile} | grep cluster-config-file | grep -v '#' | awk '{print$1}' | head -n 1`
sed -i "${number}d" ${confile}
else
sed -i "${clusternumber}i ${clusterconfnew}" ${confile}
fi
done
}
function trib(){
cd ${installdir}/${cluster_name}
echo "|----------------------------------------------------------------------------------------------------------------|"
echo "|-------------------------------------------------[启动集群进程]-------------------------------------------------|"
echo "|----------------------------------------------------------------------------------------------------------------|"
./start.sh
/usr/bin/sleep 2
echo "|----------------------------------------------------------------------------------------------------------------|"
echo "|-------------------------------------------------[进程 已启动]-------------------------------------------------|"
echo "|----------------------------------------------------------------------------------------------------------------|" bindip=`ps aux | grep "redis" | awk '{print$12}' | grep -v "auto" | head -n ${number}`
echo "|----------------------------------------------------------------------------------------------------------------|"
echo "|-------------------------------------------------[安装 gem环境]-------------------------------------------------|"
echo "|----------------------------------------------------------------------------------------------------------------|"
/usr/bin/sleep 2
wget https://rubygems.org/downloads/redis-3.2.0.gem
gem install redis-*.gem
echo "|----------------------------------------------------------------------------------------------------------------|"
echo "|-------------------------------------------------[gem 环境已装]-------------------------------------------------|"
echo "|----------------------------------------------------------------------------------------------------------------|"
/usr/bin/sleep 2
echo "|----------------------------------------------------------------------------------------------------------------|"
echo "|-------------------------------------------------[搭建节点连接]-------------------------------------------------|"
echo "|----------------------------------------------------------------------------------------------------------------|"
./redis-trib.rb create --replicas 1 ${bindip} > redis-cluster.log > /dev/null 2>&1
}
function main(){
gem=`which gem`
gemv=$(echo $gem | grep gem)
if [[ $gemv != "" ]]
then
echo "|----------------------------------------------------------------------------------------------------------------|"
echo "|-------------------------------------------------[gem 环境存在]-------------------------------------------------|"
echo "|----------------------------------------------------------------------------------------------------------------|"
checkroot
judge
trib
else
echo "|----------------------------------------------------------------------------------------------------------------|"
echo "|-------------------------------------------------[gem 环境未装]-------------------------------------------------|"
echo "|----------------------------------------------------------------------------------------------------------------|"
yum install ruby rubygems -y
wget https://rubygems.org/downloads/redis-3.2.0.gem
gem install redis-*.gem
/usr/bin/sleep 2
echo "|----------------------------------------------------------------------------------------------------------------|"
echo "|-------------------------------------------------[gem 环境已装]-------------------------------------------------|"
echo "|----------------------------------------------------------------------------------------------------------------|"
checkroot
judge
trib
fi
}
main

shell脚本一键安装redis集群[最终版]的更多相关文章

  1. shell脚本一键安装redis集群

    简介: 明天再写,上脚本 #!/bin/bash #-------------------------------------------------------------------------- ...

  2. 编写shell脚本一键启动zookeeper集群!!

    踩了一个多小时坑终于解决了: 这里分享给大家,更主要的目的是记住这些坑,避免以后重复走!!! 首先,这里采用ssh秘钥方式进行集群主机之间免密登录执行启动命令 这里简单说下原理: 通过ssh去另外一台 ...

  3. shell脚本一键安装redis

    支持识别离线包和联网安装,自动修改使用后台运行模式,离线安装的方法是,将离线包和脚本放在同一个文件夹, 它会先识别有没有离线包, 有离线包就先安装离线包, 没有离线包就安装进行判断机器是否能联网, 能 ...

  4. liunx contos 7.4 安装redis集群

    前前后后安装了几次redis集群,基本上每次安装都会采坑,耗时伤神. 安装redis依赖gcc环境,安装前先检查liunx上面有没有安装GCC 命令:gcc -v 上传redis-4.0.1.tar. ...

  5. 从零开始安装Redis 集群(Linux CenOS7)

    从零开始安装Redis 集群(Linux CenOS7) 使用ISO安装CentOS7虚拟机 配置静态IP(参考Mac VMware Fusion CentOS7配置静态IP) 安装vim [root ...

  6. centos7.0 安装redis集群

    生产环境下redis基本上都是用的集群,毕竟单机版随时都可能挂掉,风险太大.这里我就来搭建一个基本的redis集群,功能够用但是还需要完善,当然如果有钱可以去阿里云买云数据库Redis版的,那个还是很 ...

  7. Centos 6.4上面用Shell脚本一键安装vsftpd

    Centos 6.4上面用Shell脚本一键安装vsftpd install.sh #!/bin/bash if [ `uname -m` == "x86_64" ];then m ...

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

    Centos 6.4上面用Shell脚本一键安装mysql 5.6.15  #!/bin/bash if [ `uname -m` == "x86_64" ];then machi ...

  9. 安装完Ubuntu后通过shell脚本一键安装软件

    安装完Ubuntu后通过shell脚本一键安装软件 以下代码中#是单行注释 :<<! ! 是多行注释. 运行的时候需要把多行注释去掉. 比如把以下代码保存为install.sh, 那么在终 ...

随机推荐

  1. 线程中使用SaveFileDialog不能弹出窗体

    在子线程中使用 SaveFileDialog 无法弹出窗体,主要是我们需要用主线程去处理SaveFileDialog , 我们可以将子线程进行如下设置: public partial class Fo ...

  2. 邮件收取客户端LumiSoft类库接收yahoo邮件的问题。

    //开始循环取邮件数据 m_pImap.Fetch( false, IMAP_t_SeqSet.Parse("1:*"), new IMAP_t_Fetch_i[]{ new IM ...

  3. unity3d生命周期

  4. Java中通过Class类获取Class对象的方法详解

    方式1:通过Object类的getObject()方法 Person p = new Person(); Class c = p.getClass(); 方式2: 通过 类名.class 获取到字节码 ...

  5. RNN通俗理解

    让数据间的关联也被 NN 加以分析,我们人类是怎么分析各种事物的关联,?最基本的方式,就是记住之前发生的事情. 那我们让神经网络也具备这种记住之前发生的事的能力. 再分析 Data0 的时候, 我们把 ...

  6. node.js初识01

    1.对于node.js的安装在这里就不做过多的介绍了,安装成功后,可以通过cmd 输入node -v查看node的版本号,如图所示 2.开始我们的hello world,通过cmd进入所属文件夹,输入 ...

  7. JavaScript学习笔记之call和apply

    前端的知识面太广了,想要记住所有知识点是不可能的,所以将这些学过的记录下来,随时都可以翻开来参考 1.call方法 调用一个对象的一个方法,call(this, arg1, arg2,argN);用来 ...

  8. a标签响应onclick事件,并且不执行href动作

    1.javascript:void(0)相当于一个死链接,href不执行 <a href="javascript:void(0)" onclick="doSomet ...

  9. CoreWebApi集成Exceptionless

    参考博客 https://www.cnblogs.com/akaxb/p/7207827.html 一. Exceptionlessr的安装部署 依乐祝,<ASP.NET Core免费开源分布式 ...

  10. JVM调优总结 -Xms

    转载自:http://unixboy.iteye.com/blog/174173/ 设置eclipse : -Xms256M -Xmx512M -XX:PermSize=256m -XX:MaxPer ...