rsync+sersync

实时同步还可以使用inotify+rsync命令,但是不太方便https://www.cnblogs.com/forlive/p/10769895.html
sersync还可以实现开机自启动

配置rsync守护进程

  1. 安装rsync
yum install rsync
  1. 修改rsync配置文件/etc/rsyncd.conf,默认就是这个文件
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

uid = developer
gid = developer
fake super = yes
use chroot = no
max connections = 200
timeout = 900
ignore nonreadable = yes
read only = false
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
# 虚拟用户,不需要useradd创建
auth users = img
secrets file = /etc/rsync.passwd

# 这块是模块名称
[imgs]
path = /data/portal/backgroundImgs
  1. 添加密码文件/etc/rsync.passwd

文件中格式 用户名:密码

echo "img:img" > /etc/rsync.passwd
  1. 启动rsync进程

rsync启动占用873端口

systemctl start rsyncd

多台机器之间的同步

在其他机器上执行上面同样的步骤,不需要更改任何内容

使用命令测试同步效果

rsync -avz --delete /data/portal/backgroundImgs/ img@sany-2::imgs --password-file=/etc/sersync.passwd

配置sersync

下载地址: https://github.com/wsgzao/sersync

  1. 下载程序包后解压,将二进制程序放到/usr/bin/下
tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
mv sersync2 /usr/bin/sersync
chmod +x /usr/bin/sersync
  1. 编辑配置文件/etc/sersync.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
	<!-- rsync服务端配置 -->
    <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配置段 -->
    <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配置段 -->
    <sersync>
	<!-- 同步的目录 -->
	<localpath watch="/data/portal/backgroundImgs">
		<!-- rsync服务端的IP,和模块名称 -->
	    <remote ip="sany-2" name="imgs"/>
	    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
	    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
	</localpath>
	
	<!-- rsync命令行选项 -->
	<rsync>
	    <commonParams params="-avz"/>
		<!-- 是否开启免密认证模式,users虚拟用户,passwordfile指定客户端密码文件 -->
         <!-- 这条命令结合上面的目录就相当于,这里不需要加上--delete参数,因为sersync在启动时就已经指定了
			密码文件里面仅包含密码,这里和rsync启动时候指定的密码文件不同
			rsync -avz /data/portal/backgroundImgs/ img@sany-2::imgs --password-file=/etc/sersync.passwd
		-->
	    <auth start="true" users="img" passwordfile="/etc/sersync.passwd"/>
	    <userDefinedPort start="false" port="874"/><!-- port=874 -->
	    <timeout start="false" time="100"/><!-- timeout=100 -->
	    <ssh start="false"/>
	</rsync>
    <!-- 日志 -->
	<failLog path="/var/log/sersync.log" timeToExecute="60"/><!--default every 60mins execute once-->
	<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"/>	<!--prefix /opt/tongbu/mmm.sh suffix-->
	<filter start="false">
	    <include expression="(.*)\.php"/>
	    <include expression="(.*)\.sh"/>
	</filter>
    </plugin>

    <plugin name="socket">
	<localpath watch="/opt/tongbu">
	    <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>
  1. 添加密码文件,用于sersync的免密认证,也就是上面配置文件中指定的passwordfile="/etc/sersync.passwd"

密码文件的权限需要为600,密码文件中只需要有密码就行,相当于rsync密码文件的密码部分

echo "img" > /etc/sersync.passwd
chmod 600 /etc/sersync.passwd
  1. 添加启动配置文件 /etc/systemd/system/sersync.service
[Unit]
Description=sersync
Documentation=http://www.xxx.com
After=network.target

[Service]
ExecStart=/usr/bin/sersync -rdo /etc/sersync.xml
Type=forking

[Install]
WantedBy=multi-user.target
  1. 启动sersync
systemctl start sersync
systemctl enable sersync

多台机器之间同步

在其他机器上做上面的同样操作, 只需将/etc/sersync.xml文件中 ip地址对应修改就行了

<remote ip="sany-2" name="imgs"/>
posted @ 2022-12-27 11:53  ForLivetoLearn  阅读(132)  评论(0编辑  收藏  举报