今天不是很高兴,为啥呢?之前,A在B找工作的时候 一直帮助B,然后有次B写了一段cpp代码,实现,变量都放在了类的内部,很臃肿的一个,然后A说了一句,太烂了,要放在公司肯定挨骂的,然后A和B就关系变差了。A很苦恼。
lsync 是啥呢? 其实可以简单的理解成inotufy+rsync,
机器角色:
A 10.210.71.144 安装了 rsync lsync
B 10.210.71.146 安装了 rsync
A机器做出了改变,会在某个特定的时间内,将变化同步到B,甚至更多的机器,inotify 感知文件的变化,rsync 用来把文件推送过去。
配置方案如下:
B机器上的配置:
安装rsync,目录树如下所示:
[root@test test]# tree /etc/rsyncd
/etc/rsyncd
├── rsyncd.conf
├── rsyncd.motd
└── rsyncd.secrets
0 directories, 3 files
为了方便,简单的配置文件如下所示:
[root@test test]# cat /etc/rsyncd/rsyncd.conf
pid file = /var/run/rsyncd.pid
uid = root
gid = root
use chroot = yes
read only = no
write only = no
hosts allow = 10.209.65.21 10.210.71.146 192.168.0.1/255.255.255.0 198.162.145.1 10.0.1.0/255.255.255.0 10.210.71.145 10.210.71.144 10.210.71.143
hosts deny = *
max connections = 5
#motd file = /etc/rsyncd/rsyncd.motd
log file = /tmp/rsyncd.log
transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
[data1]
path=/tmp/test
list=yes
comment = some description about this moudle
exclude = test1/ test2/
[root@test test]# vim /etc/rsyncd/rsyncd.conf
[root@test test]# cat /etc/rsyncd/rsyncd.conf
pid file = /var/run/rsyncd.pid
uid = root
gid = root
use chroot = yes
read only = no
write only = no
hosts allow = 10.210.71.146 10.210.71.145 10.210.71.144 10.210.71.143
hosts deny = *
max connections = 5
#motd file = /etc/rsyncd/rsyncd.motd
log file = /tmp/rsyncd.log
transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
[data1]
path=/tmp/test
在A机器上执行如下的命令进行测试:
[root@test test]# rsync root@10.210.71.146::data1/
drwxr-xr-x 4096 2015/01/23 13:28:53 .
-rw-r--r-- 0 2015/01/23 13:28:53 test
[root@test test]#
通了,配置成功。
配置A,A上面需要安装inotify 和lsync,比较新版本的系统可以支持yum安装了,省略
A机器上最简单的一个配置:
[root@test test]# cat /etc/lsyncd.conf
----
-- 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.rsync,
-- source="/tmp/",
-- host="10.210.71.146",
-- targetdir="/tmp/test/"
--}
settings {
logfile = "/var/log/lsyncd.log",
statusFile = "/var/log/lsyncd-status.log",
statusInterval = 20
}
sync{
default.rsync,
source = "/tmp/test",
target = "10.210.71.146::data1/",
}
[root@test test]#
source 指定的是A端的目录,B目录需要去同步的目录
target 指定的是B端的需要承接同步目录的目录。
测试如下
[root@test test]# rsync root@10.210.71.146::data1/
drwxr-xr-x 4096 2015/01/23 13:28:53 .
-rw-r--r-- 0 2015/01/23 13:28:53 test
[root@test test]# pwd
/tmp/test
[root@test test]# ll
total 0
-rw-r--r-- 1 root root 0 Jan 23 13:28 test
[root@test test]# touch test1
[root@test test]# date
Fri Jan 23 13:46:38 CST 2015
[root@test test]# rsync root@10.210.71.146::data1/
drwxr-xr-x 4096 2015/01/23 13:28:53 .
-rw-r--r-- 0 2015/01/23 13:28:53 test
[root@test test]# date
Fri Jan 23 13:46:51 CST 2015
[root@test test]# date
Fri Jan 23 13:47:21 CST 2015
[root@test test]# rsync root@10.210.71.146::data1/
drwxr-xr-x 4096 2015/01/23 13:46:37 .
-rw-r--r-- 0 2015/01/23 13:28:53 test
-rw-r--r-- 0 2015/01/23 13:46:37 test1
[root@test test]#
同步的工具很多,但是感觉这个非常的易用。