Update 试验用脚本

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

View File

@@ -1,122 +1,50 @@
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 LATEST_VERSION=$(curl -s https://api.github.com/repos/syncthing/syncthing/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
OS=$ID ARCH=$(uname -m)
VERSION=$VERSION_ID
else
error "无法检测操作系统"
exit 1
fi
}
# 安装 Syncthing case $ARCH in
install_syncthing() { x86_64) ARCH="amd64" ;;
log "检测到系统: $OS $VERSION" aarch64) ARCH="arm64" ;;
armv7l) ARCH="armv7" ;;
case $OS in *) ARCH="amd64" ;;
ubuntu|debian) esac
install_debian ;;
centos|rhel|fedora|rocky|almalinux)
install_centos ;;
*)
install_generic ;;
esac
}
# Debian/Ubuntu 安装 echo "下载 Syncthing $LATEST_VERSION ($ARCH)"
install_debian() { wget -q https://github.com/syncthing/syncthing/releases/download/${LATEST_VERSION}/syncthing-linux-${ARCH}-${LATEST_VERSION}.tar.gz
log "安装 Syncthing (Debian/Ubuntu)" tar -xzf syncthing-linux-${ARCH}-${LATEST_VERSION}.tar.gz
cp syncthing-linux-${ARCH}-${LATEST_VERSION}/syncthing /usr/local/bin/
# 添加 Syncthing 官方仓库 chmod +x /usr/local/bin/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() { echo "创建配置目录..."
log "安装 Syncthing (CentOS/RHEL)" useradd -r -s /bin/false -d /var/lib/syncthing -m syncthing 2>/dev/null || true
mkdir -p /var/lib/syncthing/config
# 添加 Syncthing 官方仓库 chown -R syncthing:syncthing /var/lib/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() { echo "创建系统服务..."
log "使用通用安装方法" cat > /etc/systemd/system/syncthing.service << 'SERVICE'
# 下载最新版本
LATEST_VERSION=$(curl -s https://api.github.com/repos/syncthing/syncthing/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
ARCH=$(uname -m)
case $ARCH in
x86_64) ARCH="amd64" ;;
aarch64) ARCH="arm64" ;;
armv7l) ARCH="armv7" ;;
*) ARCH="amd64" ;;
esac
log "下载 Syncthing $LATEST_VERSION ($ARCH)"
# 下载并解压
cd /tmp
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
sudo cp syncthing-linux-${ARCH}-${LATEST_VERSION}/syncthing /usr/local/bin/
sudo chmod +x /usr/local/bin/syncthing
}
# 创建系统服务
create_service() {
log "创建系统服务"
# 创建用户和目录
if ! id syncthing &>/dev/null; then
sudo useradd -r -s /bin/false -d /var/lib/syncthing -m syncthing
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
if sudo systemctl is-active --quiet syncthing; then
log "Syncthing 服务启动成功"
else
error "Syncthing 服务启动失败"
sudo systemctl status syncthing
fi
}
# 显示安装信息 sleep 3
show_info() {
echo ""
echo "========================================"
echo " Syncthing 安装完成!"
echo "========================================"
echo ""
echo "访问地址: https://$(hostname -I | awk '{print $1}'):8384"
echo ""
echo "管理命令:"
echo " sudo systemctl start syncthing # 启动"
echo " sudo systemctl stop syncthing # 停止"
echo " sudo systemctl restart syncthing # 重启"
echo " sudo systemctl status syncthing # 状态"
echo ""
echo "默认端口:"
echo " - Web 界面: 8384/tcp"
echo " - 文件传输: 22000/tcp"
echo " - 设备发现: 21027/udp"
echo ""
warn "首次访问需要设置管理员密码"
echo ""
}
# 主函数 if systemctl is-active --quiet syncthing; then
main() { echo "✓ Syncthing 服务启动成功"
detect_os else
install_syncthing echo "查看服务状态..."
create_service systemctl status syncthing -l --no-pager
setup_firewall echo "尝试手动启动..."
start_service sudo -u syncthing /usr/local/bin/syncthing -generate="/var/lib/syncthing/config"
show_info systemctl start syncthing
} fi
# 运行安装 # 显示信息
main echo ""
echo "========================================"
echo " Syncthing 安装完成!"
echo "========================================"
IP=$(hostname -I | awk '{print $1}')
echo "访问地址: https://$IP:8384"
echo ""
echo "管理命令:"
echo " systemctl start syncthing"
echo " systemctl stop syncthing"
echo " systemctl restart syncthing"
echo " systemctl status syncthing"
echo ""
echo "查看运行状态:"
systemctl status syncthing --no-pager -l
EOF EOF
chmod +x install-syncthing.sh chmod +x fix-syncthing-install.sh
./install-syncthing.sh ./fix-syncthing-install.sh