Rsync + sersync

来自Linux78|wiki
Bob讨论 | 贡献2019年12月4日 (三) 14:38的版本 (创建页面,内容为“rsync 介绍 rsync 是 linux 下同步文件的一个高效算法,用于同步更新两处计算机的文件和目录,并适当利用查找文件中的不同块…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

rsync 介绍

rsync 是 linux 下同步文件的一个高效算法,用于同步更新两处计算机的文件和目录,并适当利用查找文件中的不同块以减少数据传输。rsync 的主要特点就是增量传输,只对变更的部分进行传输。


sersync 介绍 sersync 利用 inotify 与 rsync 对服务器进行实时同步,其中 inotify 用于监控文件系统事件,其优点是只对文件不同的部分进行操作,所以其优势大大超过使用挂接文件系统的方式进行镜像同步。

使用服务器 A 和服务器 B 进行介绍,目的是将 A 服务器指定的目录自动同步到 B 服务器指定的目录

yum -y install rsync
systemctl start rsyncd.service 
systemctl enable rsyncd.service  

编辑 B 服务器 rsync 配置文件:

vim /etc/rsyncd.conf

log file = /var/log/rsyncd.log #日志文件位置,启动rsync后自动产生这个文件,无需提前创建
#pid file = /var/run/rsyncd.pid #pid文件的存放位置
lock file = /var/run/rsync.lock #支持max connections参数的锁文件
secrets file = /etc/rsync.pass #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
uid = root #设置rsync运行权限为root
gid = root #设置rsync运行权限为root
use chroot = no #默认为true,修改为no,增加对目录文件软连接的备份
max connections = 1200 #最大连接数
timeout = 600 #设置超时时间

[book]   #自定义名称
path = /data/book/  #rsync服务端数据目录路径
comment = book  #模块名称与[book]自定义名称相同
port=873  #默认端口
read only = no  #设置rsync服务端文件为读写权限
list = no #不显示rsync服务端资源列表
auth users = bookuser  #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = A服务器IP  #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.0.1 #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开 

创建用户认证文件

vi /etc/rsync.pass
bookuser:123456

设置文件权限

chmod 600 /etc/rsyncd.conf  #设置文件所有者读取、写入权限
chmod 600 /etc/rsync.pass  #设置文件所有者读取、写入权限

重启 rsync

systemctl restart rsyncd

A 服务器安装 rsync 参照 B 服务器安装 rsyncA 服务器创建密码认证文件

vi /etc/passwd.txt  #编辑文件,添加以下内容

123456 #密码 ,B服务器里设置的密码

chmod 600 /etc/passwd.txt #设置文件权限,只设置文件所有者具有读取、写入权限即可


到此,rsync 已经配置完毕测试 A、B 服务器之间同步

rsync -avH --port=873 --progress --delete  /www/data/  bookuser@B服务器IP::book --password-file=/etc/passwd.txt  #在A服务器下运行

serync

http://code.google.com/p/sersync/ 解压文件

tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz  #解压
mv GNU-Linux-x86  /usr/local/sersync  #移动目录到/usr/local/sersync


修改 inotify 默认参数(inotify 默认内核参数值太小)

vi /etc/sysctl.conf #添加以下代码

fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535

配置 sersync

cd  /usr/local/sersync #进入sersync安装目录
cp confxml.xml confxml.xml-back  #备份原文件

 vi 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="false"/>
       <filter start="false">
           <exclude expression="(.*)\.svn"></exclude>
           <exclude expression="(.*)\.gz"></exclude>
           <exclude expression="^info/*"></exclude>
           <exclude expression="^static/*"></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="/data/book">
               <remote ip="192.168.1.200" name="book"/>
           </localpath>
           <rsync>
               <commonParams params="-artuz"/>
               <auth start="true" users="bookuser" passwordfile="/etc/passwd.txt"/>
               <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="true" schedule="600">
               <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>
       <plugin name="socket">
           <localpath watch="/data/book">
               <deshost ip="192.168.138.20" port="8009"/>
           </localpath>
       </plugin>
       <plugin name="refreshCDN">
           <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
               <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
               <sendurl base="http://pic.xoyo.com/cms"/>
               <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
           </localpath>
       </plugin>
   </head>


参数说明:

<localpath watch="/data/book">  #源服务器同步目录
<remote ip="192.168.1.200" name="book"/> 
remote ip="23.225.218.182":  #目标服务器ip,每行一个 
name="book":   #目标服务器rsync同步目录模块名称
<auth start="true" users="bookuser" passwordfile="/etc/passwd.txt"/>
start="true"  #设置为true,每隔600分钟执行一次全盘同步
users="bookuser": #目标服务器rsync同步用户名
passwordfile="/etc/passwd.txt": #目标服务器rsync同步用户的密码在源服务器的存放路径
failLog path="/tmp/rsync_fail_log.sh"  #脚本运行失败日志记录


手动测试运行同步命令

/usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/confxml.xml


设置 sersync 监控开机自动执行

vi /etc/rc.d/rc.local  #编辑,在最后添加一行

/usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/confxml.xml  #设置开机自动运行脚本


至此,文件自动同步部署完成rsync 常见问题及解决办法 附录:rsync 常见问题及解决办法(IP 以 10.10.10.10 代替) 错误一: password file must not be other-accessible continuing without password file Password: rsync客户端路径是否写错,权限设置不对,需要再次输入密码,客户端和服务端的密码文件都应该是600的权限才可以

错误二: @ERROR: Unknown module ‘bak’ rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver= 3.0.3] 服务端server的配置中的[bak]名字和客户端client的10.10.10.10::bak不符

错误三: rsync: failed to connect to 10.10.10.10: Connection timed out (110) rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.6] 检查服务端server服务是否正常启动,检查端口防火墙,iptables打开873端口 如果服务端是windows server则在防火墙入站规则中增加873端口 如果服务端是Linux则先检查服务是否启动#ps aux | grep rsync 然后开启873端口#iptables -A INPUT -p tcp --dport 873 -j ACCEPT开启873端口 附: 安装rsync yum install rsync 启动服务/usr/bin/rsync --daemon 启动服务错误failed to create pid file /var/rsyncd.pid: File exists 看看提示服务错误的路径(这个路径不一定就是这个,看自己的报错路径)这里是/var/rsyncd.pid所以 rm -rf /var/rsyncd.pid;再重新启动Rsync服务 此时在看一下ps aux | grep rsync启动成功

错误四: @ERROR: access denied to gmz88down from unknown (10.10.10.10) rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6] 看看是不是服务端server hosts allow限制了IP,把这里的IP加入到服务端server的hosts allow白名单中,windows rsync不能写多个allow,可以在一个allow中加多个IP,例:hosts allow=10.10.10.10 20.20.20.20

错误五: @ERROR: chdir failed rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6] 服务端server的目录不存在或者没有权限(要同步的那个文件路径),安装windows rsync时候会创建一个SvcCWRSYNC用户,这个用户对要拷贝的目录没有权限,方法一,将这个用户给权限加入到目录中,方法二,修改这个用户隶属于的组,修改后要在管理中重启服务

错误六: rsync error: error starting clie nt-server protocol (code 5) at main.c(1524) [Receiver= 3.0.7 ] /etc/rsyncd.conf配置文件内容有错误,检查下配置文件

错误七: rsync: ch own "" failed: Invalid argument (22) 权限无法复制,去掉同步权限的参数即可

错误八: @ERROR: auth failed on module bak rsync error: error starting client-server protocol (code 5) at main.c(1530) [receiver=3.0.6] 密码错误或服务器上是否有bak模块

错误九: rsync: connection unexpectedly closed (5 bytes received so far) [sender] rsync error: error in rsync protocol data stream (code 12) at io.c(600) [sender=3.0.6] 模块read only = no设置为no false

错误十: @ERROR: invalid uid nobody rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6] 设置 uid =0 gid = 0

错误十一: rsync: failed to connect to 10.10.10.10: No route to host (113) rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.6] 防火墙原因

错误十二: rsync: read error: Connection reset by peer (104) rsync error: error in rsync protocol data stream (code 12) at io.c(759) [receiver=3.0.6] /etc/rsyncd.conf配置文件不存在

错误十三: rsync: Failed to exec ssh: No such file or directory (2) rsync error: error in IPC code (code 14) at pipe.c(84) [receiver=3.0.6] rsync: connection unexpectedly closed (0 bytes received so far) [receiver] rsync error: error in IPC code (code 14) at io.c(600) [receiver=3.0.6] 需要在客户端安装yum install -y openssh-clients即