Redis部署脚本(完成-第一版)
主要包含redis的一键部署脚本和redis启动脚本文件
当前可以完成一键部署,还需改进语法
install.sh
#!/bin/bash
systemctl stop firewalld
systemctl disable firewalld
#!/bin/bash
if ! command -v yum &> /dev/null; then
echo -e "\e[31m yum not installed,start install yum... \e[0m\n"
sudo apt update sudo
apt install yum
else
echo -e "\e[31m yum is installed \e[0m\n"
fiecho -e "\e[31m Starts verifying whether GCC is installed \e[0m\n"
if ! command -v gcc &> /dev/null; then
echo -e "\e[31m gcc not installed,start install gcc... \e[0m\n"
sudo yum install gcc
else
echo -e "\e[31m gcc is installed \e[0m\n"
fiif ! command -v g++ &> /dev/null; then
echo -e "\e[31m g++ not installed,start install g++... \e[0m\n"
sudo yum install g++
else
echo -e "\e[31m g++ is installed \e[0m\n"
fiecho -e "\e[31m Started upgrading the GCC version \e[0m\n"
if [ "$(gcc -dumpversion)" == "4.8.5" ]; then
echo -e "\e[31m The GCC version is 4.8.5 to prevent Redis compilation failures and start upgrading GCC... \e[0m\n"
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
gcc -dumpversion
if [ "$(gcc -dumpversion)" != "4.8.5" ]; then
echo -e "\e[31m If the GCC upgrade fails, manually upgrade the GCC version to prevent Redis compilation failures \e[0m\n"
exit 1
else
echo "\e[31m The GCC upgrade is complete \e[0m\n"
exit 0
fi
fi
if ! command -v make &> /dev/null; then
echo -e "\e[31m Make is not installed, start installing make... \e[0m\n"
sudo yum install make
else
echo -e "\e[31m make is installed \e[0m\n"
fiif ! command -v vim &> /dev/null; then
echo -e "\e[31m vim does not install,start install vim... \e[0m\n"
sudo yum install vim
else
echo -e "\e[31m vim is installed \e[0m\n"
fiecho -e "\e[31m start install Redis \e[0m\n"
REDIS_VERSION="4.0.1"
REDIS_SRC_FILENAME="redis-$REDIS_VERSION.tar.gz"
REDIS_SRC_DIRNAME="redis-$REDIS_VERSION"
tar -xvzf $REDIS_SRC_FILENAME
echo -e "\e[31m The Redis installation is complete \e[0m\n"echo -e "\e[31m Start compiling Redis \e[0m\n"
cd $REDIS_SRC_DIRNAME
make
make install
echo -e "\e[31m Redis compilation is complete \e[0m\n"
dir1="/data/redis/data"mkdir -p $dir1
dir2="/etc/redis/"
mkdir -p $dir2echo -e "\e[31m Configure a Redis configuration file,reids.conf \e[0m\n"
if [ $? -eq 0 ]; then
cat > /etc/redis/6379.conf <<EOF
bind 0.0.0.0
protected-mode no
port 6379
pidfile "/data/redis/redis_6379.pid"
logfile "/data/redis/redis_6379.log"
dir "/data/redis/data"
daemonize yes
requirepass promace@123
appendonly yes
appendfilename "appendonly-41.aof"
EOF
echo -e "\e[31m reids.conf The configuration successful \e[0m\n"
else
echo -e "\e[31m reids.conf The configuration failed!!! \e[0m\n"
fi#此处目录在redis-4.0.1
current_dir=$(pwd)
cd ../
##此处目录在redis
current_dir2=$(pwd)cp $current_dir2/redis-init $current_dir2/redis
echo -e "\e[31m Configure the Redis startup script file \e[0m\n"
sed -i '5a EXEC='$current_dir'/src/redis-server' $current_dir2/redis
sed -i '5a CLIEXEC='$current_dir'/src/redis-cli' $current_dir2/redismv $current_dir2/redis /etc/init.d/
echo -e "\e[31m The Redis startup script is successfully configured \e[0m\n"
echo -e "\e[31m /etc/init.d/redis The file configuration is successful! \e[0m\n"sudo chmod +x /etc/init.d/redis
sudo chkconfig --add redis
sudo chkconfig redis on
current_dir3=$(pwd)
if [ $? -eq 0 ]; then
cat > $current_dir3/start.sh<<EOF
service redis start
EOF
echo -e "\e[31m start.sh The creation is successful! \e[0m\n"
else
echo -e "\e[31m start.sh Creation failed! \e[0m\n"
fiif [ $? -eq 0 ]; then
cat > $current_dir3/stop.sh<<EOF
service redis stop
EOF
echo -e "\e[31m stop.sh The creation is successful! \e[0m\n"
else
echo -e "\e[31m stop.sh Creation failed! \e[0m\n"
fi
exit 0
redis-init(Redis启动脚本)
#!/bin/sh
# chkconfig: 2345 80 90
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDISPORT=6379
PIDFILE=/data/redis/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
case "$1" in
start)
if [ -f $PIDFILE ]; then
echo -e "\e[31m $PIDFILE exists, process is already running or crashed \e[0m\n"
cd /data/redis/
rm -rf redis_${REDISPORT}.pid
echo -e "\e[31m Redis Stopping ... ,please re-execute sh start.sh \e[0m\n"
else
echo -e "\e[31m Starting Redis server... \e[0m\n"
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]; then
echo -e "\e[31m $PIDFILE does not exist, process is not running \e[0m\n"
else
PID=$(cat $PIDFILE)
echo -e "\e[31m Stopping ... \e[0m\n"
kill -9 $PID
while [ -x /proc/${PID} ]
do
echo -e "\e[31m Waiting for Redis to shutdown ... \e[0m\n"
sleep 1
done
echo -e "\e[31m Redis stopped \e[0m\n"
fi
;;
*)
echo -e "\e[31m Please use start or stop as first argument \e[0m\n"
;;
esac