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 #!/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' RED='\033[0;31m'
GREEN='\033[0;32m' GREEN='\033[0;32m'
YELLOW='\033[1;33m' YELLOW='\033[1;33m'
BLUE='\033[0;34m' 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 -e "${BLUE}"
echo "🐱====================================🐱" echo "================================================"
echo " Syncthing 一键管理脚本" echo " Syncthing 智能管理脚本"
echo "🐱====================================🐱" echo "================================================"
echo -e "${NC}" echo -e "${NC}"
echo "1. 🚀 安装 Syncthing" echo "使用方法: $0 [command]"
echo "2. 🗑️ 卸载 Syncthing"
echo "3. 🔄 重启 Syncthing 服务"
echo "4. 📊 查看服务状态"
echo "5. ❌ 退出"
echo "" 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() { install_syncthing() {
echo -e "${GREEN}🐱 开始安装 Syncthing...${NC}" log_info "开始安装 Syncthing..."
# 检测系统 # 检查是否已安装
if [ -f /etc/debian_version ]; then if check_installed; then
echo -e "${YELLOW}🍥 检测到 Debian/Ubuntu 系统${NC}" log_warning "Syncthing 已安装,如需重新安装请使用: $0 reinstall"
# 安装 Syncthing show_status
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}"
exit 1 exit 1
fi fi
echo -e "${YELLOW}🎯 配置 Syncthing 服务...${NC}" # 创建安装目录
# 创建服务配置 log_info "创建安装目录..."
cat > /etc/systemd/system/syncthing@root.service << 'EOF' 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] [Unit]
Description=Syncthing - Open Source Continuous File Synchronization for %i Description=Syncthing - Open Source Continuous File Synchronization
Documentation=man:syncthing(1) Documentation=man:syncthing(1)
After=network.target After=network.target
[Service] [Service]
User=%i Type=simple
ExecStart=/usr/bin/syncthing serve --no-browser --no-restart --logflags=0 --gui-address="0.0.0.0:8384" User=$USER
ExecStart=$BIN_PATH -no-browser -gui-address="0.0.0.0:8384" -no-restart
Restart=on-failure Restart=on-failure
RestartSec=5
SuccessExitStatus=3 4 SuccessExitStatus=3 4
RestartForceExitStatus=3 4 RestartForceExitStatus=3 4
@@ -62,142 +148,132 @@ RestartForceExitStatus=3 4
WantedBy=multi-user.target WantedBy=multi-user.target
EOF EOF
echo -e "${YELLOW}🔧 设置防火墙...${NC}" # 重新加载 systemd
# 开放端口(如果防火墙开启的话) sudo systemctl daemon-reload
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
echo -e "${YELLOW}📁 创建备份目录...${NC}" # 启动服务
mkdir -p /syncthing_backup log_info "启动 Syncthing 服务..."
echo -e "${GREEN}💾 备份目录已创建: /syncthing_backup${NC}" sudo systemctl enable syncthing
sudo systemctl start syncthing
echo -e "${YELLOW}🚀 启动服务...${NC}"
systemctl daemon-reload
systemctl enable syncthing@root
systemctl start syncthing@root
# 等待服务启动 # 等待服务启动
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() { 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 log_info "停止服务..."
systemctl disable syncthing@root 2>/dev/null sudo systemctl stop syncthing 2>/dev/null || true
sudo systemctl disable syncthing 2>/dev/null || true
# 删除服务文件 # 删除服务文件
rm -f /etc/systemd/system/syncthing@root.service log_info "删除服务文件..."
systemctl daemon-reload sudo rm -f $SERVICE_FILE
sudo systemctl daemon-reload
# 卸载软件包
if [ -f /etc/debian_version ]; then # 删除安装文件
apt remove -y syncthing log_info "删除安装文件..."
rm -f /etc/apt/sources.list.d/syncthing.list sudo rm -rf $INSTALL_DIR
rm -f /usr/share/keyrings/syncthing-archive-keyring.gpg
apt update # 删除符号链接
elif [ -f /etc/redhat-release ]; then log_info "删除符号链接..."
dnf remove -y syncthing 2>/dev/null || yum remove -y syncthing sudo rm -f $BIN_PATH
fi
# 询问是否删除配置
# 关闭防火墙端口 echo ""
if command -v ufw >/dev/null; then read -p "❓ 是否删除配置文件和数据? [y/N]: " -n 1 -r
ufw delete allow 8384/tcp 2>/dev/null echo
ufw delete allow 22000/tcp 2>/dev/null if [[ $REPLY =~ ^[Yy]$ ]]; then
ufw delete allow 21027/udp 2>/dev/null log_info "删除配置文件..."
elif command -v firewall-cmd >/dev/null; then rm -rf $CONFIG_DIR
firewall-cmd --permanent --remove-port=8384/tcp 2>/dev/null log_success "配置已删除"
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
echo -e "${YELLOW}📁 是否删除备份目录?${NC}"
read -p "💭 输入 'yes' 删除 /syncthing_backup 目录: " confirm
if [ "$confirm" = "yes" ]; then
rm -rf /syncthing_backup
echo -e "${GREEN}✅ 备份目录已删除${NC}"
else else
echo -e "${YELLOW}📁 备份目录保留在 /syncthing_backup${NC}" log_info "配置保留在: $CONFIG_DIR"
fi fi
echo -e "${GREEN}✅ Syncthing 卸载完成!${NC}" log_success "Syncthing 卸载完成!"
}
# 重新安装函数
reinstall_syncthing() {
log_info "开始重新安装 Syncthing..."
$0 uninstall
sleep 2
$0 install
} }
# 重启服务 # 重启服务
restart_service() { restart_service() {
echo -e "${YELLOW}🔄 重启 Syncthing 服务...${NC}" if check_installed; then
systemctl restart syncthing@root log_info "重启 Syncthing 服务..."
sleep 2 sudo systemctl restart syncthing
systemctl status syncthing@root --no-pager sleep 3
show_status
else
log_error "Syncthing 未安装,请先安装"
exit 1
fi
} }
# 查看状态 # 主程序
show_status() { case "${1:-}" in
echo -e "${YELLOW}📊 Syncthing 服务状态:${NC}" "install")
systemctl status syncthing@root --no-pager install_syncthing
echo "" ;;
echo -e "${YELLOW}🌐 端口监听状态:${NC}" "uninstall")
netstat -tlnp | grep 8384 || echo "📭 8384 端口未监听" uninstall_syncthing
netstat -tlnp | grep 22000 || echo "📭 22000 端口未监听" ;;
} "reinstall")
reinstall_syncthing
# 显示访问信息 ;;
show_access_info() { "status")
echo "" show_status
echo -e "${BLUE}========================================" ;;
echo "🐾 Syncthing 管理界面访问信息:" "restart")
echo "🌐 外网访问: http://$(curl -s ifconfig.me):8384/" restart_service
echo "💻 本地访问: http://localhost:8384/" ;;
echo "📁 备份目录: /syncthing_backup" "")
echo "🔧 服务管理: systemctl status/start/stop syncthing@root" # 无参数时自动判断
echo "========================================" if check_installed; then
echo -e "${NC}"
}
# 主循环
while true; do
show_menu
case $choice in
1)
install_syncthing
;;
2)
uninstall_syncthing
;;
3)
restart_service
;;
4)
show_status show_status
;; else
5) log_info "未检测到 Syncthing开始安装..."
echo -e "${GREEN}🐱 再见!祝你有个美好的一天!${NC}" install_syncthing
exit 0 fi
;; ;;
*) "help"|"-h"|"--help")
echo -e "${RED}❌ 无效选择,请重新输入${NC}" show_usage
;; ;;
esac *)
log_error "未知命令: $1"
echo "" show_usage
read -p "💕 按回车键继续..." exit 1
clear ;;
done esac