inotify+rsync安装配置

来源:互联网 发布:通用电气公司人事知乎 编辑:程序博客网 时间:2024/05/21 06:42

1.两台机器192.168.1.2,192.168.1.3,想把192.168.1.2的数据同步到192.168.1.3中

2.测试开始,可以先关闭防火墙和内核linux的selinux的防火墙,避免防火墙影响

关闭防火墙,例如centos7,其他系统版本自己查询如何关闭

[python] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ systemctl stop firewalld.service #停止firewall  
  2. $ systemctl disable firewalld.service #禁止firewall开机启动  

关闭linux的selinux防火墙

永久性关闭:生效需要重启

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ vi /etc/selinux/config  
  2. SELINUX=disabled  
临时性关闭:生效无需重启
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ setenforce 0  


3.安装rsync(两台服务器)

前往rsync官网下载最新版本 http://rsync.samba.org/ftp/rsync/src  找到最新的rsync-*.*.*.tar.gz

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ tar zxvf rsync-*.*.*.tar.gz  
  2. $ cd rsync-*.*.*  
  3. $ ./configure --prefix=/usr/local/rsync  
  4. $ make && make install  


4.配置rsyncd.conf (192.168.1.3)

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ vi /etc/rsync.conf  
  2. #pid文件的存放位置  
  3. pid file = /var/run/rsync.pid  
  4. #日志文件位置,启动rsync后自动产生这个文件,无需提前创建  
  5. log file = /var/log/rsync.log  
  6. #支持max connections参数的锁文件  
  7. lock file=/var/run/rsync.lock  
  8. #用户认证配置文件,里面保存用户名称和密码  
  9. secrets file = /etc/rsync.pw  
  10. #rsync启动时欢迎信息页面文件位置  
  11. motd file = /etc/rsyncd.motd  
  12. transfer logging = yes  
  13. log format = %t %a %m %f %b  
  14. syslog facility = local3  
  15. #自定义名称  
  16. [data]  
  17. #设置需要同步的目录  
  18. path = /data/test/  
  19. #模块名称与[data]自定义名称相同  
  20. comment = data  
  21. exclude = blank.png ; spinner.gif ; downsimple.png ; rails.png ; WEB-INF/  
  22. #默认端口  
  23. port = 873  
  24. #设置rsync运行权限为root  
  25. uid = root  
  26. #设置rsync运行权限为root  
  27. gid = root  
  28. #设置超时时间  
  29. timeout = 600  
  30. #最大连接数  
  31. max connections = 200  
  32. #默认为true,修改为no,增加对目录文件软连接的备份  
  33. use chroot = no  
  34. #设置rsync服务端文件为读写权限  
  35. read only = no  
  36. #不显示rsync服务端资源列表  
  37. list = no  
  38. #允许进行数据同步的客户端IP地址  
  39. hosts allow = 192.168.1.3  

可以设置多个目录(192.168.1.3)
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #增加test1目录  
  2. [test1]  
  3. path = /data/test1  
  4. list = yes  
  5. ignore errors  
  6. comment = ucweb-file system  
  7. secrets file = /etc/rsync.pw  
  8. exclude = blank.png ; spinner.gif ; downsimple.png ; rails.png ; WEB-INF/  

建立密码认证文件(192.168.1.3)

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ vi /etc/rsync.pw  
  2. root:123456  

配置rsyncd.motd文件,开始传送的时候会显示(192.168.1.3

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ vi /etc/rsyncd.motd  
  2. ###############################  
  3. #                             #  
  4. #        start rsync          #  
  5. #                             #  
  6. ###############################  

5.启动rsync
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ /usr/local/rsync/bin/rsync --daemon --config=/etc/rsyncd.conf  

开机启动rsync

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ echo '/usr/local/rsync/bin/rsync --daemon --config=/etc/rsyncd.conf'>>/etc/rc.d/rc.local  


6.建立密码认证文件(192.168.1.2)

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ vi /etc/rsync.pw  
  2. 123456  

7.测试开始

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ /usr/local/rsync/bin/rsync -avH --port=873 --progress --delete /data/test/ root@192.168.1.3::data --password-file=/etc/rsync.pw  
查看192.168.1.2上是否有同步

8.安装inotify-tools(192.168.1.2)

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ wget https://codeload.github.com/rvoicilas/inotify-tools/zip/master  
  2. $ tar zxvf inotify-tools-*.*.tar.gz   
  3. $ cd inotify-tools-*.*  
  4. $ ./configure --prefix=/usr/local/inotify  
  5. $ make && make install  

9.查看是否安装成功

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ ll /yunwei8/apps/inotify/bin/inotifywa*  

10.新建一个inotify.sh文件同步
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #!/bin/sh  
  2.   
  3. # get the current path  
  4. CURPATH=`pwd`  
  5.   
  6. /usr/local/inotify/bin/inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' \  
  7. -e close_write /data | while read date time dir file; do  
  8.   
  9.        FILECHANGE=${dir}${file}  
  10.        # convert absolute path to relative  
  11.        FILECHANGEREL=`echo "$FILECHANGE" | sed 's_'$CURPATH'/__'`  
  12.   
  13.        rsync -avH --port=873 --progress --delete /data/test/ root@192.168.1.3::data --password-file=/etc/rsync.pw  
  14.         echo "At ${time} on ${date}, file $FILECHANGE was backed up via rsync"  
  15. done  
时刻监听文件
0 0
原创粉丝点击