双向同步与 rsync

我有一个文件夹和一个远程文件夹。 我现在在 Makefile 上运行类似的东西:

get-music:
rsync -avzru server:/media/10001/music/ /media/Incoming/music/


put-music:
rsync -avzru /media/Incoming/music/ server:/media/10001/music/


sync-music: get-music put-music

当我制作 sync-music 时,它首先获取服务器到本地的所有差异,然后相反,将所有差异从本地发送到服务器。

只有在将来只有更新或新文件的情况下,这种方法才能很好地工作。如果有删除,它不会做任何事情。

在 rsync 中有—— delete 和—— delete-after 选项来帮助完成我想要的任务,但问题是,它不能在双向同步中工作。

如果我想删除一个 syn 上的服务器文件,当本地文件已经被删除,它的工作,但如果,出于某种原因(解释后) ,我有一些文件不在服务器上,但存在本地和他们被删除,我想本地删除他们,而不是服务器复制(因为它发生)。

问题是我有三台机器:

  1. 桌面
  2. 笔记本
  3. 主服务器

因此,有时,服务器会有文件被删除与笔记本同步,例如,然后,当我运行与我的桌面同步(其中删除的服务器文件仍然存在) ,我希望这些文件被删除,不要被复制到服务器。

我想这只有在数据库和操作跟踪的情况下才可能实现

有更简单的解决办法吗? 谢谢你。

139575 次浏览

Try Unison: http://www.cis.upenn.edu/~bcpierce/unison/

Syntax:

unison dirA/ dirB/

Unison asks what to do when files are different, but you can automate the process by using the following which accepts default (nonconflicting) options:

unison -auto dirA/ dirB/

unison -batch dirA/ dirB/ asks no questions at all, and writes to output how many files were ignored (because they conflicted).

Note: I am no longer using Unison (I use NextCloud, which doesn't address the original use case). However, note that rsync is not designed for bidirectional sync, while unison is. unison may have its bugs (as any other piece of software) and its wrinkles. I am surprised it seems to be actively maintained now (last time I looked I think I thought it looked dead), but I'm not sure what's the state nowadays. I haven't had the need to have a two-way file synchronizer, so there may be better options, though.

You could also try bitpocket: https://github.com/sickill/bitpocket

I'm not sure whether it works with two syncing but for the --delete to work you also need to add the --recursive parameter as well.

Try this,

get-music:
rsync -avzru --delete-excluded server:/media/10001/music/ /media/Incoming/music/


put-music:
rsync -avzru --delete-excluded /media/Incoming/music/ server:/media/10001/music/


sync-music: get-music put-music

I just test this and it worked for me. I'm doing a 2-way sync between Windows7 (using cygwin with the rsync package installed) and FreeNAS fileserver (FreeNAS runs on FreeBSD with rsync package pre-installed).

You could try csync, it is the sync engine under the hood of owncloud.

You might use Osync: http://www.netpower.fr/osync , which is rsync based with intelligent deletion propagation. it has also multiple options like resuming a halted execution, soft deletion, and time control.

Since the original question also involves a desktop and laptop and example involving music files (hence he's probably using a GUI), I'd also mention one of the best bi-directional, multi-platform, free and open source programs to date: FreeFileSync.

It's GUI based, very fast and intuitive, comes with filtering and many other options, including the ability to remote connect, to view and interactively manage "collisions" (in example, files with similar timestamps) and to switch between bidirectional transfer, mirroring and so on.

FreeFileSync can easily sync two computers on the same network and also sync two computers on different and remote networks.

  • On same network: have FreeFileSync use the local file system on one side and a shared network drive / path on the other. On Windows systems you enable file / disk sharing on one computer and access that share from the other. I use FreeFileSync this way to keep my main development PC source code synced with my 2 laptops. I have also synced one of these laptops with a Linux server with Samba installed and sharing one of its directories.
  • Across networks: create a VPN and do the same as above. FreeFileSync will see the remote disk as it was on the local network. Or buy one router that allows you to connect a USB disk to it and share over the internet. I have installed a VPN on a remote Linux server and used it through the OpenVPN Windows client.

I'm now using SparkleShare https://www.sparkleshare.org/

works on mac, linux and windows.

Rclone is what you are looking for. Rclone ("rsync for cloud storage") is a command line program to sync files and directories to and from different cloud storage providers including local filesystems. Rclone was previously known as Swiftsync and has been available since 2013.

I'm surprised no one has mentioned Syncthing yet. I have been using it for years to synchronize my phone, my tablet and my two laptops. One time I also used it to send 10 GB of photos to my family ~600 km away, straight from my machine to their machine, and it was incredibly fast (despite the data getting routed through Syncthing's discovery server to work around NAT issues). I also tried OwnCloud/NextCloud at some point but Syncthing has been much more reliable and, also, much faster.