rsync+sersync实现文件实时同步

server作为备份服务器,rsyncd,客户端client使用工具连接

rsync 的服务端配置(rsyncd.conf)仅对服务器端的 rsyncd 服务 生效,必须在服务器端重启服务才能加载新配置。客户端的 rsyncd 服务与当前连接的服务器端无关,重启客户端服务既不会让服务器端配置生效,也不影响客户端用rsync命令发起连接(客户端只需安装 rsync 工具,无需运行 rsyncd 服务)。

改服务端配置,就重启服务端的 rsyncd;客户端不需要运行 rsyncd 服务,也不用重启

复制代码
 #安装软件包
 [root@server ~ 10:14:10]# yum install -y rsync
 ​
 # 准备同步目录,该目录任何用户都可以读写。
 [root@server ~ 08:43:26]#  mkdir -m 777 /backup
 # 配置rsync,不验证用户,直接同步
 [root@server ~ 10:11:06]# vim /etc/rsyncd.conf
 ​
 #vim添加命令行
 # 添加如下配置
 [backup]
 # 描述信息
 comment = backup
 # 备份路径
 path = /backup
 # 设置可写
 read only = no
 # 指定用户名
 auth users = rsync
 # 指定用户密码文件
 secrets file = /etc/rsyncd.secrets
 ​
 #编辑root权限
 [root@server ~ 10:26:58]# vim /etc/rsyncd.conf
 uid=root
 gid=root
 #重启
 [root@server ~ 10:29:23]# systemctl restart rsyncd --now
 ​
 # 创建用户凭据文件
 [root@server ~ 10:13:16]# echo 'rsync:redhat' > /etc/rsyncd.secrets
 [root@server ~ 10:13:35]# chmod 400 /etc/rsyncd.secrets
 ​
 # 启用并启动rsyncd服务
 [root@server ~ 10:13:43]# systemctl enable rsyncd --now
 Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
 ​
 # 配置防火墙
 [root@server ~ 10:13:50]# firewall-cmd --add-service=rsyncd
 success
 [root@server ~ 10:14:01]#  firewall-cmd --add-service=rsyncd --permanent
 success
 ​
 #关闭seLinux
 [root@server ~ 10:28:59]# setenforce 0

客户端配置和测试

复制代码
 # 准备密码文件
 [root@client ~ 09:21:27]# echo redhat > rsyncd.secrets
 [root@client ~ 10:20:48]# chmod 400 rsyncd.secrets
 ​
 ​
 ​
 # 传输测试
 [root@client ~ 10:30:04]# rsync -av --password-file=./rsyncd.secrets /etc/hostname rsync@server::backup
 sending incremental file list
 hostname
 ​
 sent 110 bytes  received 35 bytes  290.00 bytes/sec
 total size is 17  speedup is 0.12
 ​
 ​
 # 验证结果
 [root@server ~ 10:32:35]# ls /backup/
 hostname
 ​

安装软件包

复制代码
 # 下载软件
 [root@client ~]# wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
 #或者将sersync2.5.4_64bit_binary_stable_final.tar.gz拖入窗口安装
 [root@client ~ 10:41:09]# rz -E
 rz waiting to receive.
 ​
 # 解压文件
 [root@client ~ 10:54:48]# tar xf sersync2.5.4_64bit_binary_stable_final.t
配置文件
复制代码
 [root@client ~ 10:57:18]# vim confxml.xml
 <?xml version="1.0" encoding="ISO-8859-1"?>
 <head version="2.5">
     <host hostip="localhost" port="8008"></host>
     <debug start="false"/>
     <fileSystem xfs="true"/>
     <filter start="true">
         <exclude expression="^cache/*"></exclude>
     </filter>
     <inotify>
         <delete start="true"/>
         <createFolder start="true"/>
         <createFile start="false"/>
         <closeWrite start="true"/>
         <moveFrom start="true"/>
         <moveTo start="true"/>
         <attrib start="false"/>
         <modify start="false"/>
     </inotify>
     <sersync>
         <localpath watch="/app_data">
             <remote ip="10.1.8.10" name="backup"/>
         </localpath>
         <rsync>
             <commonParams params="-azP"/>
             <auth start="true" users="rsync" passwordfile="/root/rsyncd.secrets"/>
             <userDefinedPort start="false" port="873"/>
             <timeout start="false" time="100"/>
             <ssh start="false"/>
         </rsync>
         <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/>
         <crontab start="false" schedule="600"><!--600mins-->
             <crontabfilter start="false">
                 <exclude expression="*.php"></exclude>
                 <exclude expression="info/*"></exclude>
             </crontabfilter>
         </crontab>
         <plugin start="false" name="command"/>
     </sersync>
     <plugin name="command">
         <param prefix="/bin/sh" suffix="" ignoreError="true"/>
         <filter start="false">
             <include expression="(.*)\.php"/>
             <include expression="(.*)\.sh"/>
         </filter>
     </plugin>
 </head>

运行Sersync

复制代码
 [root@client ~ 11:01:20]# cp GNU-Linux-x86/sersync2 /usr/local/bin/
 [root@client ~ 11:02:24]# sersync2 -h
 set the system param
 execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
 execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
 parse the command param
 _______________________________________________________
 参数-d:启用守护进程模式
 参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
 c参数-n: 指定开启守护线程的数量,默认为10个
 参数-o:指定配置文件,默认使用confxml.xml文件
 参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
 参数-m:单独启用其他模块,使用 -m socket 开启socket模块
 参数-m:单独启用其他模块,使用 -m http 开启http模块
 不加-m参数,则默认执行同步程序
 _____________________________________________________________
 ​
 # 运行 Sersync,-d 后台运行
 [root@client ~ 11:02:35]# sersync2 -o confxml.xml -d
 set the system param
 execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
 execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
 parse the command param
 option: -o  config xml name:  confxml.xml
 option: -d  run as a daemon
 daemon thread num: 10
 parse xml config file
 host ip : localhost host port: 8008
 now the filter work ,if you set the crontab,you have to set crontab filter 
 WARNING XFS FILE SYSTEM WORK
 daemon start,sersync run behind the console 
 use rsync password-file :
 user is rsync
 passwordfile is     /root/rsyncd.secrets
 config xml parse success
 please set /etc/rsyncd.conf max connections=0 Manually
 sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) 
 Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
 please according your cpu ,use -n param to adjust the cpu rate
 run the sersync: 
 watch path is: /app_data

测试文件实时同步

复制代码
 # server 端监控目录 /backup
 [root@server ~ 10:41:27]# watch -n 1 ls /backup
 ​
 #在客户端创建文件和目录
 [root@client ~ 11:03:42]# echo hello world > /app_data/welcome.txt
 [root@client ~ 11:04:22]# mkdir /app_data/dbdata
 ​
 # 客户端删除文件
 [root@client ~ 11:04:37]# rm -fr /app_data/*
复制代码
 [root@client ~ 11:04:55]# cp confxml.xml /etc/sersyncd.conf
 [root@client ~ 11:23:24]# vim /etc/systemd/system/sersyncd.service
 [Unit]
 Description=SerSync server daemon
 ​
 [Service]
 Type=forking
 ExecStart=/usr/local/bin/sersync2 -o /etc/sersyncd.conf -d
 ​
 [Install]
 WantedBy=multi-user.target
 [root@client ~ 11:24:22]# systemctl daemon-reload 
 [root@client ~ 11:24:27]# systemctl enable sersyncd.service
 Created symlink from /etc/systemd/system/multi-user.target.wants/sersyncd.service to /etc/systemd/system/sersyncd.service.
 ​
相关推荐
hello 早上好40 分钟前
负载均衡、算法/策略
运维·算法·负载均衡
林开落L1 小时前
进程控制:从创建到终结的完整指南
linux·性能优化·进程控制
21号 11 小时前
10. NAT,代理服务,内网穿透
linux·服务器·网络
一匹电信狗1 小时前
【Linux我做主】进程优先级
linux·运维·服务器·c++·ubuntu·小程序·unix
一只小松许️5 小时前
Linux性能分析与内存调试:perf和Valgrind高级使用指南
linux
巴里巴气7 小时前
zookeeper基本概念介绍
linux·分布式·zookeeper
codu4u13148 小时前
Maven中的bom和父依赖
java·linux·maven
IT成长日记9 小时前
【自动化运维神器Ansible】Ansible常用模块之hostname模块详解
运维·自动化·ansible·hostname·常用模块
lixzest10 小时前
Linux 中,命令查看系统版本和内核信息
linux·运维·服务器
朱小弟cs610 小时前
Orange的运维学习日记--16.Linux时间管理
linux·运维·学习