开源文件同步工具Syncthing搭建

Syncthing

Syncthing is a continuous file synchronization program. It synchronizes files between two or more computers in real time, safely protected from prying eyes. Your data is your data alone and you deserve to choose where it is stored, whether it is shared with some third party, and how it’s transmitted over the internet.

搭建环境及准备

  • ✅ Debian 12.6
  • ✅ Docker version:27.1.2
  • ✅ Docker compose

安装

安装Syncthing服务器端

1#下载Syncthing服务端
2wget https://github.com/syncthing/syncthing/releases/download/v1.27.13-rc.1/syncthing-linux-amd64-v1.27.13-rc.1.tar.gz
3#解压下载好的Syncthing服务端
4tar xzvf syncthing-linux-amd64-v1.27.13-rc.1.tar.gz
5#进入解压好的目录
6cd syncthing-linux-amd64-v1.27.12/
7#将Syncthing复制到/usr/local/bin目录中,之后就可以直接使用syncthing命令启动服务端
8cp syncthing /usr/local/bin

然后直接输入syncthing命令即可启动syncthing服务端

1syncthing

通过地址127.0.0.1:port(默认8384)即可访问Syncthing WEB GUI

(可选)修改Syncthing的配置(如端口号等)

Syncthing一般会在当前用户目录生成配置文件,比如我在root用户下

1nano /root/.local/state/syncthing/config.xml
(可选)使用systemd的方式启动Syncthing

创建systemd的配置文件

1sudo nano  /etc/systemd/system/syncthing.service

systemd的配置文件如下:

 1[Unit]
 2Description=Syncthing - Open Source Continuous File Synchronization for %I
 3Documentation=https://docs.syncthing.net/
 4After=network.target
 5
 6[Service]
 7User=root #root用户启动
 8ExecStart=/usr/local/bin/syncthing -no-browser -home=/home/null/.local/state/syncthing
 9Restart=on-failure
10SuccessExitStatus=3 4
11
12[Install]
13WantedBy=default.target

ctrl+o退出,ctrl+x保存 systemd启动Syncthing

1systemctl daemon-reload
2systemctl enable syncthing
3systemctl start syncthing
(可选)为Syncthing WEB GUI配置Nginx反向代理及TLS证书
配置Nginx反向代理

编辑 Nginx 站点配置文件

1#'syncthing'这个是自定义名字,因为搭建的是syncthing故取名为这个
2nano /etc/nginx/sites-available/syncthing

Nginx 站点配置文件如下:

1server {
2        listen 80;
3        server_name example1.com;
4        location / {
5        proxy_pass http://127.0.0.1:8384; #'8384'为WEB GUI的默认监听端口
6       }
7     }

ctrl+o退出,ctrl+x保存
创建符号链接启用 Nginx 站点配置文件

1sudo ln -s /etc/nginx/sites-available/example1.com /etc/nginx/sites-enabled/
2#测试 Nginx 配置是否正确
3sudo nginx -t
4#重新加载 Nginx 配置
5sudo systemctl reload nginx
使用Certbot配置TLS证书
1#安装 Certbot
2sudo apt install certbot python3-certbot-nginx
3#申请证书
4sudo certbot --nginx -d your_domain_or_ip

当提示successful时,重新刷新页面看到域名左方出现🔒即TLS配置成功

Docker compose部署Syncthing发现和中继服务器

1cd syncthing-linux-amd64-v1.27.12/
2#创建docker compose配置
3nano docker-compose.yaml

docker compose配置如下:

 1services:
 2   syncthing_discovery_server: # 发现服务器
 3    image: syncthing/discosrv
 4    container_name: syncthing-discovery-server
 5    command: -debug -listen=":8443" 
 6    environment:
 7      - PUID=1000
 8      - PGID=1000
 9    volumes:
10      - ./syncthing/discosrv:/var/stdiscosrv
11    ports:
12      - 8443:8443 # Listen address (default “:8443”)
13    restart: always
14
15   syncthing_relay_server: # 中继服务器
16    image: syncthing/relaysrv:latest
17    container_name: syncthing-relay-server
18    command: -debug -pools="" -listen=":22067"
19    environment:
20      - PUID=1000
21      - PGID=1000
22    volumes:
23      - ./syncthing/strelaysrv:/var/strelaysrv
24    ports:
25      - 22067:22067  # 中继服务器的数据连接端口(必须开启)
26      #- 22070:22070  # 用于公用的中继服务器池,显示数据传输、客户端数量等状态,可不开启
27    restart: always

ctrl+o退出,ctrl+x保存
拉取镜像

1docker compose pull

启动 Docker Compose 项目并在后台运行它们

1docker compose up -d

获取Syncthing发现和中继服务的 Server device ID

1# 查看syncthing-discovery-server日志获取Server device ID
2docker logs syncthing-discovery-server

Server device ID is后面便是Server device ID(发现和中继服务ID是相同的)

1stdiscosrv v1.27.12 "Gold Grasshopper" (go1.22.6 linux-amd64) [email protected] 2024-09-06 07:15:45 UTC [noupgrade, purego]
2Server device ID is RUA24CQ-SVNBBBJ-3UKXRJZ-11111-111111-JH7YJNQ-S4KCO4W-YHYIPQL

为发现和中继服务器配置反向代理及TLS证书

Nginx 站点配置文件如下:

1server {
2        listen 80;
3        server_name example2.com;
4        location / {
5        proxy_pass http://127.0.0.1:8443;
6       }
7      }

创建符号链接和TLS证书配置可模仿上文重复操作

Syncthing 中配置发现和中继服务器

打开Syncthing WEB页面,点击 ⚙操作->⚙设置->连接

1# 协议监听地址,中继服务URI
2relay://公网IP:22067?id=中继服务器device ID
3
4# 全局发现服务器
5https://公网IP:8443/?id=发现服务器device ID

参考

使用 Hugo 构建
主题 StackJimmy 设计