运维技能

来源:互联网 发布:如何下载app软件 编辑:程序博客网 时间:2024/06/17 04:28

lsync 是啥呢? 其实可以简单的理解成inotufy+rsync, 

机器角色:

A 10.210.71.144  安装了 rsync lsync

B 10.210.71.146  安装了 rsync

A机器做出了改变,会在某个特定的时间内,将变化同步到B,甚至更多的机器,inotify 感知文件的变化,rsync 用来把文件推送过去。

配置方案如下:

B机器上的配置:

安装rsync,目录树如下所示:

[python] view plain copy
  1. [root@test test]# tree  /etc/rsyncd  
  2. /etc/rsyncd  
  3. ├── rsyncd.conf  
  4. ├── rsyncd.motd  
  5. └── rsyncd.secrets  
  6.   
  7. 0 directories, 3 files  
为了方便,简单的配置文件如下所示:

[python] view plain copy
  1. [root@test test]# cat /etc/rsyncd/rsyncd.conf   
  2. pid file = /var/run/rsyncd.pid  
  3. uid = root  
  4. gid = root  
  5. use chroot = yes  
  6. read only = no  
  7. write only = no  
  8. 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   
  9. hosts deny = *  
  10. max connections = 5  
  11. #motd file = /etc/rsyncd/rsyncd.motd  
  12. log file = /tmp/rsyncd.log  
  13. transfer logging = yes  
  14. log format = %t %a %m %f %b  
  15. syslog facility = local3  
  16. timeout = 300  
  17.   
  18. [data1]  
  19. path=/tmp/test  
  20. list=yes  
  21. comment = some description about this moudle  
  22. exclude = test1/ test2/  
  23. [root@test test]# vim /etc/rsyncd/rsyncd.conf   
  24. [root@test test]# cat  /etc/rsyncd/rsyncd.conf   
  25. pid file = /var/run/rsyncd.pid  
  26. uid = root  
  27. gid = root  
  28. use chroot = yes  
  29. read only = no  
  30. write only = no  
  31. hosts allow = 10.210.71.146 10.210.71.145 10.210.71.144 10.210.71.143   
  32. hosts deny = *  
  33. max connections = 5  
  34. #motd file = /etc/rsyncd/rsyncd.motd  
  35. log file = /tmp/rsyncd.log  
  36. transfer logging = yes  
  37. log format = %t %a %m %f %b  
  38. syslog facility = local3  
  39. timeout = 300  
  40.   
  41. [data1]  
  42. path=/tmp/test  
在A机器上执行如下的命令进行测试:

[python] view plain copy
  1. [root@test test]# rsync root@10.210.71.146::data1/  
  2. drwxr-xr-x        4096 2015/01/23 13:28:53 .  
  3. -rw-r--r--           0 2015/01/23 13:28:53 test  
  4. [root@test test]  

通了,配置成功。

配置A,A上面需要安装inotify 和lsync,比较新版本的系统可以支持yum安装了,省略

A机器上最简单的一个配置:

[python] view plain copy
  1. [root@test test]# cat /etc/lsyncd.conf   
  2. ----  
  3. -- User configuration file for lsyncd.  
  4. --  
  5. -- Simple example for default rsync, but executing moves through on the target.  
  6. --  
  7. -- For more examples, see /usr/share/doc/lsyncd*/examples/  
  8. --   
  9. --   
  10. -- sync{  
  11. --     default.rsync,   
  12. --     source="/tmp/",   
  13. --     host="10.210.71.146",   
  14. --     targetdir="/tmp/test/"  
  15. --}  
  16. settings {  
  17.     logfile = "/var/log/lsyncd.log",  
  18.     statusFile = "/var/log/lsyncd-status.log",  
  19.     statusInterval = 20  
  20. }  
  21.   
  22. sync{  
  23.     default.rsync,  
  24.     source = "/tmp/test",  
  25.     target = "10.210.71.146::data1/",  
  26. }  
  27.   
  28.   
  29.   
  30. [root@test test]  

source 指定的是A端的目录,B目录需要去同步的目录

target   指定的是B端的需要承接同步目录的目录。

测试如下

[python] view plain copy
  1. [root@test test]# rsync root@10.210.71.146::data1/  
  2. drwxr-xr-x        4096 2015/01/23 13:28:53 .  
  3. -rw-r--r--           0 2015/01/23 13:28:53 test  
  4. [root@test test]# pwd  
  5. /tmp/test  
  6. [root@test test]# ll  
  7. total 0  
  8. -rw-r--r-- 1 root root 0 Jan 23 13:28 test  
  9. [root@test test]# touch test1  
  10. [root@test test]# date  
  11. Fri Jan 23 13:46:38 CST 2015  
  12. [root@test test]# rsync root@10.210.71.146::data1/  
  13. drwxr-xr-x        4096 2015/01/23 13:28:53 .  
  14. -rw-r--r--           0 2015/01/23 13:28:53 test  
  15. [root@test test]# date  
  16. Fri Jan 23 13:46:51 CST 2015  
  17. [root@test test]# date  
  18. Fri Jan 23 13:47:21 CST 2015  
  19. [root@test test]# rsync root@10.210.71.146::data1/  
  20. drwxr-xr-x        4096 2015/01/23 13:46:37 .  
  21. -rw-r--r--           0 2015/01/23 13:28:53 test  
  22. -rw-r--r--           0 2015/01/23 13:46:37 test1  
  23. [root@test test]#  

同步的工具很多,但是感觉这个非常的易用。