Publish: 2017-11-11 | Modify: 2017-11-11
Lsyncd combines inotify + rsync and monitors local directory tree events through the inotify or fsevents interface. It aggregates and combines events for a few seconds and then generates one or more processes to synchronize the changes.
There are two CentOS 7 servers, where A: 192.168.1.100
needs to be synchronized in real-time to B: 192.168.1.200
.
Lsyncd is based on rsync and requires rsync >= 3.1. You can check the current version by entering rsync -v
. If it does not meet the requirements, please refer to: "CentOS Upgrade rsync version" to upgrade rsync on both servers A and B.
If you want to synchronize data from A to B, A must have passwordless login permission to B. You can set up key login to achieve this. Only two commands are needed, and the following commands are executed on server A:
# Generate key file
ssh-keygen -t rsa
# Copy public key to B
ssh-copy-id username@remote-server
# Test
ssh username@remote-server
username
is the username of server B, and remote-server
is the IP address of server B. If the server is not using 22
as the SSH port, you need to specify the port number with -p
parameter. For more information, refer to: "SSH Passwordless Login: Only Two Simple Steps (Linux)".
The built-in sources in CentOS 7 do not include Lsyncd. You can compile and install Lsyncd by yourself, but a simpler way is to install the epel repository first, and then you can install it directly with yum. The following commands are executed on server A:
yum -y install epel-release
If the epel repository has been installed, you can directly enter the following command to install lsyncd. The following commands are executed on server A:
# Install lsyncd
yum -y install lsyncd
# Check lsyncd version
lsyncd --version
After the installation is completed, the configuration file is located at /etc/lsyncd.conf
. You will see the following content, where --
is the comment symbol.
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
-- For more examples, see /usr/share/doc/lsyncd*/examples/
--
sync{default.rsyncssh, source="/var/www/html", host="localhost", targetdir="/tmp/htmlcopy/"}
Modify the configuration file with vi /etc/lsyncd.conf
, comment or delete the last line of the default configuration, and then add your own configuration.
settings {
logfile = "/tmp/lsyncd.log",
statusFile = "/tmp/lsyncd.status",
insist = true,
statusInterval = 10
}
sync {
default.rsyncssh,
source="/home/test1",
host="192.168.1.200",
targetdir="/home/test2",
rsync = {
archive = true,
compress = false,
whole_file = false
},
ssh = {
port = 22
}
}
Continue to enter the command lsyncd -nodaemon /etc/lsyncd.conf
to run and check for any errors. If there are errors, check and modify according to the error messages. If there are no errors, exit and enter the command systemctl start lsyncd
to start lsyncd.
settings
is the global configuration, and some parameters are as follows:
logfile
: Path of the log filestatusFile
: Path of the processinsist
: Continue running even if there are failed targetsstatusInterval
: Write to file every X seconds, default is 10ssync
is the synchronization configuration, and some parameters are as follows:
source
: Local file directoryhost
: Remote server addresstargetdir
: Remote target directoryport
: SSH port number of the target host, default is 22# Start
systemctl start lsyncd
# Stop
systemctl stop lsyncd
# Restart
systemctl restart lsyncd
# Set to start automatically on boot
systemctl enable lsyncd
The above method has been tested on CentOS 7. If it is CentOS 6, there may be some slight differences, but the process and steps are the same.
Lsyncd is an open-source synchronization software based on inotify + rsync. It supports incremental synchronization. Compared with Resilio Sync, its advantages are better security and lower resource consumption. It also provides more convenient customization with various parameters. The disadvantage is that the configuration is a bit more complicated than Resilio Sync, and the speed may be slower than Resilio Sync when there are multiple servers because Resilio Sync uses P2P sharing.
Lsyncd official documentation: https://axkibe.github.io/lsyncd/
Project repository: https://github.com/axkibe/lsyncd
I come from China and I am a freelancer. I specialize in Linux operations, PHP, Golang, and front-end development. I have developed open-source projects such as Zdir, ImgURL, CCAA, and OneNav.