向晚时光

告别繁琐操作:BtCloud 一键更新脚本

笔记 ·

基于开源项目:BtCloud
https://github.com/flucont/btcloud
#!/bin/bash

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

log_info() {
    echo -e "${GREEN}[INFO] $1${NC}"
}

log_warn() {
    echo -e "${YELLOW}[WARN] $1${NC}"
}

log_error() {
    echo -e "${RED}[ERROR] $1${NC}"
}

check_dependencies() {
    local dependencies=("jq" "wget" "rsync" "curl" "unzip")
    local pkg_manager=""
    local install_cmd=""

    if command -v apt-get >/dev/null 2>&1; then
        pkg_manager="apt-get"
        install_cmd="sudo apt-get install -y"
    elif command -v yum >/dev/null 2>&1; then
        pkg_manager="yum"
        install_cmd="sudo yum install -y"
    elif command -v dnf >/dev/null 2>&1; then
        pkg_manager="dnf"
        install_cmd="sudo dnf install -y"
    else
        log_error "未找到支持的包管理器 (apt/yum/dnf),请手动安装依赖: ${dependencies[*]}"
        exit 1
    fi

    local need_update=0
    for dep in "${dependencies[@]}"; do
        if ! command -v "$dep" >/dev/null 2>&1; then
            if [ "$pkg_manager" == "apt-get" ] && [ $need_update -eq 0 ]; then
                sudo apt-get update -y >/dev/null 2>&1
                need_update=1
            fi
            log_info "正在安装依赖 $dep..."
            $install_cmd "$dep" >/dev/null 2>&1
            if ! command -v "$dep" >/dev/null 2>&1; then
                 log_error "依赖 $dep 安装失败,请检查网络或手动安装。"
                 exit 1
            fi
        fi
    done
}

# 检查并安装依赖项
check_dependencies

# 设置替换域名(按需更改)
site_url="https://your-domain.com"
# 设置安装目录(按需更改)
base_dir="www/wwwroot/your-domain.com"
# 版本文件位置
version_file="$base_dir/version.txt"
# 批量替换脚本位置
convert_dir="$base_dir/app/script"

# 获取最新版本
log_info "正在检查最新版本..."
latest_tag=$(curl -s --connect-timeout 10 "https://api.github.com/repos/flucont/btcloud/tags" | jq -r '.[0].name')
# 获取最新版本(国内机器专用)
# latest_tag=$(curl -s --connect-timeout 10 "https://gha.push.ac.cn/repos/flucont/btcloud/tags" | jq -r '.[0].name')
if [[ -z "$latest_tag" || "$latest_tag" == "null" ]]; then
    log_error "无法获取最新版本号,请检查网络连接或 GitHub API 状态。"
    exit 1
fi

# 下载并解压最新版本
download_and_extract() {
    # 构造下载链接
    download_url="https://github.com/flucont/btcloud/releases/download/$latest_tag/btcloud_$latest_tag.zip"
    # 构造下载链接(国内机器专用)
    # download_url="https://ghp.42lab.link/https://github.com/flucont/btcloud/releases/download/$latest_tag/btcloud_$latest_tag.zip"
    
    temp_work_dir=$(mktemp -d)
    zip_file="$temp_work_dir/btcloud_$latest_tag.zip"
    extract_dir="$temp_work_dir/extract"

    log_info "准备下载版本: $latest_tag"
    log_info "下载地址: $download_url"

    if ! wget -q "$download_url" -O "$zip_file"; then
        log_error "下载失败!"
        rm -rf "$temp_work_dir"
        exit 1
    fi

    log_info "正在解压..."
    mkdir -p "$extract_dir"
    if ! unzip -q "$zip_file" -d "$extract_dir"; then
        log_error "解压失败!"
        rm -rf "$temp_work_dir"
        exit 1
    fi

    if [ "$(ls -A "$extract_dir")" ]; then
        log_info "正在更新文件..."
        
        mkdir -p "$base_dir"
        
        # 同步文件 (排除 data 目录)
        rsync -a --exclude="data" "$extract_dir/" "$base_dir/"
        
        if [ -f "$convert_dir/convert.sh" ]; then
            chmod +x "$convert_dir/convert.sh"
            bash "$convert_dir/convert.sh" "$base_dir/" "$site_url"
        else
             log_warn "未找到 convert.sh,跳过域名替换。"
        fi

        # 替换相关文案
        target_html="$base_dir/app/view/index/download.html"
        if [ -f "$target_html" ]; then
            sed -i 's/<title>宝塔面板安装脚本<\/title>/<title>宝塔面板 - 42Lab专业版<\/title>/' "$target_html"
            sed -i 's/宝塔面板安装脚本/宝塔面板/g' "$target_html"
        fi

        chmod -R 755 "$base_dir"
        chown -R www:www "$base_dir"
        find "$base_dir/" -name ".gitkeep" -exec rm -f {} \;
        
        echo "BtCloud $latest_tag" > "$version_file"
        
        log_info "已完成升级!当前版本: BtCloud $latest_tag"
    else
        log_error "解压目录为空,升级失败!"
        rm -rf "$temp_work_dir"
        exit 1
    fi

    # 清理临时文件
    rm -rf "$temp_work_dir"
}

confirm_update() {
    local prompt_msg="$1"
    read -p "$prompt_msg" -n 1 -r
    echo
    if [[ -z "$REPLY" || "$REPLY" =~ ^[Yy]$ ]]; then
        download_and_extract
    else
        log_info "已取消升级。"
    fi
}

if [ ! -f "$version_file" ]; then
    confirm_update "安装目录中未找到版本信息,是否升级至最新版本: BtCloud $latest_tag ? [Y/n] "
else
    current_version=$(cat "$version_file")
    if [[ "BtCloud $latest_tag" != "$current_version" ]]; then
        confirm_update "检测到更新 ($current_version -> BtCloud $latest_tag),是否升级? [Y/n] "
    else
        log_info "未检测到更新,本地版本为最新版本: $current_version !"
    fi
fi

配置

自行替换脚本中以下信息:

  • site_url: 域名(默认值:https://your-domain.com
  • base_dir: BtCloud 安装目录(默认值:/www/wwwroot/your-domain.com)
  • 其他变量(version_fileconvert_dir)为版本文件和批量替换脚本的路径,一般无需修改。
  • 替换文案的代码段是修改download页面的标题,自行更改。
  • 国内机器如无法直连github请取消脚本中的相关注释符。

修改后脚本会自动按照新路径进行升级操作,并在同步完成后自动替换所有相关的域名配置。

食用

假设保存文件名为:btcloud-upgrade.sh

bash btcloud-upgrade.sh
#或者
chmod +x btcloud-upgrade.sh
./btcloud-upgrade.sh

执行脚本后会自动进入交互流程:

  • 首次使用:提示安装最新版本
  • 有新版本:询问是否升级
  • 已是最新:直接显示当前版本

只需输入 Y 确认,脚本就会自动完成后续所有操作。

DLC

这是另一个配套脚本,功能对应BtCloud后台的自动更新功能,单纯是方便自动更新插件后修复/data 目录下文件权限不对的问题。

#!/bin/bash

# Execute the php command
php /www/wwwroot/your-domain.com/think updateall && php /www/wwwroot/your-domain.com/think clean

# Check the exit status of the previous command
if [ $? -eq 0 ]; then
    # If successful, change the permission of the data directory
    chmod -R 755 /www/wwwroot/your-domain.com/data
    chown -R www:www /www/wwwroot/your-domain.com/data
else
    # If failed, print an error message and exit
    echo "The php command failed. Aborting."
    exit 1
fi

自行修改脚本中的路径,为实际网站路径(默认值:/www/wwwroot/your-domain.com/),然后扔到Crontab。

crontab -e

假设保存名字为SyncPlugins.sh,路径在/root

0 12 * * * /bin/bash /root/SyncPlugins.sh

# btcloud

添加新评论

仅有一条评论

返回顶部 ↑