Update 试验用脚本

This commit is contained in:
2025-11-03 12:27:47 +08:00
committed by GitHub
parent 3b29d37920
commit fc11d827d6

View File

@@ -1,60 +1,146 @@
#!/bin/bash
# Syncthing 智能管理脚本 - 安装/卸载二合一
set -e
# 配置变量
SYNCTHING_VERSION="v1.27.7"
INSTALL_DIR="/opt/syncthing"
CONFIG_DIR="$HOME/.config/syncthing"
SERVICE_FILE="/etc/systemd/system/syncthing.service"
BIN_PATH="/usr/local/bin/syncthing"
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
NC='\033[0m' # No Color
# 显示菜单
show_menu() {
# 输出函数
log_info() { echo -e "${BLUE} $1${NC}"; }
log_success() { echo -e "${GREEN}✅ $1${NC}"; }
log_warning() { echo -e "${YELLOW}⚠️ $1${NC}"; }
log_error() { echo -e "${RED}❌ $1${NC}"; }
# 显示使用说明
show_usage() {
echo -e "${BLUE}"
echo "🐱====================================🐱"
echo " Syncthing 一键管理脚本"
echo "🐱====================================🐱"
echo "================================================"
echo " Syncthing 智能管理脚本"
echo "================================================"
echo -e "${NC}"
echo "1. 🚀 安装 Syncthing"
echo "2. 🗑️ 卸载 Syncthing"
echo "3. 🔄 重启 Syncthing 服务"
echo "4. 📊 查看服务状态"
echo "5. ❌ 退出"
echo "使用方法: $0 [command]"
echo ""
read -p "💕 请选择操作 (1-5): " choice
echo "命令:"
echo " install - 安装 Syncthing"
echo " uninstall - 卸载 Syncthing"
echo " reinstall - 重新安装 Syncthing"
echo " status - 查看服务状态"
echo " restart - 重启服务"
echo " 不指定命令 - 自动检测并安装或显示状态"
echo ""
echo "特性:"
echo " ✅ 直接IP访问 (0.0.0.0:8384)"
echo " ✅ 系统服务守护"
echo " ✅ 完整文件权限"
echo " ✅ 一键安装卸载"
echo -e "${BLUE}================================================"
echo -e "${NC}"
}
# 检查是否已安装
check_installed() {
if [ -f "$BIN_PATH" ] || [ -f "$INSTALL_DIR/syncthing" ] || systemctl is-enabled syncthing 2>/dev/null | grep -q enabled; then
return 0 # 已安装
else
return 1 # 未安装
fi
}
# 获取本机IP
get_ip() {
hostname -I | awk '{print $1}' 2>/dev/null || echo "127.0.0.1"
}
# 显示状态
show_status() {
if check_installed; then
log_success "Syncthing 已安装"
echo ""
echo "📁 安装目录: $INSTALL_DIR"
echo "⚙️ 配置目录: $CONFIG_DIR"
echo "🔧 二进制文件: $BIN_PATH"
echo ""
# 检查服务状态
if systemctl is-active --quiet syncthing; then
log_success "服务状态: 运行中"
echo "🌐 访问地址: http://$(get_ip):8384"
else
log_warning "服务状态: 已停止"
fi
# 检查端口监听
if netstat -tuln 2>/dev/null | grep -q ":8384 "; then
log_success "端口 8384: 正在监听"
else
log_warning "端口 8384: 未监听"
fi
else
log_warning "Syncthing 未安装"
fi
}
# 安装函数
install_syncthing() {
echo -e "${GREEN}🐱 开始安装 Syncthing...${NC}"
log_info "开始安装 Syncthing..."
# 检测系统
if [ -f /etc/debian_version ]; then
echo -e "${YELLOW}🍥 检测到 Debian/Ubuntu 系统${NC}"
# 安装 Syncthing
curl -s -o /usr/share/keyrings/syncthing-archive-keyring.gpg https://syncthing.net/release-key.gpg
echo "deb [signed-by=/usr/share/keyrings/syncthing-archive-keyring.gpg] https://apt.syncthing.net/ syncthing stable" | tee /etc/apt/sources.list.d/syncthing.list
apt update && apt install -y syncthing
elif [ -f /etc/redhat-release ]; then
echo -e "${YELLOW}🎀 检测到 CentOS/RHEL 系统${NC}"
dnf install -y syncthing 2>/dev/null || (yum install -y epel-release && yum install -y syncthing)
else
echo -e "${RED}❌ 不支持的系统${NC}"
# 检查是否已安装
if check_installed; then
log_warning "Syncthing 已安装,如需重新安装请使用: $0 reinstall"
show_status
exit 1
fi
echo -e "${YELLOW}🎯 配置 Syncthing 服务...${NC}"
# 创建服务配置
cat > /etc/systemd/system/syncthing@root.service << 'EOF'
# 创建安装目录
log_info "创建安装目录..."
sudo mkdir -p $INSTALL_DIR
sudo chown $USER:$USER $INSTALL_DIR
# 下载并安装
log_info "下载 Syncthing $SYNCTHING_VERSION..."
cd /tmp
wget -q --show-progress https://github.com/syncthing/syncthing/releases/download/$SYNCTHING_VERSION/syncthing-linux-amd64-$SYNCTHING_VERSION.tar.gz
log_info "解压安装包..."
tar xzf syncthing-linux-amd64-$SYNCTHING_VERSION.tar.gz
cd syncthing-linux-amd64-$SYNCTHING_VERSION
# 安装二进制文件
log_info "安装二进制文件..."
sudo cp syncthing $INSTALL_DIR/
sudo chmod +x $INSTALL_DIR/syncthing
sudo ln -sf $INSTALL_DIR/syncthing $BIN_PATH
# 创建配置目录
log_info "创建配置目录..."
mkdir -p $CONFIG_DIR
# 创建 systemd 服务文件
log_info "创建系统服务..."
sudo tee $SERVICE_FILE > /dev/null <<EOF
[Unit]
Description=Syncthing - Open Source Continuous File Synchronization for %i
Description=Syncthing - Open Source Continuous File Synchronization
Documentation=man:syncthing(1)
After=network.target
[Service]
User=%i
ExecStart=/usr/bin/syncthing serve --no-browser --no-restart --logflags=0 --gui-address="0.0.0.0:8384"
Type=simple
User=$USER
ExecStart=$BIN_PATH -no-browser -gui-address="0.0.0.0:8384" -no-restart
Restart=on-failure
RestartSec=5
SuccessExitStatus=3 4
RestartForceExitStatus=3 4
@@ -62,142 +148,132 @@ RestartForceExitStatus=3 4
WantedBy=multi-user.target
EOF
echo -e "${YELLOW}🔧 设置防火墙...${NC}"
# 开放端口(如果防火墙开启的话)
if command -v ufw >/dev/null; then
ufw allow 8384/tcp comment "Syncthing GUI" 2>/dev/null
ufw allow 22000/tcp comment "Syncthing Transfer" 2>/dev/null
ufw allow 21027/udp comment "Syncthing Discovery" 2>/dev/null
echo -e "${GREEN}✅ UFW 防火墙端口已开放${NC}"
elif command -v firewall-cmd >/dev/null; then
firewall-cmd --permanent --add-port=8384/tcp 2>/dev/null
firewall-cmd --permanent --add-port=22000/tcp 2>/dev/null
firewall-cmd --permanent --add-port=21027/udp 2>/dev/null
firewall-cmd --reload 2>/dev/null
echo -e "${GREEN}✅ Firewalld 防火墙端口已开放${NC}"
else
echo -e "${YELLOW}📝 未检测到防火墙,跳过端口配置${NC}"
fi
# 重新加载 systemd
sudo systemctl daemon-reload
echo -e "${YELLOW}📁 创建备份目录...${NC}"
mkdir -p /syncthing_backup
echo -e "${GREEN}💾 备份目录已创建: /syncthing_backup${NC}"
echo -e "${YELLOW}🚀 启动服务...${NC}"
systemctl daemon-reload
systemctl enable syncthing@root
systemctl start syncthing@root
# 启动服务
log_info "启动 Syncthing 服务..."
sudo systemctl enable syncthing
sudo systemctl start syncthing
# 等待服务启动
sleep 3
log_info "等待服务启动..."
for i in {1..10}; do
if systemctl is-active --quiet syncthing; then
break
fi
sleep 2
done
echo -e "${GREEN}🎉 Syncthing 安装完成!${NC}"
show_access_info
# 检查服务状态
if systemctl is-active --quiet syncthing; then
log_success "Syncthing 安装成功!"
log_success "访问地址: http://$(get_ip):8384"
log_success "配置目录: $CONFIG_DIR"
else
log_error "服务启动失败,检查日志: journalctl -u syncthing -f"
exit 1
fi
# 清理临时文件
rm -rf /tmp/syncthing-linux-amd64*
}
# 卸载函数
uninstall_syncthing() {
echo -e "${YELLOW}🗑️ 开始卸载 Syncthing...${NC}"
log_info "开始卸载 Syncthing..."
if ! check_installed; then
log_warning "Syncthing 未安装,无需卸载"
exit 1
fi
# 停止服务
systemctl stop syncthing@root 2>/dev/null
systemctl disable syncthing@root 2>/dev/null
log_info "停止服务..."
sudo systemctl stop syncthing 2>/dev/null || true
sudo systemctl disable syncthing 2>/dev/null || true
# 删除服务文件
rm -f /etc/systemd/system/syncthing@root.service
systemctl daemon-reload
log_info "删除服务文件..."
sudo rm -f $SERVICE_FILE
sudo systemctl daemon-reload
# 卸载软件包
if [ -f /etc/debian_version ]; then
apt remove -y syncthing
rm -f /etc/apt/sources.list.d/syncthing.list
rm -f /usr/share/keyrings/syncthing-archive-keyring.gpg
apt update
elif [ -f /etc/redhat-release ]; then
dnf remove -y syncthing 2>/dev/null || yum remove -y syncthing
fi
# 删除安装文件
log_info "删除安装文件..."
sudo rm -rf $INSTALL_DIR
# 关闭防火墙端口
if command -v ufw >/dev/null; then
ufw delete allow 8384/tcp 2>/dev/null
ufw delete allow 22000/tcp 2>/dev/null
ufw delete allow 21027/udp 2>/dev/null
elif command -v firewall-cmd >/dev/null; then
firewall-cmd --permanent --remove-port=8384/tcp 2>/dev/null
firewall-cmd --permanent --remove-port=22000/tcp 2>/dev/null
firewall-cmd --permanent --remove-port=21027/udp 2>/dev/null
firewall-cmd --reload 2>/dev/null
fi
# 删除符号链接
log_info "删除符号链接..."
sudo rm -f $BIN_PATH
echo -e "${YELLOW}📁 是否删除备份目录?${NC}"
read -p "💭 输入 'yes' 删除 /syncthing_backup 目录: " confirm
if [ "$confirm" = "yes" ]; then
rm -rf /syncthing_backup
echo -e "${GREEN}✅ 备份目录已删除${NC}"
# 询问是否删除配置
echo ""
read -p "❓ 是否删除配置文件和数据? [y/N]: " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
log_info "删除配置文件..."
rm -rf $CONFIG_DIR
log_success "配置已删除"
else
echo -e "${YELLOW}📁 备份目录保留在 /syncthing_backup${NC}"
log_info "配置保留在: $CONFIG_DIR"
fi
echo -e "${GREEN}✅ Syncthing 卸载完成!${NC}"
log_success "Syncthing 卸载完成!"
}
# 重新安装函数
reinstall_syncthing() {
log_info "开始重新安装 Syncthing..."
$0 uninstall
sleep 2
$0 install
}
# 重启服务
restart_service() {
echo -e "${YELLOW}🔄 重启 Syncthing 服务...${NC}"
systemctl restart syncthing@root
sleep 2
systemctl status syncthing@root --no-pager
if check_installed; then
log_info "重启 Syncthing 服务..."
sudo systemctl restart syncthing
sleep 3
show_status
else
log_error "Syncthing 未安装,请先安装"
exit 1
fi
}
# 查看状态
show_status() {
echo -e "${YELLOW}📊 Syncthing 服务状态:${NC}"
systemctl status syncthing@root --no-pager
echo ""
echo -e "${YELLOW}🌐 端口监听状态:${NC}"
netstat -tlnp | grep 8384 || echo "📭 8384 端口未监听"
netstat -tlnp | grep 22000 || echo "📭 22000 端口未监听"
}
# 显示访问信息
show_access_info() {
echo ""
echo -e "${BLUE}========================================"
echo "🐾 Syncthing 管理界面访问信息:"
echo "🌐 外网访问: http://$(curl -s ifconfig.me):8384/"
echo "💻 本地访问: http://localhost:8384/"
echo "📁 备份目录: /syncthing_backup"
echo "🔧 服务管理: systemctl status/start/stop syncthing@root"
echo "========================================"
echo -e "${NC}"
}
# 主循环
while true; do
show_menu
case $choice in
1)
# 主程序
case "${1:-}" in
"install")
install_syncthing
;;
2)
"uninstall")
uninstall_syncthing
;;
3)
restart_service
"reinstall")
reinstall_syncthing
;;
4)
"status")
show_status
;;
5)
echo -e "${GREEN}🐱 再见!祝你有个美好的一天!${NC}"
exit 0
"restart")
restart_service
;;
"")
# 无参数时自动判断
if check_installed; then
show_status
else
log_info "未检测到 Syncthing开始安装..."
install_syncthing
fi
;;
"help"|"-h"|"--help")
show_usage
;;
*)
echo -e "${RED}❌ 无效选择,请重新输入${NC}"
log_error "未知命令: $1"
show_usage
exit 1
;;
esac
echo ""
read -p "💕 按回车键继续..."
clear
done
esac