搭建rsync-inotify同步服务

在目标服务器上做以下操作:
//关闭防火墙,selinux
[root@zzg ~]# systemctl stop firewalld
[root@zzg ~]# setenforce 0

//安装rsync服务,启动服务
[root@zzg ~]# yum -y install rsync
[root@zzg ~]# systemctl start rsyncd
[root@zzg ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.

//配置服务的配置文件
[root@zzg ~]# vim /etc/rsyncd.conf
(在后面加上以下代码)
log file = /var/log/rsyncd.log //日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile = /var/run/rsyncd.pid  //pid文件的存放位置
lock file = /var/run/rsync.lock  //支持max connections参数的锁文件
secrets file = /etc/rsync.pass   //用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件

[zhangzhengguang]  //自定义同步名称
path = /zzg/    //rsync服务端数据存放路径,客户端的数据将同步至此目录
comment = hello world
uid = 0  //设置rsync运行权限为root
gid = 0   //设置rsync运行权限为root
port = 873  //默认端口
ignore errors  //表示出现错误忽略错误
use chroot = no  //默认为true,修改为no,增加对目录文件软连接的备份
read only = no   //设置rsync服务端为读写权限
list = no   //不显示rsync服务端资源列表
max connections = 200  //最大连接数
timeout = 600   //设置超时时间
auth users = tom   //执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 192.168.220.50  //允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.220.60  hosts deny = 192.168.1.1      //禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
"/etc/rsyncd.conf" 45L, 856C                             40,1          66%

//创建用户认证文件
[root@zzg ~]# echo 'tom:123456' > /etc/rsync.pass

//设置文件权限
[root@zzg ~]# chmod 600 /etc/rsync*
[root@zzg ~]# ll /etc/rsync*
-rw-------. 1 root root 856 Feb 19 01:44 /etc/rsyncd.conf
-rw-------. 1 root root  11 Feb 19 01:46 /etc/rsync.pass

//创建同步目录
[root@zzg ~]# mkdir /zzg

//重启服务
[root@zzg ~]# systemctl restart rsyncd

在源服务器上做以下操作
//关闭防火墙,selinux
[root@zzg ~]# systemctl stop firewalld
[root@zzg ~]# setenforce 0

//安装rsync服务
[root@zzg ~]# yum -y install rsync

//创建认证密码文件,设置权限

[root@zzg ~]# echo '123456' > /etc/rsync.pass
[root@zzg ~]# chmod 600 /etc/rsync.pass 
[root@zzg ~]# ll /etc/rsync.pass 
-rw-------. 1 root root 7 Feb 19 01:56 /etc/rsync.pass

//在源服务器上创建测试目录,然后在源服务器运行以下命令
[root@zzg ~]# mkdir -p etc/test
[root@zzg ~]# rsync -avH --port 873 --progress --delete /root/etc/ tom@192.168.220.40::zhangzhengguang --password-file=/etc/rsync.pass
sending incremental file list
./
test/

sent 49 bytes  received 15 bytes  6.10 bytes/sec
total size is 0  speedup is 0.00

//在目标服务器上查看
[root@zzg ~]# cd /zzg
[root@zzg zzg]# ls
test

//安装inotify-tools工具,实时触发rsync进行同步
[root@zzg ~]# yum -y install make gcc gcc-c++
[root@zzg ~]# yum -y install inotify-tools
此工具只有网络源有

//写同步脚本,此步乃最最重要的一步,请慎之又慎。让脚本自动去检测我们指定的目录下 \
//文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去
[root@zzg ~]# mkdir /scripts
[root@zzg ~]# touch /scripts/inotify.sh
[root@zzg ~]# chmod 755 /scripts/inotify.sh 
[root@zzg ~]# ll /scripts/inotify.sh 
-rwxr-xr-x. 1 root root 0 Feb 19 02:33 /scripts/inotify.sh
[root@zzg ~]# vim /scripts/inotify.sh
host=192.168.220.40      //目标服务器的ip(备份服务器)
src=/root/etc        //在源服务器上所要监控的备份目录(此处可>以自定义,但是要保证存在)
des=zhangzhengguang     //自定义的模块名,需要与目标服务器上定
义的同步名称一致
password=/etc/rsync.pass        //执行数据同步的密码文件
user=tom          //执行数据同步的用户名
inotifywait=/usr/bin/inotifywait

$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files ;do
    rsync -avzP --delete  --timeout=100 --password-file=${password} $src $user@$host::$des
    echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
~                                                             
~                                                             
~                                                             
~                                                             
~                                                             
~                                                             

"/scripts/inotify.sh" 12L, 720C             12,1          All

//启动脚本
[root@zzg ~]# nohup bash /scripts/inotify.sh &
[2] 36434
[root@zzg ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@zzg ~]# ps -ef |grep inotify
root      36229  12670  0 02:45 pts/1    00:00:00 bash /scripts/inotify.sh
root      36230  36229  0 02:45 pts/1    00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /root/etc
root      36231  36229  0 02:45 pts/1    00:00:00 bash /scripts/inotify.sh
root      36434  12670  0 02:49 pts/1    00:00:00 bash /script

//在源服务器上生成一个新文件
[root@zzg ~]# cd etc/test/
[root@zzg test]# touch aa

//查看inotify生成的日志
[root@zzg test]# tail /tmp/rsync.log 
20190219 02:52 /root/etc/test/aaCREATE was rsynced
20190219 02:52 /root/etc/test/aaCREATE was rsynced
20190219 02:52 /root/etc/test/aaATTRIB was rsynced
20190219 02:52 /root/etc/test/aaATTRIB was rsynced

//设置脚本开机自动启动:
[root@zzg etc]# ll /etc/rc.local 
lrwxrwxrwx. 1 root root 13 Jan  8 16:04 /etc/rc.local -> rc.d/rc.local
[root@zzg etc]# ll /etc/rc.d/rc.local 
-rw-r--r--. 1 root root 473 Jun 27  2017 /etc/rc.d/rc.local
[root@zzg etc]# chmod 755 /etc/rc.d/rc.local 
[root@zzg etc]# ll /etc/rc.d/rc.local 
-rwxr-xr-x. 1 root root 473 Jun 27  2017 /etc/rc.d/rc.local (给rc.local执行权限)
[root@zzg etc]# vim /etc/rc.local 
[root@zzg etc]# tail -1 /etc/rc.local 
nohup /bin/bash /scripts/inotify.sh

到目标服务器上去查看是否把新生成的文件自动传上去了:
[root@zzg ~]# cd /root/etc/
[root@zzg etc]# ls
test
[root@zzg etc]# touch hello world
//目标服务器查看
[root@zzg ~]# cd /zzg/
[root@zzg zzg]# ls
etc
[root@zzg zzg]# cd etc/
[root@zzg etc]# ls
hello  test  world
此时已经同步成功


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值