145 lines
4.2 KiB
Bash
145 lines
4.2 KiB
Bash
#!/bin/bash
|
|
###### 这个脚本用于安装多实例的 redis 必须已经安装了 redis-server 才能执行
|
|
###### 只能修改 read_variables 里的参数
|
|
function read_variables(){
|
|
readonly redis_path="/usr/local/" # redis 安装路径
|
|
readonly redis_name="redis-4.0.8" # redis 安装路径
|
|
readonly port="$1" # 多实例的端口
|
|
readonly password="admin202" # 密码
|
|
}
|
|
|
|
function build_variables(){
|
|
readonly install_path="${redis_path}/$redis_name"
|
|
readonly instance_name="redis_${port}"
|
|
|
|
readonly instalce_service_name="/etc/init.d/redis_${port}"
|
|
readonly instance_conf_name="redis_${port}.conf"
|
|
|
|
readonly redis_server_path="${install_path}/src/redis-server"
|
|
readonly redis_client_path="${install_path}/src/redis-cli"
|
|
readonly backup_conf_name="redis_bak.conf"
|
|
}
|
|
|
|
function rEcho(){
|
|
echo -e "\033[31m\033[01m$1\033[0m"
|
|
}
|
|
|
|
|
|
function check_env() {
|
|
cd $install_path
|
|
[ $? -ne 0 ] && rEcho "Error: [ Path Not Found ]! Exit " && exit
|
|
########### workdir = redis/
|
|
[ ! -f $redis_server_path ] && rEcho "Error: [ redis-server ] Not Found! Exit" && exit
|
|
[ -f $instalce_service_name ] && rEcho "Error: [ service_name: redis_${port} ] Exist! Exit" && exit
|
|
[ -f $instance_conf_name ] && rEcho "Error: [ conf: $instance_conf_name ] Exist! Exit" && exit
|
|
[ ! -f $backup_conf_name ] && rEcho "Error: [ conf: $backup_conf_name ] Not Found! Exit" && exit
|
|
}
|
|
|
|
function do_install() {
|
|
#### workdir = redis/
|
|
cat $backup_conf_name | grep -vw "^#" | grep -vw ^$ > $instance_conf_name
|
|
sed -i '/port/s/6379/'$port'/' $instance_conf_name
|
|
sed -i '/dbfilename/s/dump/dump_'$port'/' $instance_conf_name
|
|
sed -i '/dir/s%./%/%g' $instance_conf_name
|
|
sed -i '/pidfile/s/6379/'$port'/g' $instance_conf_name
|
|
echo "requirepass $password" >> $instance_conf_name
|
|
cat << EOF > $instalce_service_name
|
|
#!/bin/sh
|
|
#
|
|
# Simple Redis init.d script conceived to work on Linux systems
|
|
# as it does use of the /proc filesystem.
|
|
|
|
REDISPORT=${port}
|
|
EXEC=${redis_server_path}
|
|
AUTHEN=${password}
|
|
PIDFILE=/var/run/redis_\${REDISPORT}.pid
|
|
CONF=${install_path}/redis_\${REDISPORT}.conf
|
|
|
|
|
|
|
|
if [ "\${AUTHEN}" == "" ]
|
|
then
|
|
CLIEXEC="${redis_client_path} -p \${REDISPORT}"
|
|
else
|
|
CLIEXEC="${redis_client_path} -a \${AUTHEN} -p \${REDISPORT}"
|
|
fi
|
|
|
|
|
|
case "\$1" in
|
|
start)
|
|
if [ -f \$PIDFILE ]
|
|
then
|
|
echo "\$PIDFILE exists, process is already running or crashed"
|
|
else
|
|
echo "Starting Redis server..."
|
|
\$EXEC \$CONF &
|
|
fi
|
|
;;
|
|
stop)
|
|
if [ ! -f \$PIDFILE ]
|
|
then
|
|
echo "\$PIDFILE does not exist, process is not running"
|
|
else
|
|
PID=\$(cat \$PIDFILE)
|
|
echo "Shutdown Redis? Y/N?"
|
|
read -p "Answer: " choose
|
|
if [ "\$choose" != "Y" ] && [ "\$choose" != "y" ]
|
|
then
|
|
echo -e "\\033[01mExit!\\033[0m" && exit
|
|
fi
|
|
echo "Stopping ..."
|
|
\$CLIEXEC shutdown
|
|
while [ -x /proc/\${PID} ]
|
|
do
|
|
echo "Waiting for Redis to shutdown ..."
|
|
sleep 1
|
|
done
|
|
echo "Redis stopped"
|
|
fi
|
|
;;
|
|
connect)
|
|
\$CLIEXEC
|
|
;;
|
|
*)
|
|
echo "Please use start or stop or connect as first argument"
|
|
;;
|
|
esac
|
|
EOF
|
|
chmod +x $instalce_service_name
|
|
echo -en "\033[01m\033[32m"
|
|
echo "Install [ Redis ${port} ] Success!"
|
|
echo "$instalce_service_name start | service redis_${port} start: Start Redis Server"
|
|
echo "$instalce_service_name stop | service redis_${port} stop: Stop Redis Server"
|
|
echo -en "\033[0m"
|
|
}
|
|
|
|
|
|
function init_redis(){
|
|
read_variables $2
|
|
build_variables
|
|
check_env
|
|
do_install
|
|
}
|
|
|
|
function help1(){
|
|
for i in {1..2} ; do echo ; done
|
|
echo -e "\033[01mInstall Multiple Instances Redis "
|
|
echo -e "$0 init [ Port ]"
|
|
echo
|
|
echo "ForExample: "
|
|
echo -e "$0 init 18001 => Install [ 18001 ] Port Instance\033[0m"
|
|
for i in {1..2}
|
|
do
|
|
echo
|
|
done
|
|
}
|
|
|
|
|
|
if [ $# -eq 2 ]
|
|
then
|
|
[ "$1" != "init" ] && help1 && exit
|
|
init_redis $@
|
|
else
|
|
help1
|
|
fi
|