Files
dock/Syncthing 交互式管理脚本

437 lines
11 KiB
Bash
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m'
# 输出函数
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}"; }
log_menu() { echo -e "${PURPLE}$1${NC}"; }
log_step() { echo -e "${CYAN}📌 $1${NC}"; }
# 显示炫酷标题
show_header() {
clear
echo -e "${PURPLE}"
echo "╔══════════════════════════════════════════════╗"
echo "║ Syncthing 交互式管理脚本 ║"
echo "║ 直接安装 · 极简使用 ║"
echo "╚══════════════════════════════════════════════╝"
echo -e "${NC}"
}
# 显示状态信息
show_status() {
log_step "系统状态检查..."
# 检查安装状态
if [ -f "$BIN_PATH" ]; then
log_success "Syncthing: 已安装"
echo " 📁 安装路径: $INSTALL_DIR"
echo " ⚙️ 配置路径: $CONFIG_DIR"
else
log_warning "Syncthing: 未安装"
fi
# 检查服务状态
if systemctl is-active --quiet syncthing 2>/dev/null; then
log_success "服务状态: 运行中"
LOCAL_IP=$(hostname -I | awk '{print $1}')
echo " 🌐 访问地址: http://${LOCAL_IP}:8384"
elif systemctl is-enabled syncthing 2>/dev/null; then
log_warning "服务状态: 已停止"
else
log_warning "服务状态: 未配置"
fi
# 检查端口
if ss -tuln | grep -q ":8384 "; then
log_success "网络端口: 8384 正在监听"
else
log_warning "网络端口: 8384 未监听"
fi
echo ""
}
# 等待用户按键
press_any_key() {
echo -e "${YELLOW}"
read -n 1 -s -r -p "🔸 按任意键继续..."
echo -e "${NC}"
echo ""
}
# 安装 Syncthing
install_syncthing() {
show_header
log_menu "开始安装 Syncthing"
# 检查是否已安装
if [ -f "$BIN_PATH" ]; then
log_warning "检测到 Syncthing 已安装!"
echo ""
echo "请选择:"
echo " 1) 重新安装(覆盖现有版本)"
echo " 2) 返回主菜单"
echo " 3) 查看当前状态"
read -p "请输入选择 [1-3]: " choice
case $choice in
1)
log_info "开始重新安装..."
;;
2)
return
;;
3)
show_status
press_any_key
return
;;
*)
log_error "无效选择"
press_any_key
return
;;
esac
fi
log_step "步骤 1/5 - 创建安装目录..."
sudo mkdir -p $INSTALL_DIR
sudo chown $USER:$USER $INSTALL_DIR
log_step "步骤 2/5 - 下载 Syncthing $SYNCTHING_VERSION..."
cd /tmp
if wget -q --show-progress https://github.com/syncthing/syncthing/releases/download/$SYNCTHING_VERSION/syncthing-linux-amd64-$SYNCTHING_VERSION.tar.gz; then
log_success "下载完成"
else
log_error "下载失败,请检查网络连接"
press_any_key
return
fi
log_step "步骤 3/5 - 解压和安装..."
tar xzf syncthing-linux-amd64-$SYNCTHING_VERSION.tar.gz
cd syncthing-linux-amd64-$SYNCTHING_VERSION
sudo cp syncthing $INSTALL_DIR/
sudo chmod +x $INSTALL_DIR/syncthing
sudo ln -sf $INSTALL_DIR/syncthing $BIN_PATH
log_step "步骤 4/5 - 创建系统服务..."
mkdir -p $CONFIG_DIR
sudo tee $SERVICE_FILE > /dev/null <<EOF
[Unit]
Description=Syncthing - Open Source Continuous File Synchronization
After=network.target
[Service]
Type=simple
User=$USER
ExecStart=$BIN_PATH -no-browser -gui-address="0.0.0.0:8384" -no-restart
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
log_step "步骤 5/5 - 启动服务..."
sudo systemctl enable syncthing
sudo systemctl start syncthing
# 等待启动
log_info "等待服务启动..."
for i in {1..10}; do
if systemctl is-active --quiet syncthing; then
break
fi
sleep 1
echo -n "."
done
echo ""
if systemctl is-active --quiet syncthing; then
log_success "🎉 安装完成!"
LOCAL_IP=$(hostname -I | awk '{print $1}')
echo ""
echo "✨ 访问信息:"
echo " 🌐 管理界面: http://${LOCAL_IP}:8384"
echo " 📍 直接使用IP访问无需复杂配置"
echo ""
echo "📋 下一步建议:"
echo " 1. 打开浏览器访问上述地址"
echo " 2. 添加需要同步的文件夹"
echo " 3. 配置其他设备进行同步"
else
log_error "服务启动失败"
log_info "请运行: sudo systemctl status syncthing 查看详情"
fi
# 清理
rm -rf /tmp/syncthing-linux-amd64*
press_any_key
}
# 卸载 Syncthing
uninstall_syncthing() {
show_header
log_menu "卸载 Syncthing"
if [ ! -f "$BIN_PATH" ]; then
log_warning "未找到 Syncthing 安装文件,可能未安装"
press_any_key
return
fi
log_warning "⚠️ 警告:此操作将卸载 Syncthing"
echo ""
echo "将删除以下内容:"
echo " ✅ 程序文件: $INSTALL_DIR/"
echo " ✅ 系统服务: syncthing.service"
echo " ✅ 启动链接: $BIN_PATH"
echo ""
read -p "❓ 是否继续卸载?[y/N]: " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log_info "取消卸载"
press_any_key
return
fi
log_step "停止服务..."
sudo systemctl stop syncthing 2>/dev/null || true
sudo systemctl disable syncthing 2>/dev/null || true
log_step "删除服务文件..."
sudo rm -f $SERVICE_FILE
sudo systemctl daemon-reload
log_step "删除程序文件..."
sudo rm -rf $INSTALL_DIR
sudo rm -f $BIN_PATH
echo ""
read -p "❓ 是否删除配置文件?[y/N]: " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
log_step "删除配置文件..."
rm -rf $CONFIG_DIR
log_success "配置已删除"
else
log_info "配置文件保留在: $CONFIG_DIR"
fi
log_success "卸载完成!"
press_any_key
}
# 服务管理
manage_service() {
show_header
log_menu "服务管理"
if [ ! -f "$BIN_PATH" ]; then
log_error "Syncthing 未安装,请先安装"
press_any_key
return
fi
show_status
echo "请选择操作:"
echo " 1) 启动服务"
echo " 2) 停止服务"
echo " 3) 重启服务"
echo " 4) 查看服务状态"
echo " 5) 查看实时日志"
echo " 6) 返回主菜单"
read -p "请输入选择 [1-6]: " choice
case $choice in
1)
log_step "启动服务..."
sudo systemctl start syncthing
sleep 2
if systemctl is-active --quiet syncthing; then
log_success "服务启动成功"
else
log_error "服务启动失败"
fi
;;
2)
log_step "停止服务..."
sudo systemctl stop syncthing
log_success "服务已停止"
;;
3)
log_step "重启服务..."
sudo systemctl restart syncthing
sleep 2
if systemctl is-active --quiet syncthing; then
log_success "服务重启成功"
else
log_error "服务重启失败"
fi
;;
4)
log_step "服务状态:"
sudo systemctl status syncthing
;;
5)
log_step "开始显示实时日志Ctrl+C 退出)..."
sudo journalctl -u syncthing -f
;;
6)
return
;;
*)
log_error "无效选择"
;;
esac
press_any_key
}
# 快速访问
quick_access() {
show_header
log_menu "快速访问"
if ! systemctl is-active --quiet syncthing 2>/dev/null; then
log_error "服务未运行,请先启动服务"
press_any_key
return
fi
LOCAL_IP=$(hostname -I | awk '{print $1}')
log_success "访问信息:"
echo " 🌐 本地访问: http://127.0.0.1:8384"
echo " 🌐 网络访问: http://${LOCAL_IP}:8384"
echo " 📍 端口: 8384"
echo ""
echo "快捷操作:"
echo " 1) 用默认浏览器打开"
echo " 2) 复制访问地址到剪贴板"
echo " 3) 显示二维码(如果有)"
echo " 4) 返回主菜单"
read -p "请输入选择 [1-4]: " choice
case $choice in
1)
if command -v xdg-open >/dev/null; then
xdg-open "http://${LOCAL_IP}:8384"
log_success "已尝试打开浏览器"
else
log_info "请在浏览器中访问: http://${LOCAL_IP}:8384"
fi
;;
2)
if command -v xclip >/dev/null; then
echo "http://${LOCAL_IP}:8384" | xclip -selection clipboard
log_success "地址已复制到剪贴板"
else
log_info "请手动复制: http://${LOCAL_IP}:8384"
fi
;;
3)
log_info "二维码功能需要额外工具支持"
;;
4)
return
;;
*)
log_error "无效选择"
;;
esac
press_any_key
}
# 显示主菜单
show_main_menu() {
show_header
show_status
log_menu "主菜单"
echo "请选择操作:"
echo " 1) 安装 Syncthing"
echo " 2) 卸载 Syncthing"
echo " 3) 服务管理(启动/停止/重启)"
echo " 4) 快速访问"
echo " 5) 查看详细状态"
echo " 6) 退出脚本"
echo ""
}
# 主循环
main() {
while true; do
show_main_menu
read -p "请输入选择 [1-6]: " choice
case $choice in
1)
install_syncthing
;;
2)
uninstall_syncthing
;;
3)
manage_service
;;
4)
quick_access
;;
5)
show_header
show_status
press_any_key
;;
6)
log_success "再见!"
exit 0
;;
*)
log_error "无效选择,请输入 1-6 之间的数字"
sleep 2
;;
esac
done
}
# 脚本启动
if [[ $1 == "-y" || $1 == "--auto" ]]; then
# 非交互模式
if [ ! -f "$BIN_PATH" ]; then
install_syncthing
else
show_status
fi
else
# 交互模式
main
fi