rsync+inotify实现实时同步

本文介绍如何使用rsync实现文件同步,并结合inotify工具实时监控文件变化,自动触发同步任务。文中详细记录了rsync及inotify-tools的安装配置过程,以及客户端脚本的编写方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

soft:rsync-3.0.8.tar.gz inotify-tools-3.14.tar.gz

wget http://rsync.samba.org/ftp/rsync/rsync-3.0.8.tar.gz
wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
server:192.168.2.211
bakserver:192.168.2.67 
bakserver(被同步端):

  1. rsync安装 
  2. tar xzvf rsync-3.0.8.tar.gz 
  3. cd rsync-3.0.8 
  4. ./configure 
  5. make 
  6. make install 
  7.  
  8. mkdir /etc/rsyncd 
  9. vim rsyncd.conf 
  10.  
  11. uid = root 
  12. gid = root 
  13. user chroot = no 
  14. max connections = 50 
  15. timeout = 180 
  16. pid file = /etc/rsyncd/rsyncd.pid 
  17. lock file= /etc/rsyncd/rsyncd.lock 
  18. log file = /var/log/rsyncd.log 
  19. transfer logging = yes 
  20. log format = %t %a %m %f %b 
  21. syslog facility = local3 
  22. secrets file = /etc/rsyncd/rsyncd.password 
  23.  
  24. [nagios] 
  25. path=/usr/local/nagios/ 
  26. ignore errors 
  27. hosts allow = 192.168.0.0/22 
  28. hosts deny = 0.0.0.0/32 
  29. secrets file = /etc/rsyncd/rsyncd.password 
  30. read only = no 
  31. list = no 
  32. auth users = yunwei  保存退出。 echo "yunwei:123456" >/etc/rsyncd/rsyncd.password #yunwei:123456,前者为认证用户名,后者为认证密码。 chmod 600 /etc/rsyncd/rsyncd.password #安全考虑 启动服务:rsync --daemon --config=/etc/rsyncd/rsyncd.conf 加入自启动:ehoc "rsync --daemon --config=/etc/rsyncd/rsyncd.conf" >/etc/rc.d/rc.local

server:
 

  1. rsync安装 
  2. tar xzvf rsync-3.0.8.tar.gz 
  3. cd rsync-3.0.8 
  4. ./configure 
  5. make 
  6. make install 
  7.  
  8. tar xvzf inotify-tools-3.14.tar.gz 
  9. cd inotify-tools-3.14 
  10. ./configure 
  11. make 
  12. make install 
  13.  
  14. ll /usr/local/bin/inotifywa* 
  15. -rwxr-xr-x 1 root root 37264 04-14 13:42 /usr/local/bin/inotifywait 
  16. -rwxr-xr-x 1 root root 35438 04-14 13:42 /usr/local/bin/inotifywatch 
  17. inotify-tools安装完成后,会生成inotifywait和inotifywatch两个指令,其中, 
  18. inotifywait用于等待文件或文件集上的一个特定事件,它可以监控任何文件和目录设置,并且可以递归地监控整个目录树。 
  19. inotifywatch用于收集被监控的文件系统统计数据,包括每个inotify事件发生多少次等信息。 
  20.  
  21. 创建认证密码: 
  22. mkdir /etc/rsyncd 
  23. echo "123456" >/etc/rsyncd/rsyncd.password     #此处只需密码即可。 
  24. chmod 600 /etc/rsyncd/rsyncd.password 

客户端运行脚本:

  1. vim nagios_rsync.sh 
  2. #!/bin/bash 
  3. src=/usr/local/nagios/ 
  4. des=nagios           #此处为模块名称。 
  5. ip=192.168.2.67 
  6. /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib $src | while read file 
  7. do 
  8. rsync -vzrtopg --delete --progress $src yunwei@$ip::$des --password-file=/etc/rsyncd/rsyncd.password >/dev/null 2>&1 
  9. done 
  10.  
  11. 保存,赋予执行权限。 
  12.  
  13. 后台运行: 
  14. nohup ./nagios_rsync.sh & 
  15.  
  16. 加入自启动: 
  17. echo "nohup ./nagios_rsync.sh &" >>/etc/rc.d/rc.local
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值