服务源:192.168.1.101
发起端:192.168.1.102

服务端: hostnamectl set-hostname rsyncd
发起端: hostnamectl set-hostname client

rpm -q rsync #测试是否安装rsync
--------------------配置rsync源服务-------------------------
vim /etc/rsyncd.conf

uid = nobody
gid = nobody            #设置为匿名用户
use chroot = yes        #限制用户登录目录
pid file = /var/run/rsyncd.pid  #生成的进程文件
#以下格式的文件不进行压缩
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
address = 192.168.1.101 #监听地址
port 873        #指定端口
log file = /var/log/rsyncd.log  #指定日志路径

[demo]  #共享名称,自行定义
path = /var/www/html
comment =
www.demo.com
read only = yes #只读模式
auth users = backuper
secrets file = /etc/rsyncd_users.db

# max connections = 4   #最大连接数
-------------------------------上面是注释解释-----------------
uid = nobody
gid = nobody
use chroot = yes
pid file = /var/run/rsyncd.pid
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
address = 192.168.1.101
port 873
log file = /var/log/rsyncd.log

[demo]
path = /var/www/html
comment =
www.demo.com
read only = yes
auth users = backuper
secrets file = /etc/rsyncd_users.db

####################配置同步用户密码###############
vim /etc/rsyncd_users.db

backuper:123 #名称要跟主配置文件当中的用户名一致

chmod 600 /etc/rsyncd_users.db
rm -rf /var/run/rsyncd.pid
rsync --daemon

yum -y install httpd #yum安装Apache
chmod -R 777 /var/www/html
systemctl stop firewalld.service
-------------------------------------发起端---------------------------

格式一:
rsync -avz [email protected]::wwwroot /var/www/html

格式二:
rsync -avz raync://[email protected]/wwwroot /var/www/html


-------------------------开启面交互-----------------------------------
vim /etc/server.pass

chmod 600 /etc/server.pass

#免交互
rsync -az --delete --password-file=/etc/server.pass [email protected]::demo /opt



------------------------------------rsync+inotify--------------------

注意:源端设置:read only = no

vim /etc/sysctl.conf

fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

sysctl -p

tar zxvf inotify-tools-3.14.tar.gz

cd inotify-tools-3.14

./configure

make && make install

inotifywait -mrq -e modify,create,move,delete /var/www/html/ #监控同步的状态

chmod -R 777 /var/www/html/

另外在开启一个新的总端设置


vim inotify.sh

#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ [email protected]::demo/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
 if [ $(pgrep rsync | wc -l) -le 0 ];then
$RSYNC_CMD
 fi
done

chmod -R  777 /var/www/html/
//一定注意两边的同步目录权限都设置777

vim /etc/rsyncd.conf

read only = no

kill -9 14992

rsync --daemon