146 lines
3.6 KiB
Bash
146 lines
3.6 KiB
Bash
#!/usr/bin/env bash
|
||
set -euo pipefail
|
||
|
||
# Filebrowser 一键安装脚本
|
||
# 用法示例:curl -fsSL "https://pan.shumengya.top/d/scripts/filebrowser/install_filebrowser.sh" | sudo bash
|
||
# 目录结构假设:
|
||
# BASE_URL/linux-amd64/filebrowser
|
||
# BASE_URL/linux-arm64/filebrowser
|
||
|
||
BASE_URL="https://pan.shumengya.top/d/scripts/filebrowser"
|
||
INSTALL_DIR="/shumengya/bin/filebrowser"
|
||
SERVICE_NAME="smy-filebrowser"
|
||
BINARY_NAME="filebrowser"
|
||
|
||
log() { printf '[Filebrowser-安装] %s\n' "$*" >&2; }
|
||
fail() { log "错误: $*" >&2; exit 1; }
|
||
|
||
# 进度条函数
|
||
show_progress() {
|
||
local pid=$1
|
||
local text=$2
|
||
local delay=0.1
|
||
local spin='-\|/'
|
||
|
||
printf "[Filebrowser-安装] %s... " "$text" >&2
|
||
|
||
while ps -p "$pid" > /dev/null 2>&1; do
|
||
local temp=${spin#?}
|
||
printf "\b%c" "$spin" >&2
|
||
local spin=$temp${spin%"$temp"}
|
||
sleep $delay
|
||
done
|
||
printf "\b完成\n" >&2
|
||
}
|
||
|
||
require_root() {
|
||
if [ "${EUID:-$(id -u)}" -ne 0 ]; then
|
||
fail "请使用 root 权限运行 (sudo bash install_filebrowser.sh)"
|
||
fi
|
||
}
|
||
|
||
detect_arch() {
|
||
local machine
|
||
machine=$(uname -m)
|
||
case "$machine" in
|
||
x86_64|amd64) echo "linux-amd64" ;;
|
||
aarch64|arm64) echo "linux-arm64" ;;
|
||
*) fail "不支持的架构: $machine" ;;
|
||
esac
|
||
}
|
||
|
||
download_and_install() {
|
||
local arch=$1
|
||
local tmp_dir
|
||
tmp_dir=$(mktemp -d)
|
||
trap 'if [ -n "${tmp_dir:-}" ] && [ -d "$tmp_dir" ]; then rm -rf "$tmp_dir"; fi' EXIT
|
||
|
||
mkdir -p "$INSTALL_DIR"
|
||
|
||
local bin_url="${BASE_URL}/${arch}/${BINARY_NAME}"
|
||
|
||
log "正在处理 ${BINARY_NAME} ..."
|
||
|
||
# 停止旧服务
|
||
systemctl stop "${SERVICE_NAME}.service" 2>/dev/null || true
|
||
|
||
# 强制清理可能残留的进程 (避免 Text file busy)
|
||
pids=$(pgrep -f "$INSTALL_DIR/$BINARY_NAME" || true)
|
||
if [ -n "$pids" ]; then
|
||
log "清理残留进程..."
|
||
kill -9 $pids 2>/dev/null || true
|
||
fi
|
||
|
||
# 优先检测当前目录下是否有二进制文件(本地安装模式)
|
||
if [ -f "./${BINARY_NAME}" ]; then
|
||
log "发现本地文件 ./${BINARY_NAME},跳过下载..."
|
||
cp "./${BINARY_NAME}" "$tmp_dir/$BINARY_NAME"
|
||
else
|
||
# 下载二进制
|
||
curl -fsSL "$bin_url" -o "$tmp_dir/$BINARY_NAME" &
|
||
show_progress $! "下载二进制文件"
|
||
[ -f "$tmp_dir/$BINARY_NAME" ] || fail "下载失败"
|
||
fi
|
||
|
||
# 安装文件
|
||
cp "$tmp_dir/$BINARY_NAME" "$INSTALL_DIR/$BINARY_NAME"
|
||
chmod +x "$INSTALL_DIR/$BINARY_NAME"
|
||
|
||
# 配置服务
|
||
write_service
|
||
|
||
# 启动服务
|
||
systemctl daemon-reload
|
||
systemctl enable "${SERVICE_NAME}.service"
|
||
systemctl restart "${SERVICE_NAME}.service"
|
||
|
||
sleep 2
|
||
if systemctl is-active --quiet "${SERVICE_NAME}.service"; then
|
||
log "服务启动成功"
|
||
log "Filebrowser 监听端口: 2333"
|
||
log "配置文件和数据库将存储在: $INSTALL_DIR"
|
||
else
|
||
log "警告: 服务启动失败,查看日志:"
|
||
journalctl -u "${SERVICE_NAME}.service" --no-pager -n 10
|
||
fi
|
||
}
|
||
|
||
write_service() {
|
||
local service_file="/etc/systemd/system/${SERVICE_NAME}.service"
|
||
|
||
cat > "$service_file" <<EOF
|
||
[Unit]
|
||
Description=Filebrowser Service
|
||
After=network.target
|
||
|
||
[Service]
|
||
Type=simple
|
||
User=root
|
||
Group=root
|
||
WorkingDirectory=$INSTALL_DIR
|
||
ExecStart=$INSTALL_DIR/$BINARY_NAME -r $INSTALL_DIR -d $INSTALL_DIR/filebrowser.db -l /var/log/filebrowser.log -p 2333 -a 0.0.0.0
|
||
Restart=on-failure
|
||
RestartSec=5
|
||
|
||
[Install]
|
||
WantedBy=multi-user.target
|
||
EOF
|
||
}
|
||
|
||
main() {
|
||
require_root
|
||
local arch
|
||
arch=$(detect_arch)
|
||
log "检测到架构: $arch"
|
||
|
||
download_and_install "$arch"
|
||
|
||
log "=========================================="
|
||
log "安装任务完成!"
|
||
log "安装目录: $INSTALL_DIR"
|
||
log "服务名称: $SERVICE_NAME"
|
||
log "=========================================="
|
||
}
|
||
|
||
main "$@"
|