环境:两台Centos7的虚拟机

          服务端ip:192.168.120.12

          客户端ip:192.168.120.13




服务端配置

 

一  安装rsync程序,编辑rsync守护进程配置文件

#yum install rsync -y

#vim /etc/rsyncd.conf

 

各项参数含义可参考帮助手册

#man 5 rsyncd.conf


uid=nobody

gid=nobody

use chroot=no  

max_connections=10 

strict modes=yes

pid file=/var/run/rsyncd.pid

lock file=/var/run/rsync.lock   

log file=/var/log/rsyncd.log

 

[web1]

path=/liferay/

comment= document_library file

ignore errors

read only=no

write only=no

#hosts   allow="SRProdLnxAmp01.*****.local"

hosts allow=192.168.120.13

hosts deny=*

list=false

uid=root

gid=root

auth users=web1

secrets file=/etc/web1.pass

 

[web2]

path=/liferay/

comment= lucene file

ignore errors

read only=no

write only=no

#hosts   allow=SRProdLnxAmp01.*****.local

hosts allow=192.168.120.13

hosts deny=*

list=false

uid=root

gid=root

auth users=web1

secrets file=/etc/web1.pass

 

[web3]

path=/liferay/

comment= customer_html_import

ignore errors

read only=no

write only=no

hosts allow=192.168.120.13

hosts deny=*

list=false

uid=root

gid=root

auth users=web1

secrets file=/etc/web1.pass

   

二   创建用户名/密码,注意这里用户名和密码与系统的无关,可随意指定,但是要和rsyncd.conf中的auth users这一项匹配。

 

#vim /etc/web1.pass


web1:password

 

三 防火墙放开873端口

# firewall-cmd --add-port=873/tcp --permanent

# firewall-cmd --reload

 

四 启动rsync后台进程,设置开机自启。

#rsync  --daemon

#netstat   -lntup | grep  rsync

#echo "/usr/bin/rsync --deamon">>/etc/rc.local

 

客户端配置

 

一  安装rsync和inotify

#yum install rsync

下载inotify的rpm包

#wget http://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/i/inotify-tools-3.14-8.el7.x86_64.rpm

#rpm -Uvh inotify-tools-3.14-8.el7.x86_64.rpm

#yum install inotify-tools

 

二  创建rsync认证文件

#vim /etc/server.pass

 

三  创建脚本

#vim /usr/local/rsync/rsync001.sh

#vim /usr/local/rsync/rsync002.sh

#vim /usr/local/rsync/rsync003.sh

   


#!/bin/bash


host1=SRProdLnxLiferayBackup.****.local

src1=/usr/local/liferay/data/document_library

src2=/usr/local/liferay/data/lucene

dst1=web1

dst2=web2

user1=web1

 

 /usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib ${src1} \

| while read files

      do

          /usr/bin/rsync -vzrtopg  --delete --progress --password-file=/etc/server.pass ${src1}  $user1@$host1::$dst1

          echo "${files} was   rsynced">>/var/log/rsy.log   2>&1

     done

   

四 运行脚本并查看运行状态

 

#sh /usr/local/rsync/rsync001.sh &

#sh /usr/local/rsync/rsync002.sh &

#sh /usr/local/rsync/rsync003.sh &

#ps aux | grep '/usr/local/rsync/rsync'