一、sersync
理论
sersync=inotify+rsync
inotify有监控的功能,而rsync有同步的功能,那么sersync这个软件的功能就是监控某个目录,当这个目录发生特定的动作时(比如:插入、删除等),就自动触发rsync的推送功能,将变化及时的同步到服务端。
场景:
在客户端上通过sersync
监控/data
目录,有变化时及时同步到服务端的/backup
目录。
客户端的配置
先安装rsync,因为sersync要依赖于rsync,还有inotiry-tools也要安装上
[root@client ~]# yum -y install rsync #任意源都可以
[root@client ~]# yum -y install inotify-tools
然后去github上面把sersync下载下来
https://github.com/wsgzao/sersync
https://github.com/wsgzao/sersync/raw/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
sersync是一个绿色包,解压出来就能用了,不用安装,我们最好将它解压到/var/local/目录里面,tar -xf 解压之后,会生成一个GUI开头的目录,更名为sersync
[root@client sersync]# pwd
/var/local/sersync
[root@client sersync]# ls #conf是配置文件,sersync2是运行文件,编辑一下配置文件
confxml.xml sersync2
[root@client sersync]# vim confxml.xml
[root@client sersync]# cat 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="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify> #所有的操作都改成true
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>
<sersync>
<localpath watch="/data"> #监控哪个目录,/data目录
<remote ip="192.168.80.23" name="data"/> #服务端的IP和服务端模块的名字,而不是目录的名字
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-az"/> #改成az即可
//打开密码认证,用户和密码文件注意不要输入错了,不忘记改passwd的权限600
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.passwd"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- timeout=100 --> #这里打开
<ssh start="false"/>
[root@client sersync]# echo 1 > /etc/rsync.passwd #注意,客户端就直接填写密码就可以了
[root@client sersync]# chmod 600 /etc/rsync.passwd #注意权限问题,密码文件必须是600权限
[root@client sersync]# ./sersync2 -dro confxml.xml #启动
execute command: cd /data && rsync -az -R --delete ./ --timeout=100 rsync_backup@192.168.80.23::data --password-file=/etc/rsync.passwd >/dev/null 2>&1
run the sersync:
watch path is: /data
服务端的配置
[root@server ~]# yum -y install rsync #默认就在后台运行
[root@server ~]# systemctl start rsyncd
cat /etc/rsyncd.conf
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsync.log
[data]
comment = welcome!
path = /data
[root@server ~]# useradd -M -s /sbin/nologin rsync
[root@server ~]# echo "rsync_backup:1" > /etc/rsync.passwd #密码定义为1
[root@server ~]# chmod 600 /etc/rsync.passwd
[root@server ~]# mkdir /data
[root@server ~]# chown -R rsync:rsync /data