• rsync+inotify 远程同步


    rsync与传统的cptar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
    随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!

    Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。
    在前面有讲到,rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。

    环境说明:

    操作系统 IP 应用 服务器类型
    centos8 192.168.248.200

    rsync

    inotify-tools

    源服务器
    centos8 192.168.248.201 rsync 目标服务器

    需求:

    • 源服务器上/etc目录实时同步到目标服务器的/opt/下

    在目标服务器上做以下操作:

    #安装rsync
    [root@localhost opt]# yum -y install rsync
    
    #设置rsyncd.conf配置文件
    [root@localhost opt]# 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   #用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件
    
    [etc_from_client]  #自定义同步名称
    path = /opt/         #rsync服务端数据存放路径,数据将同步至此目录
    comment = sync etc from client   
    uid = root            # 设置rsync运行权限为root
    gid = root            # 设置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 = ganyu   #执行数据同步的用户名,可以设置多个用,隔开
    hosts allow = 192.168.248.200  #允许进行数据同步的客户端IP地址可以设置多个,隔开
    hosts deny = 192.168.1.1    #禁止数据同步的客户端IP地址可以设置多个,隔开
    
    #创建用户认证
    [root@localhost opt]# echo "ganyu:123456" > /etc/rsync.pass
    
    #修改文件权限
    [root@localhost opt]# chmod 600 /etc/rsync*
    
    #放行端口
    [root@localhost opt]# firewall-cmd  --zone=public --add-port=873/tcp --permanent
    [root@localhost opt]# firewall-cmd  --reload
    
    #启动服务
    [root@localhost opt]# rsync --daemon
    

    源服务器上做以下操作

    #安装rsync,只需要安装,不要启动,不需要配置
    [root@Centos8 opt]# yum -y install rsync
    
    #创建密码认证文件
    [root@Centos8 opt]# echo '123456' > /etc/rsync.pass
    
    #设置文件权限
    [root@Centos8 opt]# chmod 600 /etc/rsync.pass
    
    #在源服务器上创建测试目录,然后在源服务器运行以下命令
    [root@Centos8 opt]# mkdir -p test/file
    
    [root@Centos8 opt]# rsync -avH --port 873 --progress --delete /opt/test ganyu@192.168.248.202::etc_from_client --password-file=/etc/rsync.pass
    
    #运行完成后,在目标服务器上查看,在/opt目录下有test目录,说明数据同步成功
    

    再源服务器安装inotify-tools工具,实时触发rsync进行同步

    #查看服务器内核是否支持inotify
    [root@Centos8 opt]# ll /proc/sys/fs/inotify/
    total 0
    -rw-r--r-- 1 root root 0 May 12 00:21 max_queued_events
    -rw-r--r-- 1 root root 0 May 12 00:21 max_user_instances
    -rw-r--r-- 1 root root 0 May 12 00:21 max_user_watches
    
    #安装inotify-tools
    [root@Centos8 opt]# yum -y install inotify-tools
    
    #写同步脚本,让脚本自动去检测我们制定的目录下,文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去
    [root@Centos8 opt]# mkdir /scripts
    [root@Centos8 opt]# vim /scripts/inotify.sh
    #!/bin/bash
    
    #目标服务器IP
    host=192.168.248.202
    
    #源服务器监控目录,必须存在
    src=/etc        
    
    #模块名,需要与目标服务器上定义的同步名称一致
    des=etc_from_client   
    
    #执行数据同步的密码文件
    password=/etc/rsync.pass
    
    #执行同步的用户名
    user=ganyu       
    
    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
    
    [root@Centos8 opt]# chmod 755 /scripts/inotify.sh
    
    
    #防火墙放行
    [root@Centos8 opt]# firewall-cmd  --zone=public --add-port=873/tcp --permanent
    
    #启动脚本
    [root@Centos8 opt]# nohup  /usr/bin/bash /scripts/inotify.sh &
    
    #在源服务器修改文件
    [root@Centos8 opt]# userdel -r test
    
    #查看生成日志
    [root@Centos8 opt]# tail -5 /tmp/rsync.log 
    20210512 00:54 /etc/passwd.lock DELETE was rsynced
    20210512 00:54 /etc/group.lock DELETE was rsynced
    20210512 00:54 /etc/gshadow.lock DELETE was rsynced
    20210512 00:54 /etc/subuid.lock DELETE was rsynced
    

     设置开启自启动脚本

    [root@Centos8 opt]# chmod +x /etc/rc.d/rc.local
    
    [root@Centos8 opt]# echo 'nohup /bin/bash /scripts/inotify.sh &' >> /etc/rc.d/rc.loca
    
    #到目标服务器上去查看是否同步
    [root@localhost opt]# ls
    etc  test
    

    配置rsyncd.service文件

    [root@localhost opt]# vim /usr/lib/systemd/system/rsyncd.service
    [Unit]
    Description=fast remote file copy program daemon
    ConditionPathExists=/etc/rsyncd.conf
    
    [Service]
    EnvironmentFile=/etc/sysconfig/rsyncd
    ExecStart=/usr/bin/rsync --daemon --no-detach "$OPTIONS"
    
    [Install]
    WantedBy=multi-user.target
    
    
    [root@localhost opt]#  echo 'OPTIONS=""' > /etc/sysconfig/rsyncd
    
    
    [root@localhost opt]# systemctl  enable  rsyncd
    
  • 相关阅读:
    Atitit s2018.6 s6 doc list on com pc.docx Atitit s2018.6 s6 doc list on com pc.docx  Aitit algo fix 算法系列补充.docx Atiitt 兼容性提示的艺术 attilax总结.docx Atitit 应用程序容器化总结 v2 s66.docx Atitit file cms api
    Atitit s2018.5 s5 doc list on com pc.docx  v2
    Atitit s2018.5 s5 doc list on com pc.docx  Acc 112237553.docx Acc baidu netdisk.docx Acc csdn 18821766710 attilax main num.docx Atiitt put post 工具 开发工具dev tool test.docx Atiitt 腾讯图像分类相册管家.docx
    Atitit s2018 s4 doc list dvchomepc dvccompc.docx .docx s2018 s4 doc compc dtS44 s2018 s4 doc dvcCompc dtS420 s2018 s4f doc homepc s2018 s4 doc compc dtS44(5 封私信 _ 44 条消息)WebSocket 有没有可能取代 AJAX
    Atitit s2018 s3 doc list alldvc.docx .docx s2018 s3f doc compc s2018 s3f doc homepc sum doc dvcCompc dtS312 s2018 s3f doc compcAtitit PathUtil 工具新特性新版本 v8 s312.docx s2018 s3f doc compcAtitit 操作日
    Atitit s2018.2 s2 doc list on home ntpc.docx  Atiitt uke制度体系 法律 法规 规章 条例 国王诏书.docx Atiitt 手写文字识别 讯飞科大 语音云.docx Atitit 代码托管与虚拟主机.docx Atitit 企业文化 每日心灵 鸡汤 值班 发布.docx Atitit 几大研发体系对比 Stage-Gat
    Atitit 文员招募规范 attilax总结
    Atitit r2017 r6 doc list on home ntpc.docx
    atitit r9 doc on home ntpc .docx
    Atitit.如何文章写好 论文 文章 如何写好论文 技术博客 v4
  • 原文地址:https://www.cnblogs.com/diqiyao/p/14755490.html
Copyright © 2020-2023  润新知