inotify+rsync安装配置,文件同步

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

4.可选:可以设置多个目录(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/  

5.建立密码认证文件(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.测试开始(在192.168.1.2)

先打开192.168.1.2上的/data/test/创建一个test.PHP测试文件,执行下面的命令

[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.3上/data/test目录下是否有同步过来的test.php

8.安装inotify-tools(192.168.1.2)

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

 

9.查看是否安装成功

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

10.新建一个inotify.sh设置文件同步/root/inotify.sh,如果下面代码不管用可参考https://github.com/rvoicilas/inotify-tools/wiki#info

 #!/bin/sh


# get the current path
CURPATH=`pwd`


/usr/local/inotify/bin/inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' \
-e close_write /data/test | while read date time dir file; do


       FILECHANGE=${dir}${file}
       # convert absolute path to relative
       FILECHANGEREL=`echo "$FILECHANGE" | sed 's_'$CURPATH'/__'`


       rsync -avH --port=873 --progress --delete /data/test/ root@192.168.1.3::data --password-file=/etc/rsync.pw
        echo "At ${time} on ${date}, file $FILECHANGE was backed up via rsync"
done

11.可执行权限与后台无输出运行

chmod 777 /root/inotify.sh

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. sh /root/inotify.sh >/dev/null 2>&1 &  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值