• Rsync+Sersync实时文件同步


    Rsync+Sersync实时文件同步

    实时同步方案:

    1、rsync+inotify (不推荐,inotify是对本地文件或目录的实时监控)

    2、rsync+sersync (推荐,是inotify的升级版,功能更强大,本篇博客就使用该方案)

    sersync是基于inotify开发的,类似于inotify-tools的工具,Sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或者某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的文件或者目录,因此效率更高。

    环境
    服务器名称IP地址服务
    web服务器(客户端)10.0.8.59rsync、sersync
    backup服务器(服务端)10.0.8.70rsync

    实操

    一、安装rsync (客户端、服务端均安装,需要epel源支持)

    1. # yum install rsync -y
    2. 已加载插件:fastestmirror
    3. Determining fastest mirrors
    4. base | 3.6 kB 00:00:00
    5. epel | 4.7 kB 00:00:00
    6. extras | 2.9 kB 00:00:00
    7. updates | 2.9 kB 00:00:00
    8. (1/5): epel/group_gz | 97 kB 00:00:00
    9. (2/5): epel/updateinfo | 1.0 MB 00:00:00
    10. (3/5): epel/primary_db | 7.0 MB 00:00:00
    11. (4/5): extras/primary_db | 247 kB 00:00:00
    12. (5/5): updates/primary_db | 17 MB 00:00:00
    13. 正在解决依赖关系
    14. --> 正在检查事务
    15. ---> 软件包 rsync.x86_64.0.3.1.2-10.el7 将被 安装
    16. --> 解决依赖关系完成
    17. 依赖关系解决
    18. =============================================================================================================================================================================================
    19. Package 架构 版本 源 大小
    20. =============================================================================================================================================================================================
    21. 正在安装:
    22. rsync x86_64 3.1.2-10.el7 base 404 k
    23. 事务概要
    24. =============================================================================================================================================================================================
    25. 安装 1 软件包
    26. 总下载量:404 k
    27. 安装大小:815 k
    28. Downloading packages:
    29. rsync-3.1.2-10.el7.x86_64.rpm | 404 kB 00:00:00
    30. Running transaction check
    31. Running transaction test
    32. Transaction test succeeded
    33. Running transaction
    34. 正在安装 : rsync-3.1.2-10.el7.x86_64 1/1
    35. 验证中 : rsync-3.1.2-10.el7.x86_64 1/1
    36. 已安装:
    37. rsync.x86_64 0:3.1.2-10.el7
    38. 完毕!

     二、服务端配置,后台启动rsync服务

    1、添加配置文件

    vim /etc/rsyncd.conf

    1. # 全局配置
    2. uid = rsync
    3. gid = rsync
    4. use chroot = no
    5. max connections = 4
    6. pid file = /var/run/rsyncd.pid
    7. lock file = /var/run/rsyncd.lock
    8. log file = /data/rsync-log/rsyncd.log
    9. timeout = 900
    10. fake super = yes
    11. auth users = yukw # 虚拟用户,全局配置
    12. secrets file = /data/rsync-passwd/rsync.passwd
    13. hosts allow = 10.0.0.0/8 # 这里注意如果写成 10.0.0.0/24会出现客户端同步时找不到模块的报错
    14. hosts deny = *
    15. read only = false
    16. list = false
    17. [Qyun-10.0.8.59] # 模块一
    18. Comment = 10.0.8.59:/data/service/Automatic-deployment/Qyun-10.0.8.59
    19. path = /data/rsync-backup/Qyun-10.0.8.59
    20. [Qyun-10.0.8.59-nginx]
    21. Comment = 10.0.8.59:/data/services/nginx/conf
    22. path = /data/rsync-backup/Qyun-10.0.8.59-nginx

     如果写成  hosts allow = 10.0.0.0/24 后出现的报错信息

    2、创建目录、文件、修改权限、后台启动服务 

    1. # mkdir /data/{rsync-passwd,rsync-backup,rsync-log}
    2. # mkdir /data/rsync-backup/{Qyun-10.0.8.59,Qyun-10.0.8.59-nginx}
    3. # cd /data/
    4. # ll
    5. # useradd -M -s /sbin/nologin rsync
    6. # chown -R rsync.rsync rsync-backup/
    7. # chown -R rsync.rsync rsync-log
    8. # chown rsync.rsync rsync-passwd
    9. # cd rsync-passwd/
    10. # vim rsync.passwd
    11. yukw:123
    12. # chmod 600 rsync.passwd
    13. # cd /data/rsync-passwd/
    14. # ll
    15. 总用量 4
    16. -rw------- 1 root root 15 8月 25 10:50 rsync.passwd
    17. # cat rsync.passwd
    18. yukw:123
    19. # rsync --daemon # 后台启动
    20. # ps -ef | grep rsync
    21. root 1342 1 0 09:50 ? 00:00:00 rsync --daemon
    22. root 5102 1420 0 11:09 pts/0 00:00:00 grep --color=auto rsync
    23. 或者通过systemctl启动
    24. # systemctl start rsyncd.service
    25. # systemctl stauts rsyncd.service
    26. # systemctl enable rsyncd.service

    通过systemctl start rsyncd.service方式启动

    三、客户端配置,安装sersync

    1、安装sersync

    1. # wget https://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz
    2. # ll
    3. # tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
    4. # ll
    5. # mv GNU-Linux-x86/ sersync-2.5.4
    6. # ln -svf sersync-2.5.3 sersync
    7. # cd sersync/
    8. # ll
    9. # cp sersync2 /usr/bin/sersync

    2、修改配置文件

    # vim confxml.xml

    1. "1.0" encoding="ISO-8859-1"?>
    2. <head version="2.5">
    3. <host hostip="localhost" port="8008">host>
    4. <debug start="false"/>
    5. <fileSystem xfs="false"/>
    6. <filter start="false">
    7. <exclude expression="(.*)\.svn">exclude>
    8. <exclude expression="(.*)\.gz">exclude>
    9. <exclude expression="^info/*">exclude>
    10. <exclude expression="^static/*">exclude>
    11. filter>
    12. <inotify>
    13. <delete start="true"/>
    14. <createFolder start="true"/>
    15. <createFile start="true"/>
    16. <closeWrite start="true"/>
    17. <moveFrom start="true"/>
    18. <moveTo start="true"/>
    19. <attrib start="false"/>
    20. <modify start="false"/>
    21. inotify>
    22. <sersync>
    23. <localpath watch="/data/service/Automatic-deployment/Qyun-10.0.8.59">
    24. <remote ip="10.0.8.70" name="Qyun-10.0.8.59-nginx"/>
    25. localpath>
    26. <rsync>
    27. <commonParams params="-artuz"/>
    28. <auth start="true" users="yukw" passwordfile="/etc/rsync.passwd"/>
    29. <userDefinedPort start="false" port="874"/>
    30. <timeout start="false" time="100"/>
    31. <ssh start="false"/>
    32. rsync>
    33. <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/>
    34. <crontab start="false" schedule="600">
    35. <crontabfilter start="false">
    36. <exclude expression="*.php">exclude>
    37. <exclude expression="info/*">exclude>
    38. crontabfilter>
    39. crontab>
    40. <plugin start="false" name="command"/>
    41. sersync>
    42. <plugin name="command">
    43. <param prefix="/bin/sh" suffix="" ignoreError="true"/>
    44. <filter start="false">
    45. <include expression="(.*)\.php"/>
    46. <include expression="(.*)\.sh"/>
    47. filter>
    48. plugin>
    49. <plugin name="socket">
    50. <localpath watch="/opt/tongbu">
    51. <deshost ip="192.168.138.20" port="8009"/>
    52. localpath>
    53. plugin>
    54. <plugin name="refreshCDN">
    55. <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
    56. <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
    57. <sendurl base="http://pic.xoyo.com/cms"/>
    58. <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
    59. localpath>
    60. plugin>
    61. head>

    3、创建密码文件(仅需要配置密码就好了,和服务端的密码一致)

    1. # vim /etc/rsync.passwd
    2. 123
    3. # chmod 600 /etc/rsync.passwd

    4、启动sersync

    1. # sersync -dro /data/service/sersync/confxml.xml
    2. set the system param
    3. execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
    4. execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
    5. parse the command param
    6. option: -d run as a daemon
    7. option: -r rsync all the local files to the remote servers before the sersync work
    8. option: -o config xml name: /data/service/sersync/confxml.xml
    9. daemon thread num: 10
    10. parse xml config file
    11. host ip : localhost host port: 8008
    12. will ignore the inotify createFile event
    13. daemon start,sersync run behind the console
    14. use rsync password-file :
    15. user is yukw
    16. passwordfile is /etc/rsync.passwd
    17. config xml parse success
    18. please set /etc/rsyncd.conf max connections=0 Manually
    19. sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
    20. Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
    21. please according your cpu ,use -n param to adjust the cpu rate
    22. ------------------------------------------
    23. rsync the directory recursivly to the remote servers once
    24. working please wait...
    25. execute command: cd /data/test && rsync -artuz -R --delete ./ yukw@10.0.8.70::Qyun-192.168.1.2-nginx --password-file=/etc/rsync.passwd >/dev/null 2>&1
    26. run the sersync:
    27. watch path is: /data/service/Automatic-deployment/Qyun-10.0.8.59

    参数说明

    参数-d:启用守护进程模式
    参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
    参数-n:指定开启守护线程的数量,默认为10个
    参数-o:指定配置文件,默认使用confxml.xml文件
    参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
    参数-m:单独启用其他模块,使用 -m socket 开启socket模块
    参数-m:单独启用其他模块,使用 -m http 开启http模块
    不加-m参数,则默认执行同步程序

    四、在服务端验证是否推送成功

    1. # data/rsync-backup/Qyun-192.168.1.2-nginx
    2. # ll

    五、如果还要同步另外一个目录文件,需要将config.xml拷贝一份,重命名后,修改端口、同步路径、模块名称就可以了

    1. # cp confxml.xml confxml-1.xml
    2. # vim confxml-1.xml
    3. 修改端口为8009、修改同步路径、修改模块名称
    4. 3 "localhost" port="8009">
    5. 24 "/data/service/Automatic-deployment/Qyun-10.0.8.59">
    6. 25 "10.0.8.70" name="Qyun-10.0.8.59"/>
    7. # 启动服务
    8. # sersync -dro /data/service/sersync/confxml-1.xml

    注意事项:

    • 服务端 rsyncd.conf 模块中的path目录权限必须和 uid一样
    • 客户端如果要以非root用户启动sersync,那么 同样要修改 rsync.passwd文件的所有者,以及给600权限

    附加内容:

          关于inotify的一些操作,它的功能可以监控一些目录下文件的变更,通过 -e 参数指定事件类型,如果创建、删除、修改等

    开启两个窗口:
    窗口1:inotifywait -mrq --timefmt "%F%T" --format "%T %w%f %e" -e create,delete,modify /data/test/  执行后窗口会hang住
    窗口2:在 /data/test 目录下新建、删除、修改文件或者目录

    这时在窗口1 就会有实时的事件日志打印出来

    1. # yum install inotify* -y
    2. # inotifywait -mrq --timefmt "%F%T" --format "%T %w%f %e" -e create,delete,modify /data/test/
    3. 2022-08-2414:10:34 /data/test/user02.txt MODIFY
    4. 2022-08-2414:10:41 /data/test/user03.txt MODIFY
    5. 2022-08-2414:10:47 /data/test/cc.txt CREATE
    6. 2022-08-2414:10:57 /data/test/bb CREATE,ISDIR

    好了,这就是rsync+sersync实时同步文件的方法了,如有问题可与博主一起交流讨论 

  • 相关阅读:
    Chapter04 python输入输出-文件读取
    数字化智慧公厕:开创城市数智化治理新时代
    Nodejs 第五十章(lua的基本使用)
    【英语考研词汇训练营】Day 15 —— analyst,general,avoid,surveillance,compared
    有意识的神经网络之对比网络层和后意识层
    吐血整理,服务端性能测试中间件-项目集成redis实战,一篇打通...
    Python实现贝叶斯岭回归模型(BayesianRidge算法)并使用K折交叉验证进行模型评估项目实战
    .Net Web项目创建比较不错的参考文章
    Git patch的使用方法和场景
    第三章:最新版零基础学习 PYTHON 教程(第六节 - Python 运算符—Python 中的赋值运算符)
  • 原文地址:https://blog.csdn.net/qq_35663625/article/details/126519908