Sync one directory on one machine to another

From Klaus' wiki
Revision as of 09:13, 8 July 2016 by Klaus (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Now you may be in the situation that you want to keep two directories in sync. In my example it was a virtual Fedora that I wanted keep a copy of in another place. My virtual is running on my home laptop and the work I do here I want to copy to a network connected drive out on the university.

Luckely one good hearted soul invented lsyncd. See github for more details and the manual.

What I did was to install lsyncd, which is a part of one of the Fedora repositories I have installed, in fact the default Fedora repository.

So, as root, issue:

dnf install lsyncd

then enable the lsyncd daemon

systemctl enable lsyncd.service

next edit the configuration file

vi /etc/lsyncd.conf

to have a similar content as shown below. Modify for your need.

sync{
        default.rsyncssh,
        source="/home/klaus/workspaces",
        host="klaus@192.168.0.183",
        targetdir="/home/klaus/mounts/.../workspaces"
}

From this you can see that I want to copy whatever exists in /home/klaus/workspaces to my laptop, identified by the login name and the IP address. The destination is my sshFS mounted remote filesystem on the university network.

Just before starting the lsyncd do ensure that your root account can login on the remote computer by issuing

ssh-copy-id -i ~/.ssh/id_rsa.pub klaus@192.168.0.183

test that you can login without using password.

Finally let the daemon loose

systemctl start lsyncd.service

In a few seconds you'll wee that the local directory structur and files are rsync'ed to the remote destination. Take a look on the man page for rsync - it's a pretty neat tool for synchronising computers and other stuff. lsync just builds an easy interface for using rsync.

What if the network is delayed

If your network is taking its time to get up and being stable you can modify the control file that starts up the lsync daemon.

vi /etc/systemd/system/multi-user.target.wants/lsyncd.service

Add a line

Requires=network-online.target

in the Unit section.

This will ensure that the network is up and that rsync is able to establish a connection to the remote server. If rsync fails login in the lsyncd fails too.