Update 试验用脚本

This commit is contained in:
2025-11-03 00:33:16 +08:00
committed by GitHub
parent c25a3ee851
commit 728330b083

View File

@@ -1,88 +1,25 @@
cat > install-syncthing.sh << 'EOF' cat > fix-syncthing-install.sh << 'EOF'
#!/bin/bash #!/bin/bash
# Syncthing 一键安装脚本
echo "========================================" echo "========================================"
echo " Syncthing 一键安装脚本" echo " Syncthing 修复安装脚本"
echo "========================================" echo "========================================"
# 颜色定义 # 修复系统依赖
RED='\033[0;31m' echo "安装系统依赖..."
GREEN='\033[0;32m' apt update
YELLOW='\033[1;33m' apt install -y gnupg2 curl wget
NC='\033[0m'
log() { echo -e "${GREEN}[✓]${NC} $1"; } # 清理之前的错误配置
warn() { echo -e "${YELLOW}[!]${NC} $1"; } echo "清理旧配置..."
error() { echo -e "${RED}[✗]${NC} $1"; } systemctl stop syncthing 2>/dev/null
systemctl disable syncthing 2>/dev/null
rm -f /etc/systemd/system/syncthing.service
rm -f /etc/apt/sources.list.d/syncthing.list
# 检测系统 # 方法1: 使用通用安装(推荐)
detect_os() { echo "使用通用安装方法..."
if [ -f /etc/os-release ]; then cd /tmp
. /etc/os-release
OS=$ID
VERSION=$VERSION_ID
else
error "无法检测操作系统"
exit 1
fi
}
# 安装 Syncthing
install_syncthing() {
log "检测到系统: $OS $VERSION"
case $OS in
ubuntu|debian)
install_debian ;;
centos|rhel|fedora|rocky|almalinux)
install_centos ;;
*)
install_generic ;;
esac
}
# Debian/Ubuntu 安装
install_debian() {
log "安装 Syncthing (Debian/Ubuntu)"
# 添加 Syncthing 官方仓库
curl -s https://syncthing.net/release-key.txt | sudo apt-key add -
echo "deb https://apt.syncthing.net/ syncthing stable" | sudo tee /etc/apt/sources.list.d/syncthing.list
# 更新并安装
sudo apt update
sudo apt install -y syncthing
}
# CentOS/RHEL 安装
install_centos() {
log "安装 Syncthing (CentOS/RHEL)"
# 添加 Syncthing 官方仓库
cat > /etc/yum.repos.d/syncthing.repo << REPO
[syncthing]
name=Syncthing
baseurl=https://syncthing.net/repo/redhat
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://syncthing.net/release-key.txt
REPO
# 安装
if command -v dnf &> /dev/null; then
sudo dnf install -y syncthing
else
sudo yum install -y syncthing
fi
}
# 通用安装方法
install_generic() {
log "使用通用安装方法"
# 下载最新版本
LATEST_VERSION=$(curl -s https://api.github.com/repos/syncthing/syncthing/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') LATEST_VERSION=$(curl -s https://api.github.com/repos/syncthing/syncthing/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
ARCH=$(uname -m) ARCH=$(uname -m)
@@ -93,30 +30,21 @@ install_generic() {
*) ARCH="amd64" ;; *) ARCH="amd64" ;;
esac esac
log "下载 Syncthing $LATEST_VERSION ($ARCH)" echo "下载 Syncthing $LATEST_VERSION ($ARCH)"
# 下载并解压
cd /tmp
wget -q https://github.com/syncthing/syncthing/releases/download/${LATEST_VERSION}/syncthing-linux-${ARCH}-${LATEST_VERSION}.tar.gz wget -q https://github.com/syncthing/syncthing/releases/download/${LATEST_VERSION}/syncthing-linux-${ARCH}-${LATEST_VERSION}.tar.gz
tar -xzf syncthing-linux-${ARCH}-${LATEST_VERSION}.tar.gz tar -xzf syncthing-linux-${ARCH}-${LATEST_VERSION}.tar.gz
sudo cp syncthing-linux-${ARCH}-${LATEST_VERSION}/syncthing /usr/local/bin/ cp syncthing-linux-${ARCH}-${LATEST_VERSION}/syncthing /usr/local/bin/
sudo chmod +x /usr/local/bin/syncthing chmod +x /usr/local/bin/syncthing
}
# 创建系统服务 # 创建配置目录
create_service() { echo "创建配置目录..."
log "创建系统服务" useradd -r -s /bin/false -d /var/lib/syncthing -m syncthing 2>/dev/null || true
mkdir -p /var/lib/syncthing/config
chown -R syncthing:syncthing /var/lib/syncthing
# 创建用户和目录 # 创建正确的服务文件
if ! id syncthing &>/dev/null; then echo "创建系统服务..."
sudo useradd -r -s /bin/false -d /var/lib/syncthing -m syncthing cat > /etc/systemd/system/syncthing.service << 'SERVICE'
fi
sudo mkdir -p /var/lib/syncthing/{config,sync}
sudo chown -R syncthing:syncthing /var/lib/syncthing
# 创建 systemd 服务
cat > /tmp/syncthing.service << SERVICE
[Unit] [Unit]
Description=Syncthing Description=Syncthing
Documentation=man:syncthing(1) Documentation=man:syncthing(1)
@@ -124,7 +52,7 @@ After=network.target
[Service] [Service]
User=syncthing User=syncthing
ExecStart=/usr/bin/syncthing serve --no-browser --no-restart --home=/var/lib/syncthing/config ExecStart=/usr/local/bin/syncthing serve --no-browser --no-restart --home=/var/lib/syncthing/config
Restart=on-failure Restart=on-failure
RestartSec=5 RestartSec=5
SuccessExitStatus=3 4 SuccessExitStatus=3 4
@@ -134,80 +62,41 @@ RestartForceExitStatus=3 4
WantedBy=multi-user.target WantedBy=multi-user.target
SERVICE SERVICE
sudo mv /tmp/syncthing.service /etc/systemd/system/
sudo systemctl daemon-reload
}
# 配置防火墙
setup_firewall() {
if command -v ufw &> /dev/null; then
log "配置 UFW 防火墙"
sudo ufw allow 8384/tcp comment "Syncthing Web UI"
sudo ufw allow 22000/tcp comment "Syncthing Transfer"
sudo ufw allow 21027/udp comment "Syncthing Discovery"
elif command -v firewall-cmd &> /dev/null; then
log "配置 FirewallD"
sudo firewall-cmd --permanent --add-port=8384/tcp
sudo firewall-cmd --permanent --add-port=22000/tcp
sudo firewall-cmd --permanent --add-port=21027/udp
sudo firewall-cmd --reload
fi
}
# 启动服务 # 启动服务
start_service() { echo "启动服务..."
log "启动 Syncthing 服务" systemctl daemon-reload
sudo systemctl enable syncthing systemctl enable syncthing
sudo systemctl start syncthing systemctl start syncthing
# 等待服务启动
sleep 3 sleep 3
if sudo systemctl is-active --quiet syncthing; then if systemctl is-active --quiet syncthing; then
log "Syncthing 服务启动成功" echo "✓ Syncthing 服务启动成功"
else else
error "Syncthing 服务启动失败" echo "查看服务状态..."
sudo systemctl status syncthing systemctl status syncthing -l --no-pager
echo "尝试手动启动..."
sudo -u syncthing /usr/local/bin/syncthing -generate="/var/lib/syncthing/config"
systemctl start syncthing
fi fi
}
# 显示安装信息 # 显示信息
show_info() {
echo "" echo ""
echo "========================================" echo "========================================"
echo " Syncthing 安装完成!" echo " Syncthing 安装完成!"
echo "========================================" echo "========================================"
echo "" IP=$(hostname -I | awk '{print $1}')
echo "访问地址: https://$(hostname -I | awk '{print $1}'):8384" echo "访问地址: https://$IP:8384"
echo "" echo ""
echo "管理命令:" echo "管理命令:"
echo " sudo systemctl start syncthing # 启动" echo " systemctl start syncthing"
echo " sudo systemctl stop syncthing # 停止" echo " systemctl stop syncthing"
echo " sudo systemctl restart syncthing # 重启" echo " systemctl restart syncthing"
echo " sudo systemctl status syncthing # 状态" echo " systemctl status syncthing"
echo "" echo ""
echo "默认端口:" echo "查看运行状态:"
echo " - Web 界面: 8384/tcp" systemctl status syncthing --no-pager -l
echo " - 文件传输: 22000/tcp"
echo " - 设备发现: 21027/udp"
echo ""
warn "首次访问需要设置管理员密码"
echo ""
}
# 主函数
main() {
detect_os
install_syncthing
create_service
setup_firewall
start_service
show_info
}
# 运行安装
main
EOF EOF
chmod +x install-syncthing.sh chmod +x fix-syncthing-install.sh
./install-syncthing.sh ./fix-syncthing-install.sh