Why do I need to sync local folders?
Imagine that you host multiple websites of same kind and you are hosting them on the same server. Say you have four blog websites that uses the same code.
During deployment you have to go onto each project and have to do similar deployment for all. What if you have about 15 such project you waste lot of your energy doing this?
Why cant we have one folder where we do deployment and all the files in that folder will be copied to other directories?
I know you might be thinking that why can't we have one directory and serve all domains from one directory? right?
Well, I am just making up the scenarios for our tutorial so that you understand what is sync process. Le't learn how to sync two local folder on linux based operating system.
What is Lsyncd daemon?
Lyncd is a linux daemon which sync your local directory with remote machines or other local directory on your machine.
It requires source and destination folders to sync. It keeps watching on source directory and if it detects any changes on source it will copy them to destination directory.
How to install Lsyncd on ubuntu or linux?
$ sudo apt-get update
$ sudo apt-get install lsyncd
How to Sync Two Local Folders with lsyncd?
First of all we have to create configuration and log folders and related files. To create log and config file for lsyncd follow the steps below on your terminal:
# create config dir and file
sudo mkdir /etc/lsyncd
touch /etc/lsyncd/lsyncd.conf.lua
# create log dir and files
sudo mkdir /var/log/lsyncd
touch /var/log/lsyncd/lsyncd.{log,status}
Next, we need to create a configuration file where we will define our logic that does the sync process. Let's follow the steps below to create a new config file with some contents:
# Let's add contents for our main config file /etc/lsyncd/lsyncd.conf.lua:
$ sudo nano /etc/lsyncd/lsyncd.conf.lua
# paste following contents in /etc/lsyncd/lsyncd.conf.lua
# save the file and exit the nano editor
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status"
}
Create source and destination folders
In order to test sync process we need to create source and destination folders. Let's create them using terminal:
# create source and destination folders
$ sudo mkdir /source_folder
$ sudo mkdir /destination_folder
# let's add 100 files to our source folder
$ cd /source_folder
$ sudo touch file{1..100}
For our test purpose we created 100 files in our source directory. Now, we will configure our lsyncd daemon to watch on our source directory. At this moment destination is an empty directory.
Once we run our lsyncd daemon all the 100 files from source should be copied to destination folder. Open /etc/lsyncd/lsyncd.conf.lua file and paste following contents:
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status"
}
sync {
default.rsync,
init = false, -- won't copy during first start
delete = false, -- won't delete dest files
excludeFrom="/etc/lsyncd.exclude",
source = "/source_folder",
target = "/destination_folder"
}
Now, let's restart our lsyncd daemon and see the magic:
# start the daemon
$ sudo service lsyncd start
# now, check your destination folder to see if
# all the 100 files from /source_folder folder
# has been copied properly
$ ls -l /destination_folder
You will see now that all the files from source to destination has been copied. In some cases you might want to ignore copying some files and folders within a source folder.
How to exclude files and folders using lsyncd?
To ignore some of the files and folders to be copied to destination folder. Let's create /etc/lsyncd.exclude file and paste following contents.
.svn
.git
vendor/
Now, if you restart the lsyncd daemon and place above files they wont be copied to the destination folder.