发现了lsyncd这个程序,比较好用,于是就寻思这用这个来替代。
A若要实现免密码登录B,那么需要在A上面生成密钥,然后将密钥传给B,这样A就可以实现免秘钥登录B了。
ssh-keygen -t rsa
ssh-copy-id username@host
编译要求:
Lua >= 5.2
Lsyncd depends on Lua 5.2 or greater; that is 5.2 or 5.3. For most distributions you need to install the liblua??, the liblua??-dev and the lua?? package, with ?? being the respective Lua version.
cmake >= 2.8
To configure Lsyncd to your system, cmake >= 2.8 is required
rsync >= 3.1
During runtime Lsyncd needs rsync > 3.1 installed both on source and target systems
yum -y install lua-devel cmake rsync
从github同步源码并编译安装
$ git clone https://github.com/LoveyCat/lsyncd.git
$ cd lsyncd && mkdir build && cd build && cmake .. -DCMAKE_INSTALL_PREFIX=/opt/lsyncd && make
$ sudo make install
创建配置文件
$ sudo vim /etc/lsyncd.conf
settings {
logfile = "/var/lsyncd.log",
statusFile = "/var/lsyncd.status",
inotifyMode = "CloseWrite",
}
sync {
default.rsync,
source = "/home/Data",
target = "root@172.16.0.1:/mnt/sdb1",
rsync = {
binary= "/usr/bin/rsync",
archive = true,
-- compress = true,
}
}
简单说下logfile是日志文件,statusFile是运行状态文件,这个可有可无,source就是你要监视并同步的目录,target就是你要同步的目标,比如本次主机B就是我的目标,这种属于远程同步,想要了解更多,请参阅官方文档。
保存后,直接用命令启动就可以了
$ sudo /opt/lsyncd/bin/lsyncd /etc/lsyncd.conf
这样可以直接加载配置文件运行lsyncd,如果不用配置文件,最简单的命令就是官方提供的部分例子了:
Although for standard use or quick testing it can be cursorily configured by command line options. The following will keep a local source and destination directory in sync using rsync:
lsyncd -rsync /home/USER/src /home/USER/dst
The target can here be anything that Rsync recognizes.
lsyncd -rsync /home/USER/src remotehost:dst
Two (or more) targets are configured by calling -rsync twice (or several times).
lsyncd -rsync /home/USER/src remotehost1:dst -rsync /home/USER/src remotehost2:dst
写出了自启动脚本,更新下:
[Unit]
Description=lsyncd service
After=network.target
[Service]
Type=simple
ExecStart=/opt/lsyncd/bin/lsyncd -nodaemon /etc/lsyncd.conf
ExecReload=
ExecStop=/bin/kill -WINCH ${MAINPID}
[Install]
WantedBy=multi-user.target
参考的文章还是要说下的:http://seanlook.com/2015/05/06/lsyncd-synchronize-realtime/
至于更多的用法,请自己参阅官方文档吧:https://axkibe.github.io/lsyncd/manual/invoking/
本文由 Ethan 创作,采用 知识共享署名4.0 国际许可协议进行许可。
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。