向晚时光

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

笔记 ·

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

# 检查并安装依赖项的函数
check_dependencies() {
  local dependencies=("jq" "wget" "rsync")

  for dep in "${dependencies[@]}"; do
    if ! command -v "$dep" >/dev/null 2>&1; then
      echo "正在安装脚本运行所需依赖 $dep..."
      if [ "$dep" == "jq" ]; then
        sudo apt-get update -y
        sudo apt-get install -y jq
      elif [ "$dep" == "wget" ]; then
        sudo apt-get install -y wget
      elif [ "$dep" == "rsync" ]; then
        sudo apt-get install -y rsync
      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"

# 从 GitHub 获取最新版本
latest_tag=$(curl -s "https://api.github.com/repos/flucont/btcloud/tags" | jq -r '.[0].name')
# 从 GitHub 获取最新版本(国内机器专用)
#latest_tag=$(curl -s "https://gha.push.ac.cn/repos/flucont/btcloud/tags" | jq -r '.[0].name')

# 下载并解压最新版本
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_dir="/$base_dir/btcloud-upgrade"
  mkdir -p "$temp_dir"

  # 下载并解压最新版本
  zip_file="/$temp_dir/btcloud_$latest_tag.zip"
  wget -q "$download_url" -O "$zip_file"
  unzip -q "$zip_file" -d "$temp_dir"

  # 检查解压是否成功
  if [ "$(ls -A "$temp_dir")" ]; then
    # 创建或更新安装目录(跳过数据目录)
    mkdir -p "$base_dir"
    rsync -a --exclude="data" "$temp_dir/" "$base_dir/"
    # 清理临时文件
    rm -rf "$base_dir/btcloud_$latest_tag.zip" "$temp_dir"
    # 批量替换升级包域名
    chmod +x $convert_dir/convert.sh
    bash $convert_dir/convert.sh $base_dir/ $site_url
    #替换相关文案
    sed -i 's/<title>宝塔面板安装脚本<\/title>/<title>宝塔面板 - 42Lab专业版<\/title>/' $base_dir/app/view/index/download.html
    sed -i 's/宝塔面板安装脚本/宝塔面板/g' $base_dir/app/view/index/download.html
    #赋权目录
    chmod -R 755 $base_dir
    chown -R www:www $base_dir
    find $base_dir/ -name ".gitkeep" -exec rm -f {} \;
    echo "已完成升级!"
  else
    echo "升级出错啦!"
    exit 1
  fi

  # 将新版本写入版本文件
  echo "BtCloud $latest_tag" > "$version_file"
}

# 检查版本文件是否存在
if [ ! -f "$version_file" ]; then
  read -p "安装目录中未找到版本信息,是否升级至最新版本: BtCloud $latest_tag ? [Y/n]" -n 1 -r
  echo
  if [[ $REPLY =~ ^[Yy]$ ]]; then
    download_and_extract
  fi
else
  # 从版本文件获取当前版本
  current_version=$(cat "$version_file")

  if [[ "BtCloud $latest_tag" != "$current_version" ]]; then
    read -p "检测到更新,是否升级至最新版本: BtCloud $latest_tag ? [Y/n]" -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
      download_and_extract
    fi
  else
    echo "未检测到更新,本地版本为最新版本: $current_version !"
  fi
fi

配置

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

  • site_url: 域名(默认值:https://your-domain.com
  • base_dir: BtCloud 安装目录(默认值:/www/wwwroot/https://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

添加新评论