问题描述
syncthing,文件同步工具,我们使用该工具进行数据备份。
该笔记将记录:在 Linux 中,如何通过 syncthing 完成文件同步,及常见问题解决方案。
解决方案
补充说明
鉴于 syncthing 的功能较多及网络环境千差万别,我们这里记录的笔记并不适用于每种环境。建议阅读官方文档,以了解更多细节。
服务部署
第一步、服务安装
Syncthing/Debian/Ubuntu Packages
Ubuntu 自带 Version 1.1.4~ds1-4ubuntu1 版本,过旧,所以我们采用 syncthing 官方安装:
wget https://syncthing.net/release-key.gpg apt-key add ./release-key.gpg # Add the "stable" channel to your APT sources: echo "deb https://apt.syncthing.net/ syncthing stable" | sudo tee /etc/apt/sources.list.d/syncthing.list # Update and install syncthing: sudo apt-get update sudo apt-get install syncthing # syncthing --version syncthing v1.18.5 "Fermium Flea" (go1.17.3 linux-amd64) deb@build.syncthing.net 2021-11-22 09:11:00 UTC [noupgrade]
补充说明:
1)为了同步文件,需要在远程主机与本地主机都安装 syncthing 服务;
第二步、服务运行
syncthing/Starting Syncthing Automatically
鉴于 syncthing 不建议 ROOT 用户运行,所以我们遵循官方建议,以非 ROOT 用户运行:
# 本地启动服务 k4nz@localhost # systemctl --user enable syncthing.service k4nz@localhost # systemctl --user start syncthing.service # 远程启动服务 user@remotesvc # systemctl enable syncthing@user.service user@remotesvc # systemctl start syncthing@user.service
补充说明:
1)用户必须存在 $HOME 目录,否则 syncthing 无法写入配置文件,而导致启动失败;
2)通过两种 systemd 方式来启动 syncthing 服务;
syncthing 带有 Web 界面,我们配置 Nginx 反向代理来访问:
server { server_name syncthing.example.com; listen 80; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://127.0.0.1:8384/; proxy_read_timeout 600s; proxy_send_timeout 600s; } }
第三步、配置使用
参考 Syncthing/Getting Started 文档,以获取详细的配置过程。如下为大致配置过程:
对于远程服务,我们使用内置的 Web 管理界面 http://127.0.0.1:8384
对于桌面环境,我们使用 Syncthing-GTK 桌面程序
1)添加设备:远程和本地,都需要通过 Device ID 添加对方;
—- Top Right ⇒ Actions ⇒ Show ID
—- Right Column ⇒ Add Remote Device
2)文件同步:添加需要同步的目录;
—- 在本地添加需要同步的目录,此时远程将收到文件同步的提示;
—- 另外:远程设备可以自动发现;或者手动添加远程设备地址;
常用配置
忽略文件或目录:Ignoring Files — Syncthing v1.19.1-8-gaf93ba9 documentation
常见问题
[WIP] 同步目录消失(非数据丢失)
问题描述:在 syncthing 中,当添加需要同步的目录后,某天我们发现我们我们添加的目录消失了(注意,这并非数据目录消失,而是在 Web 界面中创建的同步目录消失)。
原因分析:我们推测是因为重启之后,systemd 管理的 syncthing 启动较快,而此时数据盘还没有挂载,导致 syncthing 无法找到 $HOME/.config/syncthing 配置。
解决方案:在 After= 中,添加 local-fs.target 目标
# systemctl edit syncthing@.service --full ... [Unit] ... After=network.target local-fs.target ...
参考文献
Syncthing/Getting Started
Syncthing/Reverse Proxy Setup
Syncthing – 免费开源替代 Resilio/BT Sync 的文件夹同步工具神器(搭建同步网盘)