To sync files between two local directories on Unix or Linux, you can either choose to perform a soft link to the source directory or use rsync
or lsyncd
daemon.
Let’s say that you have a source directory /mnt/source
. And you want all the files to be synced from /mnt/source
to /mnt/dest
directory.
Sync with soft link
Technically, this method does not sync the files. It merely creates a symbolic link to the source directory.
1 |
# ln -s /mnt/source /mnt/dest |
That’s it. All the files that you see on /mnt/source
will now also be available under /mnt/dest
. But take note that if you accidentally delete a file from the source directory, you will not be able to recover the file from the destination directory. You can use this method if you just need the same files to appear on two different directories.
Sync with rsync
The rsync tool will allow you to make a proper sync of two directories. Install rsync
if it’s not yet available on your system.
1 |
$ sudo apt-get install rsync |
1 |
$ sudo yum install rsync |
Once installed, perform a sync by using the rsync
command. The “-a” option will preserve the file permission, ownership and timestamps.
1 |
# rsync -a /mnt/source /mnt/dest |
To perform the sync at periodic intervals, you can consider scheduling rsync from cron.
Sync with lsyncd
lsyncd
daemon monitors the filesystem for changes using inotify
. To know more about inotify, refer to Linux: How to Monitor File System for Changes. Install lysncd
daemon on your system.
1 |
$ sudo apt-get install lsyncd |
1 |
$ sudo yum install lsyncd |
Next, edit the lsyncd
configuration file lsyncd.conf.lua
found in /etc/lsyncd
. Some distributions keep the file under the /etc
directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
settings { logfile = "/var/log/lsyncd/lsyncd.log", statusFile = "/var/log/lsyncd/lsyncd-status.log", statusInterval = 5, pidfile = "/var/run/lsyncd.pid" } sync{ default.rsync, source="/mnt/source", target="/mnt/dest", rsync = { archive = true, owner = true, perms = true, group = true, compress = true, acls = true, verbose = true, } } |
Once the config file has been modified, restart the lsyncd
daemon for the changes to take effect.
1 |
# service lsyncd restart |
If there are no errors, the lysncd
daemon will start to continuously monitor the source directory for changes and the destination directory will be updated within a few seconds without any manual intervention. Do not use lsyncd
to perform backups as once the source file is removed, lsyncd
will automatically remove the corresponding file in the destination directory as well.