为减轻 Linux 运维工作中重复执行的工作量, 降低出错概率; 同时保证脚本能应用在不同的场合以及其灵活性, 特地编写此脚本, 仅供参考!
- ?
- #!/bin/bash
- ##Author: fanfubin
- #QQ: 502422514
- #Date: 2019-03-19
- #FileName: Redis.sh
- #Description: The deploy Redis script
- #Copyright (C): 2019 All rights reserved
- #
- source /etc/init.d/functions
- BLACK_COLOR='\E[1;30m'
- RED_COLOR='\E[1;31m'
- GREEN_COLOR='\E[1;32m'
- YELLOW_COLOR='\E[1;33m'
- BLUE_COLOR='\E[1;34m'
- PINK_COLOR='\E[1;35m' #紫色
- SKYBLUE_COLOR='\E[1;36m' #天蓝色
- WHITE_COLOR='\E[1;37m' #白色
- SHAN='\E[31;5m' #字体闪烁
- RES='\E[0m'
- cpu_count=`lscpu|awk -F '+' '/^CPU\(s\):/{print $2}'`
- if [ $cpu_count -le 2 ];then
- export cpu_num="1"
- else
- let cpu_num=`lscpu|awk -F '+' '/^CPU\(s\):/{print $2}'`-1
- export cpu_num
- fi
- export httpd_user="nginx"
- export httpd_group="nginx"
- export httpd_id="2000"
- export redis_***tall_ver="redis-4.0.14.tar.gz"
- export master_default_passwd="123456"
- export ruby_***tall_ver="ruby-2.5.5.tar.gz"
- export redis_gem_***tall_ver="redis-4.1.2.gem"
- f_main_title() {
f_color green "`echo -e"\t 服务器监控系统 "`"
- }
- f_color() {
- case "$1" in
- black)
- echo -e "${PINK_COLOR}$2 ${RES}"
- ;;
- red)
- echo -e "${RED_COLOR}$2 ${RES}"
- ;;
- green)
- echo -e "${GREEN_COLOR}$2 ${RES}"
- ;;
- yellow)
- echo -e "${YELLOW_COLOR}$2 ${RES}"
- ;;
- blue)
- echo -e "${BLUE_COLOR}$2 ${RES}"
- ;;
- skyblue)
- echo -e "${PINK_COLOR}$2 ${RES}"
- ;;
- pink)
- echo -e "${PINK_COLOR}$2 ${RES}"
- ;;
- white)
- echo -e "${PINK_COLOR}$2 ${RES}"
- ;;
- shan)
- echo -e "${SHAN}$2 ${RES}"
- ;;
- *)
- exit
- ;;
- esac
- }
- ## 创建通用的用户名和 id(user:nginx id:2000)
- id $httpd_user
- if [ `echo $?` -ne 0 ];then
- useradd -s /sbin/nologin -M -u $httpd_id $httpd_user
- else
- test_httpd_id=`id $httpd_user | awk -F '[=(]' '{print $2}'`
- test_httpd_gid=`id $httpd_user | awk -F '[=(]' '{print $4}'`
- if [ $test_httpd_id -ne $httpd_id -o $test_httpd_gid -ne $httpd_id ];then
- userdel -r $httpd_id
- useradd -s /sbin/nologin -M -u $httpd_id $httpd_user
- fi
- fi
- f_menu_***tall_server_redis_list() {
- local path
- local redis_***tall_dir
- local num
- local choose_num
- local passwd_choose_num
- local locate_server_num
- local find_server_num
- local new_master_passwd
- local master_passwd
- local redis_gem_***tall_dir
- local gem_passwd
- echo
- f_main_title
- echo "=================================="
- cat <<-EOF
- [1. ] Show ***tall Redis version
- [2. ] Source ***tall Redis && cluster
- [3. ] Show ***tall Ruby && Gem version
- [4. ] Source ***tall Ruby && Gem
- [0. ] Exit
- EOF
- echo "=================================="
- read -p "Pls input a num:" num
- f_is_num $num
- [ $? -eq 1 ] || { action "Pls input valid num!" false ; f_menu_***tall_server_redis_list;}
- case "$num" in
- 1)
- echo
- echo -e "\t$redis_***tall_ver"
- f_menu_***tall_server_redis_list
- ;;
- ###################################################### ***tall Redis && cluster
- 2)
- echo
- #Redis .tar. is exist?
- yum -y ***tall mlocate &> /dev/null
- updatedb &> /dev/null
- locate_server_num=`locate ${redis_***tall_ver}| wc -l`
- if [ ! -e $redis_***tall_ver ];then
- if [ $locate_server_num -eq 1 ];then
- \cp `locate ${redis_***tall_ver}` . -a
- elif [ $locate_server_num -ge 2 ];then
- \cp `locate ${redis_***tall_ver} | head -n1` . -a
- else
- find_server_num=`find / -name ${redis_***tall_ver}| wc -l`
- if [ $find_server_num -eq 1 ];then
- \cp `find / -name ${redis_***tall_ver}` . -a
- elif [ $find_server_num -ge 2 ];then
- \cp `find / -name ${redis_***tall_ver} | head -n1` . -a
- else
- f_color red "`echo"${redis_***tall_ver} is not exist!"`"
- f_menu_***tall_server_redis_list
- fi
- fi
- fi
- redis_***tall_dir=`echo $redis_***tall_ver | awk -F '.tar.' '{print $1}'`
- read -p "Pls input 1-***tall or 2-quit :" choose_num
- f_is_num $choose_num
- [ $? -eq 1 ] || { action "Pls input valid num!" false ; f_menu_***tall_server_redis_list;}
- case "$choose_num" in
- 1)
- echo
- yum -y ***tall gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel &> /dev/null
- read -p "Whether use default masterauth=requirepass 1-yes or 2-no :" passwd_choose_num
- f_is_num $passwd_choose_num
- [ $? -eq 1 ] || { action "Pls input valid num!" false ; f_menu_***tall_server_redis_list;}
- case $passwd_choose_num in
- 1)
- new_master_passwd=$master_default_passwd
- f_color shan "`echo"Default masterauth=password is: $new_master_passwd "`"
- ;;
- 2)
- while true
- do
- read -p "Pls input masterauth=requirepass password:" master_passwd
- if [ -z "$master_passwd" ];then
- f_color red "`echo"password can not be empty !"`"
- else
- new_master_passwd=$master_passwd
- f_color shan "`echo"Masterauth=requirepass password is: $new_master_passwd "`"
- break
- fi
- done
- ;;
- *)
- f_menu_***tall_server_redis_list
- esac
- read -p "Pls input ***tall Path or null:" path
- if [ -z "$path" ];then
- \rm -rf $redis_***tall_dir
- tar xf $redis_***tall_ver
- cd $redis_***tall_dir
- make PREFIX=/apps/$redis_***tall_dir ***tall
- mkdir -p /apps/$redis_***tall_dir/{etc,logs,data,run}
- \cp Redis.conf /apps/$redis_***tall_dir/etc
- \cp src/Redis-trib.rb /usr/bin/
- sed -ri 's/bind 127.0.0.1/bind 0.0.0.0/' /apps/$redis_***tall_dir/etc/Redis.conf
- sed -ri 's/daemonize no/daemonize yes/' /apps/$redis_***tall_dir/etc/Redis.conf
- sed -ri 's/supervised no/supervised systemd/' /apps/$redis_***tall_dir/etc/Redis.conf
- sed -ri 's#/var/run/redis_6379.pid#/apps/'$redis_***tall_dir'/run/redis_6379.pid#' /apps/$redis_***tall_dir/etc/Redis.conf
- sleep 0.5
- sed -ri 's#logfile""#logfile "/apps/'$redis_***tall_dir'/logs/redis_6379.log"#' /apps/$redis_***tall_dir/etc/Redis.conf
- sed -ri 's/stop-writes-on-bgsave-error yes/stop-writes-on-bgsave-error no/' /apps/$redis_***tall_dir/etc/Redis.conf
- sed -ri 's#dbfilename dump.rdb#dbfilename dump_6379.rdb#' /apps/$redis_***tall_dir/etc/Redis.conf
- sed -ri 's#dir \./#dir /apps/'$redis_***tall_dir'/data#' /apps/$redis_***tall_dir/etc/Redis.conf
- sed -ri 's/# masterauth <master-password>/masterauth"'$new_master_passwd'"/' /apps/$redis_***tall_dir/etc/Redis.conf
- sleep 0.5
- sed -ri 's/# requirepass foobared/requirepass'$new_master_passwd'/' /apps/$redis_***tall_dir/etc/Redis.conf
- sed -ri 's/# maxclients 10000/maxclients 65536/' /apps/$redis_***tall_dir/etc/Redis.conf
- sed -ri 's/appendonly no/appendonly yes/' /apps/$redis_***tall_dir/etc/Redis.conf
- sed -ri 's/appendfilename"appendonly.aof"/appendfilename"appendonly_6379.aof"/' /apps/$redis_***tall_dir/etc/Redis.conf
- sleep 0.5
- ##cluster
- sed -ri 's/# cluster-enabled yes/ cluster-enabled yes/' /apps/$redis_***tall_dir/etc/Redis.conf
- sed -ri 's/# cluster-config-file nodes-6379.conf/ cluster-config-file nodes-6379.conf/' /apps/$redis_***tall_dir/etc/Redis.conf
- total_mem=`free -m | grep 'Mem' |awk '{print $2}'`
- let mem_result=$total_mem/2*1024*1024
- sed -ri 's/# maxmemory <bytes>/maxmemory'$mem_result'/' /apps/$redis_***tall_dir/etc/Redis.conf
- ln -s /apps/$redis_***tall_dir/bin/*/usr/bin/
- sleep 1
- cat>/usr/lib/systemd/system/Redis.service<<-EOF
- [Unit]
- Description=Redis persistent key-value database
- After=network.target
- After=network-online.target
- Wants=network-online.target
- [Service]
- ExecStart=/apps/$redis_***tall_dir/bin/Redis-server /apps/$redis_***tall_dir/etc/Redis.conf --supervised systemd
- ExecReload=/bin/kill -s HUP \$MAINPID
- ExecStop=/bin/kill -s QUIT \$MAINPID
- Type=notify
- #User=$httpd_user
- #Group=$httpd_user
- User=root
- Group=root
- RuntimeDirectory=Redis
- RuntimeDirectoryMode=0755
- [Install]
- WantedBy=multi-user.target
- EOF
- #chown -R ${httpd_user}.${httpd_user} /apps/$redis_***tall_dir
- cat>>/etc/sysctl.conf<<-EOF
- net.core.somaxconn = 512
- vm.overcommit_memory = 1
- EOF
- sysctl -p
- sleep 1
- cat>>/etc/rc.d/rc.local<<-EOF
- echo never> /sys/kernel/mm/transparent_hugepage/enabled
- EOF
- chmod a+x /etc/rc.d/rc.local
- systemctl daemon-reload
- sleep 1
- systemctl start Redis
- cd -
- f_color shan "`echo"Redis ***tall path is: /apps/${redis_***tall_dir}"`"
- else
- \rm -rf $redis_***tall_dir
- tar xf $redis_***tall_ver
- cd $redis_***tall_dir
- make PREFIX=$path ***tall
- mkdir -p ${path}/{etc,logs,data,run}
- \cp Redis.conf ${path}/etc
- \cp src/Redis-trib.rb /usr/bin/
- sed -ri 's/bind 127.0.0.1/bind 0.0.0.0/' $path/etc/Redis.conf
来源: http://www.bubuko.com/infodetail-3094306.html