Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep two folders automatically synchronized?

I would like to have a synchronized copy of one folder with all its subtree.

It should work automatically in this way: whenever I create, modify, or delete stuff from the original folder those changes should be automatically applied to the sync-folder.

Which is the best approach to this task?

BTW: I'm on Ubuntu 12.04

Final goal is to have a separated real-time backup copy, without the use of symlinks or mount. I used Ubuntu One to synchronize data between my computers, and after a while something went wrong and all my data was lost during a synchronization.

So I thought to add a step further to keep a backup copy of my data:

  • I keep my data stored on a "folder A"
  • I need the answer of my current question to create a one-way sync of "folder A" to "folder B" (cron a script with rsync? could be?). I need it to be one-way only from A to B any changes to B must not be applied to A.
  • The I simply keep synchronized "folder B" with Ubuntu One

    In this manner any change in A will be appled to B, which will be detected from U1 and synchronized to the cloud. If anything goes wrong and U1 delete my data on B, I always have them on A.

Inspired by lanzz's comments, another idea could be to run rsync at startup to backup the content of a folder under Ubuntu One, and start Ubuntu One only after rsync is completed.

What do you think about that? How to know when rsync ends?

like image 338
Luca Borrione Avatar asked Sep 17 '12 13:09

Luca Borrione


People also ask

How do I sync two folders in Windows 11?

Choose the folders you want to sync from and to, the Left Folder for the source folder and the Right Folder for the destination folder. Click Browse to select. 4. Select sync mode in “What do you want to do?” step.


2 Answers

You can use inotifywait (with the modify,create,delete,move flags enabled) and rsync.

while inotifywait -r -e modify,create,delete,move /directory; do     rsync -avz /directory /target done 

If you don't have inotifywait on your system, run sudo apt-get install inotify-tools

like image 163
silgon Avatar answered Sep 21 '22 02:09

silgon


You need something like this: https://github.com/axkibe/lsyncd It is a tool which combines rsync and inotify - the former is a tool that mirrors, with the correct options set, a directory to the last bit. The latter tells the kernel to notify a program of changes to a directory ot file. It says:

It aggregates and combines events for a few seconds and then spawns one (or more) process(es) to synchronize the changes.

But - according to Digital Ocean at https://www.digitalocean.com/community/tutorials/how-to-mirror-local-and-remote-directories-on-a-vps-with-lsyncd - it ought to be in the Ubuntu repository!

I have similar requirements, and this tool, which I have yet to try, seems suitable for the task.

like image 22
Martin Avatar answered Sep 17 '22 02:09

Martin