deploy docker

#!/bin/sh

source /etc/init.d/functions

 

if [ ! -f ./docker_sh.config ];then
	echo -e \[ERROR] "\033[31m docker_sh.config,文件不存在!\033[0m"
	exit
else
	source ./docker_sh.config
fi

#镜像版本
#DOCKER_CR_VERSION=1.0

##检查docker环境是否安装
nohup docker -v >/dev/null 2>&1 &
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
	docker -v
else 
	echo -e \[ERROR] "\033[31m 请安装Docker环境 \033[0m"
fi

#镜像仓库信息
REGISTRY_CR=$REGISTRY_CR_DEV

#成功数值
SUCCEED_NUM=

###
## 输出显示日志
###
log(){
    TYPE=`tr '[a-z]' '[A-Z]' <<<"$1"`
	if [ -z "$3" ]; then
		echo \[${TYPE}]: $2
	else
		echo -$3 \[${TYPE}]: $2
	fi
}

###
## 获取时间戳
###
get_timestamp(){
	#获取当前时间
	CURRENT=`date "+%Y-%m-%d %H:%M:%S"`
	#获取当前时间戳
	TIMESTAMP=`date -d "$current" +%s`
	echo "${TIMESTAMP}"
}

###
## write docker file
###
write_docker_file(){
	echo "------------------------------------华丽的分割线--------------------------------------"
	NAME=$1
	rm -rf ${PROJECT_PATH}/docker${NAME}/Dockerfile
	log info "${PROJECT_PATH}/docker${NAME}/Dockerfile write Staring..."
	TIMESTAMP=$(get_timestamp)
	log info "Dockerfile content"
	log info ""
	log info ""
	if [ "$NAME" == "display" ]; then
		echo "FROM cr.registry.hn-1.res.sgmc.sgcc.com.cn/prod/k8sbase_images:k8s_templates_v2" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo "" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo "MAINTAINER bingo" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo "" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo "ADD ${PROJECT_TOMCAT_NAME} /home/admin/app/${PROJECT_TOMCAT_NAME}${TIMESTAMP}" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo "" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo "EXPOSE 18080" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo "" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo 'CMD ["/bin/bash", "/home/admin/app/'${PROJECT_TOMCAT_NAME}${TIMESTAMP}'/bin/catalina.sh","run"]' >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		log info "FROM cr.registry.hn-1.res.sgmc.sgcc.com.cn/prod/k8sbase_images:k8s_templates_v2"
		log info "" 
		log info "MAINTAINER bingo"
		log info ""
		log info "ADD ${PROJECT_TOMCAT_NAME} /home/admin/app/${PROJECT_TOMCAT_NAME}${TIMESTAMP}"
		log info ""
		log info "EXPOSE 18080"
		log info ""
		log info 'CMD ["/bin/bash", "/home/admin/app/'${PROJECT_TOMCAT_NAME}${TIMESTAMP}'/bin/catalina.sh","run"]'
	else
		echo "FROM cr.registry.hn-1.res.sgmc.sgcc.com.cn/prod/k8sbase_images:k8s_templates_v2" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo "" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo "ADD ${PROJECT_JAR_NAME} /home/admin/app/${TIMESTAMP}_${PROJECT_JAR_NAME}" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo "" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo "RUN echo 'java -jar $CATALINA_OPTS -Dalicoud.deployment.mode=EDAS_MANAGED -javaagent:/home/admin/ArmsAgent/arms-bootstrap-1.7.0-SNAPSHOT.jar -Darms.licenseKey='1533476027868199@28c7c127c7c86c5' -Darms.appName='hn-znsj-cloud' /home/admin/app/${TIMESTAMP}_${PROJECT_JAR_NAME} --server.port=9090' >> /home/admin/start.sh" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo "" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo "RUN chmod +x /home/admin/start.sh" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo "" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo 'WORKDIR $ADMIN_HOME' >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo "" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo 'CMD ["/bin/bash", "/home/admin/start.sh"]' >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		echo "" >>${PROJECT_PATH}/docker${NAME}/Dockerfile
		log info "FROM cr.registry.hn-1.res.sgmc.sgcc.com.cn/prod/k8sbase_images:k8s_templates_v2"
		log info ""
		log info "ADD ${PROJECT_JAR_NAME} /home/admin/app/${TIMESTAMP}_${PROJECT_JAR_NAME}"
		log info ""
		log info "RUN echo 'java -jar $CATALINA_OPTS -Dalicoud.deployment.mode=EDAS_MANAGED -javaagent:/home/admin/ArmsAgent/arms-bootstrap-1.7.0-SNAPSHOT.jar -Darms.licenseKey='1533476027868199@28c7c127c7c86c5' -Darms.appName='znsj_in' /home/admin/app/${TIMESTAMP}_${PROJECT_JAR_NAME} --server.port=9090' >> /home/admin/start.sh"
		log info ""
		log info "RUN chmod +x /home/admin/start.sh"
		log info ""
		log info 'WORKDIR $ADMIN_HOME'
		log info ""
		log info 'CMD ["/bin/bash", "/home/admin/start.sh"]'
		log info ""
	fi
	log info "${PROJECT_PATH}/docker${NAME}/Dockerfile write succeed..."
}


###
## docker部署推送
###
docker_deploy(){
	echo "------------------------------------华丽的分割线--------------------------------------"
	NAME=$1
	log info "$NAME"
	log info "del old images Staring ...."
	del_images ${NAME}
	log info "del old images end ...."
	#ALI_IMAGES_TAG=$(select_ali_tag_images $NAME)
	#获取当前时间
	CURRENT=`date "+%Y%m%d%H%M%S"`
	ALI_IMAGES_TAG=${NAME}${CURRENT}
	if [ "$NAME" == "display" ]; then
		cd ${PROJECT_PATH}/docker${NAME}
	else
		cd ${PROJECT_PATH}/docker${NAME}
	fi
	log info "************************${REGISTRY_HOST}${REGISTRY_CR}:${ALI_IMAGES_TAG}**************************"
	nohup docker build -t $REGISTRY_HOST$REGISTRY_CR:$ALI_IMAGES_TAG . >/dev/null 2>&1 &
	select_tag_images $ALI_IMAGES_TAG
	log info "************push Alibaba Registry Staring************"
	log info "login Registry Staring ...."
	docker login --username=$REGISTRY_USER_NAME $REGISTRY_HOST -p $REGISTRY_USER_PASWD
	log info "login Registry end ...."
	log info "push Registry Staring ...."
	docker push $REGISTRY_HOST$REGISTRY_CR:$ALI_IMAGES_TAG
	action "docker file ${ALI_IMAGES_TAG} push succeed " /bin/true
	log info "************push Alibaba Registry end************"
	cd ${PROJECT_PATH}
}

###
## 服务列表
###
service_list(){
	read -p '输入部署环境(dev/prod/d/p)default[dev] Enter continue:' DEPLOY_MODE
	if [ -z "$DEPLOY_MODE" ]; then
		REGISTRY_CR=$REGISTRY_CR_DEV
	elif [ "$DEPLOY_MODE" == "prod" ] || [ "$DEPLOY_MODE" == "p" ] || [ "$DEPLOY_MODE" == "P" ]; then
		REGISTRY_CR=$REGISTRY_CR_PROD
	elif [ "$DEPLOY_MODE" == "dev" ] || [ "$DEPLOY_MODE" == "d" ] || [ "$DEPLOY_MODE" == "D" ]; then
		REGISTRY_CR=$REGISTRY_CR_DEV
	else
		log error '\033[31m 输入有误,请重新输入!\033[0m' e
		service_list
	fi
	log info '\033[35m selecd deploy mode '${REGISTRY_CR}' \033[0m' e
	echo ''
	log info '#####################服务列表########################'
	log info '#-----------------\033[31m 0:all \033[0m---------------------------#' e
	log info '#-----------------\033[31m 1:znsj \033[0m--------------------------#' e
	log info '#-----------------\033[31m 2:display \033[0m-----------------------#' e
	log info '#------------------**********-----------------------#'
	log info '#------------------**********-----------------------#'
	log info '#------------------**********-----------------------#'
	log info '#------------------**********-----------------------#'
	log info '#####################################################'
	echo ''
	selecd_services
}

###
## 输入选择服务
###
selecd_services(){
	read -p '输入选择功能(1-2),可以组合选择[1,2...] Enter continue:' SELECD_SERVICE
	if [ ! -z "$SELECD_SERVICE" ]; then
		if [ "$SELECD_SERVICE" == "0" ]; then
			log info '选择所有服务启动...'
			SELECD_SERVICE='1,2'
		fi
		SELECD_SERVICE_NO=`echo $SELECD_SERVICE|sed 's/[0-9]//g'`
		SELECD_SERVICE_SYMBOL=`echo $SELECD_SERVICE_NO|sed 's/[,]//g'`
		if [ ! -z "$SELECD_SERVICE_SYMBOL" ]; then
			log error '\033[31m 输入有误,请重新输入!\033[0m' e
			selecd_services
		else
			OLD_IFS="$IFS"
			IFS=","
			arr=($SELECD_SERVICE)
			IFS="$OLD_IFS"
			if [ -z "$arr" ]; then
				log error '\033[31m 输入有误,请重新输入!\033[0m' e
				selecd_services
			fi
			for s in ${arr[@]}
			do
				if [ $s -eq 1 ]; then
					write_docker_file $PROJECT_NAME
					docker_deploy $PROJECT_NAME
				elif [ $s -eq 2 ]; then
					write_docker_file $DISPLAY_NAME
					docker_deploy $DISPLAY_NAME
				else
					log error '\033[31m 输入有误,请输入(1-2)之间的数字,或者数字0!\033[0m' e
				fi
			done
		fi
	else
		log error '\033[31m 输入有误,请重新输入!\033[0m' e
		selecd_services
	fi
}

###
## 根据部署名称获取新版本名称
###
select_ali_tag_images(){
	NAME=$1
	if [ -z "$DOCKER_CR_VERSION" ]; then
		DOCKER_CR_VERSION=1.0
	fi
	IMAGES_NAME=$(docker images $REGISTRY_HOST$REGISTRY_CR:$NAME$DOCKER_CR_VERSION | grep -v grep | sed -n "2,1p" | awk '{print $2}')
	if [ ! -z "$IMAGES_NAME" ]; then
		VERSION=${IMAGES_NAME:${#NAME}:3}
		VERSION=${VERSION%.*}
		let "NEW_VERSION=${VERSION}+1"
		DOCKER_CR_VERSION=${NEW_VERSION}.0
		select_ali_tag_images $NAME
	else
		rm -rf ${PROJECT_PATH}/docker_cr_version
		echo "DOCKER_CR_VERSION=${DOCKER_CR_VERSION}">>${PROJECT_PATH}/docker_cr_version
		echo "${NAME}${DOCKER_CR_VERSION}"
	fi
}

###
## 查看build的进度条
###
NUM=0
select_tag_images(){
	NAME=$1
	IMAGES_NAME=$REGISTRY_HOST$REGISTRY_CR:$NAME
	IMAGES_ID=$(docker images -q $IMAGES_NAME |sort -u)
	if [ -z "$IMAGES_ID" ]; then
		printf "docker file ${NAME} build [%-20s]%s%%\r" $SUCCEED_NUM $NUM
		LENGTH=${#SUCCEED_NUM}
		if [[ "$LENGTH" == 20 ]]; then
			SUCCEED_NUM=
			NUM=0
		else
			NUM=$(($NUM+5))
			SUCCEED_NUM=#$SUCCEED_NUM
		fi
		select_tag_images $NAME
	else
		SUCCEED_NUM=
		action "docker ${NAME} build succeed " /bin/true
	fi
}

###
## 删除镜像
###
del_images(){
	NAME=$1
	IMAGES_ID=$(docker images -q $REGISTRY_HOST$REGISTRY_CR:$NAME*|sort -u)
	if [ ! -z "$IMAGES_ID" ]; then
		docker rmi -f $(docker images -q $REGISTRY_HOST$REGISTRY_CR:$NAME*|sort -u)
	else
		log info "old images is null"
	fi
}

main(){
	if [ -z "$PROJECT_PATH" ]; then
		echo -e "\033[31m 请配置docker_sh.config文件项目路径,变量名为:PROJECT_PATH \033[0m"
	elif [ ! -d "$PROJECT_PATH" ]; then
		echo -e "\033[31m 请配置docker_sh.config文件项目路径,变量名为:PROJECT_PATH \033[0m"
	elif [ -z "$PROJECT_JAR_NAME" ]; then
		echo -e "\033[31m 请配置docker_sh.config文件后台项目jar包名称,变量名为:PROJECT_JAR_NAME \033[0m"
	elif [ -z "$PROJECT_TOMCAT_NAME" ]; then
		echo -e "\033[31m 请配置docker_sh.config文件前端项目Tomcat名称,变量名为:PROJECT_TOMCAT_NAME \033[0m"
	elif [ -z "$PROJECT_NAME" ]; then
		echo -e "\033[31m 请配置docker_sh.config文件项目部署服务名称,变量名为:PROJECT_NAME \033[0m"
	elif [ -z "$DISPLAY_NAME" ]; then
		echo -e "\033[31m 请配置docker_sh.config文件前端部署服务名称,变量名为:DISPLAY_NAME \033[0m"
	elif [ -z "$REGISTRY_HOST" ]; then
		echo -e "\033[31m 请配置docker_sh.config文件Registry配置信息,变量名为:REGISTRY_HOST \033[0m"
	elif [ -z "$REGISTRY_USER_NAME" ]; then
		echo -e "\033[31m 请配置docker_sh.config文件Registry配置信息,变量名为:REGISTRY_USER_NAME \033[0m"
	elif [ -z "$REGISTRY_USER_PASWD" ]; then
		echo -e "\033[31m 请配置docker_sh.config文件Registry配置信息,变量名为:REGISTRY_USER_PASWD \033[0m"
	elif [ -z "$REGISTRY_CR_DEV" ]; then
		echo -e "\033[31m 请配置docker_sh.config文件开发镜像仓库信息,变量名为:REGISTRY_CR_DEV \033[0m"
	elif [ -z "$REGISTRY_CR_PROD" ]; then
		echo -e "\033[31m 请配置docker_sh.config文件生产镜像仓库信息,变量名为:REGISTRY_CR_PROD \033[0m"
	else
		log info "************************脚本开始**************************"
		echo ''
		log info '====================================================='
		log info '====================================================='
		log info '=================Docker Deploy======================='
		log info '====================================================='
		log info '====================================================='
		echo ''
		service_list
		log info "************************脚本结束**************************"
	fi
}


main



mysql mysql源码自动安装脚本:将以下脚本复制到mysql源码文件的目录中,执行mysql_install,使用mysql-5.7.33

#!/bin/bash

#设定mysql目录,端口
echo "---------------------欢迎使用mysql-5.7.33一键部署脚本-------------------------"
sleep 2

dir(){
echo "请输入安装路径,格式:/\$dir/\$dir"
while read my_dir;do
  if [ "$my_dir" = "" ];then
    echo "!!错误,输入请重新输入!!"
  else
    echo "安装路径设置完成"
    sleep 2
    break
  fi
done
}

port(){
echo "请输入安装端口"
while read my_port;do
  n1=`echo "$my_port" | sed s/[0-9]//g`
  port_no=`netstat -antp | awk -F" " '{print $4}' | grep -w $my_port | wc -l`
  if [ "$my_port" = "" ];then
    echo "!!错误,请重新输入!!"
  elif [ ! -z "$n1" ];then
    echo "!!输入的端口号值不符合要求,请重新输入!!" 
  elif [ "$my_port" -lt 1 ] || [ "$my_port" -gt 65535 ];then
    echo "!!输入的端口号超出范围,请重新输入!!"
  elif [ "$port_no" -gt 0 ];then
    echo "!!端口已被占用,请重新输入!!"
  else
    echo "端口设置完毕"
    sleep 2
    break
  fi
done
}

#建立用户、安装目录
user_dir(){
echo "##########################开始建立用户、安装目录##############################"
sleep 2
my_name=`awk -F":" '{ print $1 }' /etc/passwd | grep -wi mysql`
if [  -z "$my_name" ];then
  useradd -s /sbin/nologin mysql
  sleep 2
  echo "mysql管理用户已设置完成:mysql"
else
  echo "mysql管理用户已存在,继续安装"
fi
if [ -d "$my_dir" ];then
  if [ "`ls $my_dir`" != "" ];then
    echo "!!目录下存在文件,请检查!!"
    sleep 2
    exit 10
  else
    mkdir -p $my_dir/{mysql_install,data,conf,logs,tmp}
    echo "目录建立完成"
    sleep 2
  fi
else
  echo "开始建立目录"
  mkdir -p $my_dir/{mysql_install,data,conf,logs,tmp}
  echo "目录建立完成"
  sleep 2
fi
}


#下载依赖环境
envir(){
echo "############################开始下载编译安装必要程序##########################"
sleep 2
ping -c3 baidu.com &>/dev/null
if [ $? -eq 0 ];then
  echo "网络连通正常"
  sleep 2
else 
  echo "!!网络连通异常,请检查!!"
  sleep 2
  exit 10
fi
if [  -f -a "/etc/yum.repos.d/*.repo" ];then
  echo "yum源存在"
  yum -y install epel-release cmake bison ncurses-devel openssl-devel
  yum -y groupinstall "Development Tools"
  sleep 2
  echo "依赖插件安装完毕"
else
  echo "!!yum源不存在,请检查!!"
  sleep 2
  exit
fi
}

#开始编译、安装
inst(){
echo "###############################开始进行编译###################################"
sleep 2
make clean
cmake -DDEFAULT_CHARSET=utf8 -DDEFAULT_CHARSET=utf8 -DCMAKE_INSTALL_PREFIX=$my_dir/mysql_install -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=boost
if [ $? -eq 0 ];then
  echo "编译成功,继续安装"
else
  echo "!编译失败,请检查!"
  sleep 2
  exit
fi
echo "###############################开始进行安装###################################"
make  && make install
if [ $? -eq 0 ];then
  echo "安装成功"
else
  echo "!!安装失败,请检查!!"
  exit 10
fi
}

#mysql配置文件
conf(){
echo "###############################开始配置文件###################################"
sleep 2
cat >$my_dir/conf/mysql.cnf <<EOF
[mysqld]
port = $my_port
user = mysql
basedir = $my_dir/mysql_install
datadir = $my_dir/data
pid-file = $my_dir/mysql_install/mysql.pid
log_error = $my_dir/logs/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = $my_dir/logs/mysql-slow.log

# session memory configure.
read_buffer_size = 8M
read_rnd_buffer_size = 4M
sort_buffer_size = 4M
tmp_table_size = 32M
max_heap_table_size = 32M
join_buffer_size = 4M
thread_cache_size = 64

#innodb engine configure.
innodb_file_per_table = 1
innodb_buffer_pool_size = 5G
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_log_file_size = 2G
innodb_log_buffer_size = 32M
innodb_sort_buffer_size = 64M
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_online_alter_log_max_size = 4G
innodb_open_files = 65535
EOF

#设置属主属组
chown -R mysql:mysql $my_dir

echo "配置文件完毕"
sleep 2
}

myinit(){
echo "###############################开始配置初始化################################"
sleep 2
#mysql初始化
$my_dir/mysql_install/bin/mysqld --defaults-file=$my_dir/conf/mysql.cnf --initialize-insecure
if [ $? -eq 0 ];then
  echo "配置初始化成功"
  sleep 2
else
  echo "!!配置初始化失败,请检查!!"
  sleep 2
  exit
fi

#配置环境变量
if [  -z "`grep -w "export PATH=$my_dir/mysql_install/bin:\$\PATH" /etc/profile`" ];then
  echo "export PATH=$my_dir/mysql_install/bin:\$PATH" >> /etc/profile
  source /etc/profile
else
  :
fi

#配置使用systemctl控制
cat >/usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
ExecStart=$my_dir/mysql_install/bin/mysqld --defaults-file=$my_dir/conf/mysql.cnf
EOF

systemctl daemon-reload
}

dir
port
user_dir
envir
inst
conf
myinit


echo "########################安装完毕,请使用systemctl启动mysql###################"
sleep 2
 

mysql库备份脚本:全库全量备份

#!/bin/bash

DATE=`date +%F-%H-%M-%S`
mysqldump -uroot -p"123Abc.." wordpress > /mysql/bak/wordpress/wp-$DATE.sql
file_name=`ls -l /mysql/bak/wordpress/ | grep -v total | tail -1 | awk -F" " '{print $9}'`

if [ $? -eq 0 ];then
  echo "`date +%F`:$file_name:successful" >> /mysql/bak/wordpress/log/wp_bak.log
else
  echo "The wordpress sql is fails to backup,please check" >> /mysql/bak/wordpress/log/wp_bak_err.log
fi

file_num=`ls -l /mysql/bak/wordpress/*.sql | wc -l`
file_1=`ls -l /mysql/bak/wordpress/*.sql | grep -v total | head -1 | awk -F" " '{print $9}'`
if [ "$file_num" -gt 5 ];then
  rm -rf $file_1
fi

redis redis安装脚本:将安装脚本复制到redis源码目录中执行,按照提示操作即可

#!/bin/bash

#设置redis安装目录
while read -p"请输入redis安装路径(按q退出):" redis_dir;do
  if [ "$redis_dir" == "" ];then
    echo "值为空,请重新输入"
  elif [ "$redis_dir" == "q" ];then
    echo "现在退出redis安装程序"
    exit 10
  else
    echo "安装目录设置完毕"
    break
  fi
done

#设置redis端口
while read -p"请输入redis安装端口:" redis_port;do
  if [ "$redis_port" == "" ];then
    echo "值为空,请重新输入"
  elif [ "$redis_dir" = "q" ];then
    echo "现在退出redis安装程序"
    exit 10
  else
#    lsof -i:$redis_port &>/dev/null
    netstat -lntp | grep -w '$redis_port' &>/dev/null
    if [ $? -eq 0 ];then
      echo "端口已被占用,请重新输入"
    else
      echo "端口设置完毕"
      break
    fi
  fi
done

#建立redis用户
useradd redis -d $redis_dir/redis

sys(){
#进行系统配置调优
grep '^vm.overcommit_memory.*$' /etc/sysctl.conf &>/dev/null
if [ $? -eq 0 ];then
  vm_value=`grep '^vm.overcommit_memory.*$' /etc/sysctl.conf | awk -F"=" '{print $2}' | sed "s/^[ \t]*//g"`
  if [ "$vm_value" != 1 ];then
    sed -i "s/^vm.overcommit_memory.*$/vm.overcommit_memory = 1/g" /etc/sysctl.conf
    sysctl -p
  fi
else
   echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
   sysctl -p
fi

egrep -w "^never$" /sys/kernel/mm/transparent_hugepage/enabled &>/dev/null
if [ $? -ne 0 ];then
  echo "never" >> /sys/kernel/mm/transparent_hugepage/enabled
fi
}
sys

redis_ins(){
#安装redis
make clean
make && make PREFIX=/$redis_dir/redis install
if [ $? -eq 0 ];then
  echo "redis安装成功"
else
  echo "redis安装失败,请检查"
fi
}
redis_ins

file_con(){
#复制配置文件及启停控制文件
cp redis.conf $redis_dir/redis/${redis_port}.conf
cp utils/redis_init_script /etc/init.d/redis_$redis_port

#启动文件
sed -i "s&^REDISPORT.*$&REDISPORT=$redis_port&g" /etc/init.d/redis_$redis_port
sed -i "s&^EXEC.*$&EXEC=$redis_dir\/redis\/bin\/redis-server&g" /etc/init.d/redis_$redis_port
sed -i "s&^CONF.*$&CONF=$redis_dir\/redis\/${redis_port}.conf&g" /etc/init.d/redis_$redis_port
sed -i "s&^CLIEXEC.*$&CLIEXEC=$redis_dir\/redis\/bin\/redis-cli&g" /etc/init.d/redis_$redis_port
sed -i "s&^PIDFILE.*$&PIDFILE=$redis_dir\/redis\/${redis_port}.pid&g" /etc/init.d/redis_$redis_port

#redis配置文件
cd $redis_dir/redis
mkdir log
mkdir backup
sed -i "s&^port.*$&port $redis_port&g" $redis_dir/redis/${redis_port}.conf
sed -i "s&^daemonize.*$&daemonize yes&g" $redis_dir/redis/${redis_port}.conf
sed -i "s&^pidfile.*$&pidfile $redis_dir\/redis\/${redis_port}.pid&g" $redis_dir/redis/${redis_port}.conf
sed -i "s&^logfile.*$&logfile $redis_dir\/redis\/log\/redis.log&g" $redis_dir/redis/${redis_port}.conf

chown -R redis:redis $redis_dir/redis
}

file_con

#redis备份
rebak(){
#rdb备份
rerdb(){
sed -i "s/^save/#save/g" $redis_dir/redis/${redis_port}.conf
#sed -i "/^#save/isave 900 1/" $redis_dir/redis/${redis_port}.conf
sed -i "/#   save \"\"/asave 900 1" $redis_dir/redis/${redis_port}.conf
sed -i "s/^stop-writes-on-bgsave-error.*$/stop-writes-on-bgsave-error no/g" $redis_dir/redis/${redis_port}.conf
sed -i "s/^rdbcompression.*$/rdbcompression yes/g" $redis_dir/redis/${redis_port}.conf
sed -i "s/^dbfilename.*$/dbfilename dump.rdb/g"  $redis_dir/redis/${redis_port}.conf
sed -i "s&^dir.*$&dir $redis_dir\/redis\/backup&g" $redis_dir/redis/${redis_port}.conf
}

reaof(){
sed -i "s/^appendonly.*$/appendonly yes/g" $redis_dir/redis/${redis_port}.conf
sed -i "s/^appendfsync/#appendfsync/g" $redis_dir/redis/${redis_port}.conf
sed -i "/^#appendfsync/iappendfsync everysec" $redis_dir/redis/${redis_port}.conf
sed -i "s&^dir.*$&dir $redis_dir\/redis\/backup&g" $redis_dir/redis/${redis_port}.conf
}


while read -p"是否设置备份[y/n]:" jubak;do
  if [ "$jubak" = "y" ];then
    cat <<-EOF
    1:rdb
    2:aof
    3:ALL
    q:退出
EOF
    while read -p"请选择开启rdb备份/aof备份:" jumeth;do
      case "$jumeth" in
      1)
      rerdb
      break
      ;;
      2)
      reaof
      break
      ;;
      3)
      rerdb
      reaof
      break
      ;;
      q)
      return 1
      ;;
      *)
      echo "value is error"
      ;;
      esac
    done
    break 
 elif [ "$jubak" = "n" ];then
    return 1
#   break
  else
    echo "value is error"
  fi
done
echo "redis备份已设置完毕"

}
rebak

echo "redis安装完毕,请切换到redis用户启动"