Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two way sync with rsync

I have a folder a/ and a remote folder A/. I now run something like this on a 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 

when I make sync-music, it first gets all the diffs from server to local and then the opposite, sending all the diffs from local to server.

This works very well only if there are just updates or new files on the future. If there are deletions, it doesn't do anything.

In rsync there is --delete and --delete-after options to help accomplish what I want but thing is, it doesn't work on a 2-way-sync.

If I want to delete server files on a syn, when local files have been deleted, it works, but if, for some reason (explained after) I have some files that aren't in the server but exist locally and they were deleted, I want locally to remove them and not server copied (as it happens).

Thing is I have 3 machines in context:

  1. desktop
  2. notebook
  3. home-server

So, sometimes, server will have files that were deleted with a notebook sync, for example and then, when I run a sync with my desktop (where the deleted server files still exist on) I want these files to be deleted and not to be copied again to the server.

I guess this is only possible with a database and track of operations :P

Any simpler solutions? Thank you.

like image 351
mwm Avatar asked May 29 '10 21:05

mwm


People also ask

Can rsync do two way sync?

rsync works in one direction, so we need to run it twice to sync directories in both directions.

How do I sync two folders with rsync?

Synchronizing two folders with rsync To keep two folders in synchrony, not only do we need to add the new files in the source folder to the destination folder, as in the past topics, we also need to remove the files that are deleted in the source folder from the destination folder.

Does rsync copy both ways?

Rsync does a one way sync, however it's up to you to decide which way the sync goes. Note that you specify sync from source to destination. Source and destination can be any local or remote path. So to answer your question: it depends on how you execute rsync.

What is a two way sync?

Two-way synchronization (or bi-directional sync) is all about mirroring the data between two business tools. It means all newly created records in one app should be instantly (or near-instantly) created in the other one, and all updates of already existing records should be reflected in the records of the other app.


2 Answers

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.

like image 194
alex Avatar answered Oct 06 '22 01:10

alex


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.
like image 45
Dario Fumagalli Avatar answered Oct 06 '22 01:10

Dario Fumagalli