#!/bin/bash
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
: '
Copyright (C) 2022 - now HaoZi Technology Co., Ltd.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
'
source <(curl -f -s --connect-timeout 10 --retry 3 https://dl.cdn.haozi.net/panel/public.sh)
if [ $? -ne 0 ]; then
echo "下载 public.sh 失败,请检查网络或稍后重试。"
echo "Download public.sh failed, please check the network or try again later."
exit 1
fi
channel=${1}
version=${2}
redis_path="${setup_path}/server/redis"
# 预检查
systemctl is-active --quiet redis
if [ "$?" != "0" ]; then
error "应用运行状态不正常"
fi
# 停止已有服务
systemctl stop redis
# 读取信息
redis_config="${redis_path}/redis.conf"
redis_port=$(cat ${redis_config} | grep 'port ' | grep -v '#' | awk '{print $2}')
redis_pass=$(cat ${redis_config} | grep 'requirepass ' | grep -v '#' | awk '{print $2}')
redis_host=$(cat ${redis_config} | grep 'bind ' | grep -v '#' | awk '{print $2}')
redis_dir=$(cat ${redis_config} | grep 'dir ' | grep -v '#' | awk '{print $2}')
# 备份
if [ -z "${redis_pass}" ]; then
redis-cli -p ${redis_port} <>${redis_path}/redis.conf
fi
# 恢复配置
if [ -n "${redis_pass}" ]; then
sed -i "s!# requirepass .*!requirepass ${redis_pass}!g" ${redis_path}/redis.conf
fi
if [ -n "${redis_host}" ]; then
sed -i "s!bind .*!bind ${redis_host}!g" ${redis_path}/redis.conf
fi
if [ -n "${redis_port}" ]; then
sed -i "s!port .*!port ${redis_port}!g" ${redis_path}/redis.conf
fi
if [ -n "${redis_dir}" ]; then
sed -i "s!dir .*!dir ${redis_dir}!g" ${redis_path}/redis.conf
fi
# 恢复数据
if [ -f "/tmp/redisdump.rdb.bak" ]; then
mv /tmp/redisdump.rdb.bak ${redis_dir}/redisdump.rdb
fi
chown -R redis:redis ${redis_path}
chmod -R 700 ${redis_path}
# 设置服务
cp -r utils/systemd-redis_server.service /etc/systemd/system/redis.service
sed -i "s!ExecStart=.*!ExecStart=${redis_path}/bin/redis-server ${redis_path}/redis.conf!g" /etc/systemd/system/redis.service
sed -i "s!#User=.*!User=redis!g" /etc/systemd/system/redis.service
sed -i "s!#Group=.*!Group=redis!g" /etc/systemd/system/redis.service
sed -i "s!#WorkingDirectory=.*!WorkingDirectory=${redis_path}!g" /etc/systemd/system/redis.service
systemctl daemon-reload
systemctl start redis
panel-cli app write redis ${channel} ${version}
echo -e $HR
echo "升级完成"
echo -e $HR