Files
linux-bash/filebrowser/uninstall_filebrowser.sh
2026-02-17 17:28:37 +08:00

110 lines
2.7 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
# Filebrowser 一键卸载脚本
# 用法curl -fsSL "https://pan.shumengya.top/d/scripts/filebrowser/uninstall_filebrowser.sh" | sudo bash
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 uninstall_filebrowser.sh)"
fi
}
stop_and_disable_service() {
if systemctl is-active --quiet "${SERVICE_NAME}.service"; then
systemctl stop "${SERVICE_NAME}.service"
fi
if systemctl is-enabled --quiet "${SERVICE_NAME}.service" 2>/dev/null; then
systemctl disable "${SERVICE_NAME}.service"
fi
}
remove_service_file() {
local service_file="/etc/systemd/system/${SERVICE_NAME}.service"
if [ -f "$service_file" ]; then
rm -f "$service_file"
systemctl daemon-reload
fi
}
cleanup_process() {
# 查找并杀掉所有匹配的进程
local pids
pids=$(pgrep -f "$INSTALL_DIR/$BINARY_NAME" || true)
if [ -n "$pids" ]; then
kill -9 $pids 2>/dev/null || true
fi
}
uninstall_main() {
log "正在卸载 Filebrowser ..."
(stop_and_disable_service) &
show_progress $! "停止服务"
cleanup_process
(remove_service_file) &
show_progress $! "删除服务文件"
if [ -f "$INSTALL_DIR/$BINARY_NAME" ]; then
rm -f "$INSTALL_DIR/$BINARY_NAME"
fi
# 清理日志文件(可选)
if [ -f "/var/log/filebrowser.log" ]; then
rm -f "/var/log/filebrowser.log"
log "已删除日志文件"
fi
# 询问是否删除数据(数据库)
if [ -d "$INSTALL_DIR" ]; then
# 检查目录下是否有除了二进制文件以外的内容(主要是数据库)
if [ -n "$(ls -A "$INSTALL_DIR" 2>/dev/null)" ]; then
log "警告: 安装目录 $INSTALL_DIR 中可能包含用户数据 (如 filebrowser.db)"
# 默认不自动删除数据目录,防止误删,提示用户手动删除
log "为了安全起见,脚本仅删除了二进制文件。若要彻底清除数据,请手动运行: rm -rf $INSTALL_DIR"
else
rm -rf "$INSTALL_DIR"
log "已删除空安装目录"
fi
fi
log "=========================================="
log "卸载任务完成"
log "=========================================="
}
main() {
require_root
uninstall_main
}
main "$@"