You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shell-project/os/get-redis-instance.sh

100 lines
3.2 KiB

2 years ago
#!/usr/bin/bash
<<!
redis 多实例配置脚本
!
REDIS_INSTALL_PATH=/data/application/redis
REDIS_COMMAND_PATH=$REDIS_INSTALL_PATH/src
REDIS_CONFIG_PATH=$REDIS_INSTALL_PATH/redis.conf
REDIS_BIND_IP="0.0.0.0"
REDIS_PORT=6379
REDIS_INSTANCE_PATH=/opt/instace/redis
ERROR_INFO="
Usage: $0 \n
\t-i\t--install 安装redis\n
\t-s\t--instance 多实例配置\n
\t--start\t启动redis\n
\t--stop\t停止redis\n"
install_function(){
if [ ! -d $REDIS_INSTALL_PATH ];then
mkdir -p $REDIS_INSTALL_PATH
fi
yum -y install gcc gcc-c++ make wget
wget http://download.redis.io/releases/redis-4.0.9.tar.gz && \
tar -zxvf redis-4.0.9.tar.gz && \
cp -r redis-4.0.9/* $REDIS_INSTALL_PATH && \
rm -rf redis-4.0.9 redis-4.0.9.tar.gz
cd $REDIS_INSTALL_PATH && \
make
echo -e "安装完成\nredis安装目录:$REDIS_INSTALL_PATH\nredis配置文件:$REDIS_CONFIG_PATH\nredis命令目录:$REDIS_COMMAND_PATH"
}
install_instance_function(){
INSTANCE_NAME=$1
if [ ! -d $REDIS_INSTANCE_PATH ];then
mkdir -p $REDIS_INSTANCE_PATH
fi
if [ ! -d $REDIS_INSTALL_PATH ];then
echo "请先安装redis,或修改脚本中的安装目录"
exit 1
fi
if [ -d $REDIS_INSTANCE_PATH/$INSTANCE_NAME ];then
echo "实例已存在"
exit 1
fi
mkdir -p $REDIS_INSTANCE_PATH/$INSTANCE_NAME
cp $REDIS_CONFIG_PATH $REDIS_INSTANCE_PATH/$INSTANCE_NAME/redis.conf
sed -i "s#^port.*#port $INSTANCE_NAME#" $REDIS_INSTANCE_PATH/$INSTANCE_NAME/redis.conf
sed -i "s#^bind.*#bind $REDIS_BIND_IP#" $REDIS_INSTANCE_PATH/$INSTANCE_NAME/redis.conf
sed -i "s#^pidfile.*#pidfile $REDIS_INSTANCE_PATH/$INSTANCE_NAME/redis_$INSTANCE_NAME.pid#" $REDIS_INSTANCE_PATH/$INSTANCE_NAME/redis.conf
sed -i "s#^logfile.*#logfile $REDIS_INSTANCE_PATH/$INSTANCE_NAME/redis_$INSTANCE_NAME.log#" $REDIS_INSTANCE_PATH/$INSTANCE_NAME/redis.conf
sed -i "s#^dir.*#dir $REDIS_INSTANCE_PATH/$INSTANCE_NAME#" $REDIS_INSTANCE_PATH/$INSTANCE_NAME/redis.conf
sed -i "s#^daemonize.*#daemonize yes#" $REDIS_INSTANCE_PATH/$INSTANCE_NAME/redis.conf
echo -e "实例安装完成\n实例目录:$REDIS_INSTANCE_PATH/$INSTANCE_NAME\n实例配置文件:$REDIS_INSTANCE_PATH/$INSTANCE_NAME/redis.conf"
}
option=$(getopt -l "help::,install,instance,start,stop" -o "i,s:" -a -- "$@")
if [ $# -lt 1 ];then
echo -e $ERROR_INFO
exit 0
fi
eval set -- "$option"
while true
do
case $1 in
-i|--install)
install_function
shift 1
;;
-s|--instance)
install_instance_function $2
shift 2
;;
--start)
for i in `ls $REDIS_INSTANCE_PATH`
do
$REDIS_COMMAND_PATH/redis-server $REDIS_INSTANCE_PATH/$i/redis.conf
echo "启动实例$i"
done
shift 1
;;
--stop)
for i in `ls $REDIS_INSTANCE_PATH`
do
$REDIS_COMMAND_PATH/redis-cli -p $i shutdown
echo "停止实例$i"
done
shift 1
;;
--)
shift
break
;;
*)
echo -e $ERROR_INFO
exit 0
;;
esac
done