基本思路
应用示例
配置文件rsync.conf
rsync账号文件
启用rsync服务
关闭rsync服务
rsync [选项] 原始位置 目标位置
常用选项
格式1:用户名@主机地址::共享模块名
格式:rsync://用户名@主机地址/共享模块名
[root@localhost ~]# rsync -avz
[email protected]::wwwroot /root
[root@localhost ~]# rsync -avz
rsync://[email protected]/wwwroot /root
[root@localhost ~]# mkdir /myweb
[root@localhost ~]# rsync -avzH --delete
[email protected]::wwwroot /myweb
Password:
receiving incremental file list
./
index.html
index.php
......
-------配置rsync源服务器------
hostnamectl set-hostname source
su
iptables -F
setenforce 0
vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
port 873
log file = /var/log/rsync.log
pid file = /var/run/rsyncd.pid
hosts allow = 14.0.0.0/24
[wwwroot]
path = /var/www/html
comment = www.yyc.cn
read only = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
auth users = backuper
secrets file = /etc/rsyncd_users.db
vim /etc/rsyncd_users.db
backuper:19961207
chmod 600 /etc/rsyncd_users.db
##启动服务
rsync --daemon
##查看状态
[root@source etc]# netstat -ntap | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 114655/rsync
tcp6 0 0 :::873 :::* LISTEN 114655/rsync
###################################
--源服务器
yum install httpd -y
cd /var/www/html
vim index.html
this is test web
echo "this is yyc web" > web.html
------发起端-------
格式一:
rsync -avz [email protected]::wwwroot /opt/ ##密码19961207
格式二:
rsync -avz rsync://[email protected]::wwwroot /opt/
#免交互:(慎用!!!用之前做好备份)
vim /etc/server.pass
19961207
chmod 600 /etc/server.pass
rsync -az --delete --password-file=/etc/server.pass [email protected]::wwwroot /opt/
'会把/opt/路径下的所有原有非源文件删了,慎用!'
定期同步的不足
实时同步的优点
[root@localhost ~]# vim /etc/sysctl.conf
......
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576 '监控数应大于监控目标的总文件数'
[root@localhost ~]# sysctl -p
[root@localhost ~]# inotifywait -mrq -e modify,create,move,delete /var/www/html
'-m,持续进行监控'
'-r,递归监控所有子对象'
'-q,简化输出信息'
'-e,指定要监控哪些事件类型'
Setting up watches. Beware:since -r was given,this may take a while!
Watches established.
/var/www/html/ CREATE index.php
/var/www/html/ MODIFY index.php
/var/www/html/ MOVED_FROM index.php
/var/www/html/ MOVED_YO test.php
[root@localhost ~]# vim /opt/inotify_rsync.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]:/var/www/html"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE '读取输出的监控记录'
do
if [ $(pgrep rsync | wc -l) -le 0 ];then
$RSYNC_CMD '如果rsync未在进行,则立即启动'
fi
done
--------------rsync+inotify(发起端)-----------
注意:源端设置:read only = no
yum install httpd -y
yum install gcc gcc-c++ -y ##如已安装则不需要再安装
##将rsync源服务器/var/www/html/路径下所有页面都删掉
#调整inotify内核参数
vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
sysctl -p ##生效
##拖入软件包 inotify-tools-3.14.tar.gz
tar zxvf inotify-tools-3.14.tar.gz -C /opt/
cd /opt/inotify-tools-3.14/
./configure
make && make install
#安装inotify-tools辅助工具
inotifywait -mrq -e modify,create,move,delete /var/www/html
#通过inotifywait触发rsync同步操作
cd /opt/
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]::ww
wroot/"
$INOTIFY_CDM | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
chmod +x inotify.sh
#rsync源服务器和发起端都要加权限
chmod 777 /var/www/html/
./inotify.sh