用SyncThing進行文件同步和存儲分發(fā)
1. 前言
當我們有文件同步或者存儲的需求時,又不想采用云盤,那么p2p的文件同步工具就是個不錯的選擇,比上傳到云盤再下載的過程更加有效率,直接點對點地同步內(nèi)容。 做的不錯的有商業(yè)化的Resilio Sync。而Syncthing是 Resilio Sync / BT Sync 的開源替代品。
Syncthing 最大的特色是采用了與 Resilio Sync (BitTorrent Sync) 類似的 P2P 分布式技術,無需中心服務器,即可讓多臺設備互相實時同步文件。
2. 安裝
2.1. ubuntu版本安裝
Debian/Ubuntu版本參考自:
https://apt.syncthing.net/
其他linux或windows版本可以參考:
https://docs.syncthing.net/users/contrib.html#contributions
執(zhí)行下列命令
# Add the release PGP keys:
curl -s https://syncthing.net/release-key.txt | sudo apt-key add -
# 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
如果遇到"The method driver /usr/lib/apt/methods/https could not be found."的提示,則
安裝apt-transport-https
sudo apt-get install apt-transport-https
2.2. 設置系統(tǒng)自啟動
https://docs.syncthing.net/users/autostart.html
對于采用 systemd 的操作系統(tǒng)可以創(chuàng)建兩種類型的服務:
user service:用戶級服務,只有在用戶登錄系統(tǒng)后 syncthing 才會啟動。
system service:系統(tǒng)級服務,syncthing 隨系統(tǒng)啟動而啟動,而不論用戶是否已經(jīng)登錄系統(tǒng)。
下面步驟是加入系統(tǒng)級服務中。
2.2.1. 創(chuàng)建運行syncthing的用戶,例如myuser.
useradd myuser -d /home/myuser -m
2.2.2. 找到 service文件并復制到system目錄
find / -name syncthing@.service
/lib/systemd/system/syncthing@.service
cp /lib/systemd/system/syncthing@.service /etc/systemd/system/
2.2.3. systemctl 執(zhí)行啟動
修改為自己期望運行syncthing的用戶。
Enable and start the service. Replace “myuser” with the actual Syncthing user after the @:
systemctl enable syncthing@myuser.service
systemctl start syncthing@myuser.service
檢查:
systemctl status syncthing@myuser.service
也可以直接使用root
● syncthing@root.service - Syncthing - Open Source Continuous File Synchronization for root
Loaded: loaded (/etc/systemd/system/syncthing@.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2018-02-08 17:24:42 CST; 5min ago
Docs: man:syncthing(1)
Main PID: 7941 (syncthing)
CGroup: /system.slice/system-syncthing.slice/syncthing@root.service
└─7941 /usr/bin/syncthing -no-browser -no-restart -logflags=0
Feb 08 17:24:44 iZ2ze55t9zj53mddhdt8c9Z syncthing[7941]: [KV4LG] INFO: Device KV4LG6S-YRZDVBA-RIPDLE7-6VF2W6X-TSVS4WP-4F2MBSD-QB6YKHW-N3UUSQK is "iZ2ze55t9zj53mddhdt8c9Z" at [dynamic]
Feb 08 17:24:44 iZ2ze55t9zj53mddhdt8c9Z syncthing[7941]: [KV4LG] INFO: Loading HTTPS certificate: open /root/.config/syncthing/https-cert.pem: no such file or directory
Feb 08 17:24:44 iZ2ze55t9zj53mddhdt8c9Z syncthing[7941]: [KV4LG] INFO: Creating new HTTPS certificate
Feb 08 17:24:44 iZ2ze55t9zj53mddhdt8c9Z syncthing[7941]: [KV4LG] WARNING: Syncthing should not run as a privileged or system user. Please consider using a normal user account.
Feb 08 17:24:44 iZ2ze55t9zj53mddhdt8c9Z syncthing[7941]: [KV4LG] INFO: GUI and API listening on 127.0.0.1:8384
Feb 08 17:24:44 iZ2ze55t9zj53mddhdt8c9Z syncthing[7941]: [KV4LG] INFO: Access the GUI via the following URL: http://127.0.0.1:8384/
Feb 08 17:24:46 iZ2ze55t9zj53mddhdt8c9Z syncthing[7941]: [KV4LG] INFO: kcp://0.0.0.0:22020 detected NAT type: Full cone NAT
Feb 08 17:24:46 iZ2ze55t9zj53mddhdt8c9Z syncthing[7941]: [KV4LG] INFO: kcp://0.0.0.0:22020 resolved external address kcp://39.106.199.67:22020 (via stun.voipbuster.com:3478)
Feb 08 17:24:54 iZ2ze55t9zj53mddhdt8c9Z syncthing[7941]: [KV4LG] INFO: Detected 0 NAT devices
Feb 08 17:25:28 iZ2ze55t9zj53mddhdt8c9Z syncthing[7941]: [KV4LG] INFO: Joined relay relay://106.185.55.60:22067
2.3. 修改Syncthing配置文件以便外網(wǎng)可以訪問
vim ~/.config/syncthing/config.xml
找到如下一行
<address>127.0.0.1:8384</address>
然后把IP127.0.0.1修改成0.0.0.0即可保存退出:
systemctl restart syncthing@root.service
3. 防火墻相關設置
參考和翻譯自官方文檔:
https://docs.syncthing.net/users/firewall.html#firewall-setup
3.1. 端口轉(zhuǎn)發(fā)設置
在計算機網(wǎng)絡中,端口轉(zhuǎn)發(fā)(Port Forwards)或端口映射(Port Mapping)是網(wǎng)絡地址轉(zhuǎn)換(NAT)的一種應用,在數(shù)據(jù)包穿越網(wǎng)絡網(wǎng)關(如路由器或防火墻)時將通信請求從一個地址和端口號組合重定向到另一個地址。此技術最常用于通過將通信的目標IP地址和端口號重新映射到網(wǎng)關對端(外部網(wǎng)絡)上的主機,使位于受保護的內(nèi)部主機網(wǎng)絡上的主機上的服務可用。
如果你有一個支持UPnP的NAT路由器,一個讓端口轉(zhuǎn)發(fā)工作的最簡單方法是確保在Syncthing和路由器上都啟用了UPnP設置,Syncthing將嘗試處理剩下的事情。如果成功,您將在控制臺中看到一條消息:
Created UPnP port mapping for external port XXXXX on UPnP device YYYYY.
如果這不可能或不可行,則應該為端口 22000 / TCP設置端口轉(zhuǎn)發(fā),或者在“ Sync Protocol Listen Address ”設置中設置自己定義的端口。外部轉(zhuǎn)發(fā)端口和內(nèi)部目標端口必須相同(即22000 / TCP)。
Syncthing中的交流是雙向的。因此,如果您為一個設備設置端口轉(zhuǎn)發(fā),即使它們位于NAT網(wǎng)絡或防火墻之后,其他設備也可以連接到該設備。
在沒有端口轉(zhuǎn)發(fā)的情況下,中繼(Relay)可能工作得很好,以使設備連接和同步,但與直接連接相比性能不佳。
3.2. 防火墻設置
如果PC機本身有一個防火墻,則需要打開以下用于傳入和傳出通信的端口:
Port 22000/TCP (or the actual listening port if you have changed the Sync Protocol Listen Address setting.)
Port 21027/UDP (for discovery broadcasts on IPv4 and multicasts on IPv6)
如果在linux上使用ufw, 則可以使用如下命令來打開:
打開syncthing服務:
sudo ufw allow syncthing
打開 Syncthing web GUI
sudo ufw allow syncthing-gui
3.3. 設置遠端Web GUI
除了前面提到的可以修改Syncthing配置文件以便外網(wǎng)可以訪問,也需要同時設置防火墻。
通過SSH隧道
如果您可以通過SSH訪問運行Syncthing的計算機,但不想將Web GUI端口對外部打開,則可以通過SSH隧道訪問它。您可以使用以下命令啟動隧道:
ssh -L 9999:localhost:8384 machine
這將綁定到您的本地端口9999,并將所有連接從那里轉(zhuǎn)發(fā)到目標機器上的端口8384。即使Syncthing只能在本地主機127.0.0.1上偵聽,這仍然有效。
4. 使用
圖形或者web界面操作,本質(zhì)上就是添加遠端設備ID,選擇需要同步或者共享的文件夾的過程,網(wǎng)上有現(xiàn)成的文檔說明,也有一些國外的視頻介紹其整個過程。 這里不再重復。
打開瀏覽器訪問本機,若是遠程云主機可以直接敲IP:
https://localhost:8384/
使用過程,共享文件和文件夾的過程可以看下面的視頻:
https://www.youtube.com/watch?v=BXSYKsYI1Zo
5. 架構與機制
然后補充一下軟件架構,如下圖所示:
架構和團隊開發(fā)的情況可以參考下面的調(diào)研報告:
調(diào)研報告:Syncthing: Open Source Continuous File Synchronisation
https://delftswa.gitbooks.io/desosa-2017/content/syncthing/chapter.html
另外需要說明的是,這個大學的學生對很多流行的軟件架構進行了調(diào)研,可以參考如下網(wǎng)址:
Delft Students on Software Architecture 的調(diào)研報告,對很多流行的開源軟件進行了軟件架構的調(diào)研:
https://www.gitbook.com/book/delftswa/desosa-2017/details
https://www.gitbook.com/book/delftswa/desosa2016/details
6. 文件大小與客戶端數(shù)量限制
查了一下官網(wǎng)的論壇, 找到一些相關限制,而且隨著開發(fā)的進行,限制也逐漸變大。
6.1. Syncing large files, “Scanning”, then nothing?
https://forum.syncthing.net/t/syncing-large-files-scanning-then-nothing/5617/10
128GB
time: Sep 2015
6.2. Syncthing lims & caps
https://forum.syncthing.net/t/syncthing-lims-caps/10011
Syncthing currently can’t handle files larger than about 1.25 TiB.
time: June 2017
6.3. Maximum amount of peer
https://github.com/syncthing/syncthing/issues/879
When I exceed 63 peer, for each connection I got the following error
version: 0.10.2 Linux x64
time: 19 Oct 2014
fixed version: 0.10.3
time: 14 Apr 2015
6.4. Maximum number of devices
https://forum.syncthing.net/t/maximum-number-of-devices/5749
data: The largest reported cluster has 457 devices, and the largest reported number of folders on a single device is 458
time: Sep 2015
6.5. 結論
看了論壇里的問答,文件和能同步的設備的最大限制一直在提升。
再看 統(tǒng)計:https://data.syncthing.net/, 最大的設備數(shù)達到了2200個。
對于日常應用是足夠的,就目前的版本,支持的最大的文件也超過1T,連接的客戶端也超過了500到2000個。
7. 測試的網(wǎng)絡占用圖示結果
在阿里云主機的1M帶寬下1.8G 文件同步情況:
顯然,文件同步期間完全占滿了整個帶寬,而且可能阿里云的1M帶寬可能有冗余,所以超出了1Mbps。
同步端:

被同步端:


8. Reference
syncthing官網(wǎng):
https://syncthing.net/
syncthing官網(wǎng)文檔:
https://docs.syncthing.net/intro/getting-started.html
Gen2安裝Syncthing
http://www.nasyun.com/thread-30460-1-1.html
Syncthing的安裝與使用
http://www.itdecent.cn/p/4235cc85c32d
syncthing統(tǒng)計:
https://data.syncthing.net/