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
# Syncthing 一键安装脚本
echo "========================================"
echo " Syncthing 一键安装脚本"
echo " Syncthing 修复安装脚本"
echo "========================================"
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# 修复系统依赖
echo "安装系统依赖..."
apt update
apt install -y gnupg2 curl wget
log() { echo -e "${GREEN}[✓]${NC} $1"; }
warn() { echo -e "${YELLOW}[!]${NC} $1"; }
error() { echo -e "${RED}[✗]${NC} $1"; }
# 清理之前的错误配置
echo "清理旧配置..."
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
# 检测系统
detect_os() {
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
VERSION=$VERSION_ID
else
error "无法检测操作系统"
exit 1
fi
}
# 方法1: 使用通用安装(推荐)
echo "使用通用安装方法..."
cd /tmp
LATEST_VERSION=$(curl -s https://api.github.com/repos/syncthing/syncthing/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
ARCH=$(uname -m)
# 安装 Syncthing
install_syncthing() {
log "检测到系统: $OS $VERSION"
case $OS in
ubuntu|debian)
install_debian ;;
centos|rhel|fedora|rocky|almalinux)
install_centos ;;
*)
install_generic ;;
esac
}
case $ARCH in
x86_64) ARCH="amd64" ;;
aarch64) ARCH="arm64" ;;
armv7l) ARCH="armv7" ;;
*) ARCH="amd64" ;;
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
}
echo "下载 Syncthing $LATEST_VERSION ($ARCH)"
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
cp syncthing-linux-${ARCH}-${LATEST_VERSION}/syncthing /usr/local/bin/
chmod +x /usr/local/bin/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
}
# 创建配置目录
echo "创建配置目录..."
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
# 通用安装方法
install_generic() {
log "使用通用安装方法"
# 下载最新版本
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
# 创建正确的服务文件
echo "创建系统服务..."
cat > /etc/systemd/system/syncthing.service << 'SERVICE'
[Unit]
Description=Syncthing
Documentation=man:syncthing(1)
@@ -124,7 +52,7 @@ After=network.target
[Service]
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
RestartSec=5
SuccessExitStatus=3 4
@@ -134,80 +62,41 @@ RestartForceExitStatus=3 4
WantedBy=multi-user.target
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() {
log "启动 Syncthing 服务"
sudo systemctl enable syncthing
sudo systemctl start syncthing
# 等待服务启动
sleep 3
if sudo systemctl is-active --quiet syncthing; then
log "Syncthing 服务启动成功"
else
error "Syncthing 服务启动失败"
sudo systemctl status syncthing
fi
}
echo "启动服务..."
systemctl daemon-reload
systemctl enable syncthing
systemctl start syncthing
# 显示安装信息
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 ""
}
sleep 3
# 主函数
main() {
detect_os
install_syncthing
create_service
setup_firewall
start_service
show_info
}
if systemctl is-active --quiet syncthing; then
echo "✓ Syncthing 服务启动成功"
else
echo "查看服务状态..."
systemctl status syncthing -l --no-pager
echo "尝试手动启动..."
sudo -u syncthing /usr/local/bin/syncthing -generate="/var/lib/syncthing/config"
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
chmod +x install-syncthing.sh
./install-syncthing.sh
chmod +x fix-syncthing-install.sh
./fix-syncthing-install.sh