1. 查看rsync是否安装(系统默认自带)
[root@centos07 ~]# rpm -q rsync
2. 修改rsync主配置文件
[root@centos07 ~]# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes //禁锢在根目录
address = 192.168.100.70 //监听ip地址
prot = 873 //监听端口
[wwwvar] //共享模块名
path = /var/www/html //源目录实际路径
comment = share wwwvar
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 //同步时不再压缩文件类型
read only = yes //是否为只读
auth users = bob //授权账户
secrets file = /etc/rsync.db //存放账户信息数据文件
3. 创建同步根目录
[root@centos07 ~]# mkdir -p /var/www/html
4. 切换为linux光盘
5. 挂载光盘
[root@centos07 ~]# mount /dev/cdrom /mnt
6. 安装inotify
[root@centos07 ~]# tar zxvf /mnt/inotify-tools-3.14.tar.gz -C /usr/src
7. 进入inotify目录,配置inotify
[root@centos07 ~]# cd /usr/src/inotify-tools-3.14/
[root@centos07 inotify-tools-3.14]# ./configure
8. 编译安装inotify
[root@centos07 inotify-tools-3.14]# make && make install
9. 修改内核参数
[root@centos07 ~]# vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384 //队列
fs.inotify.max_user_instances = 1024 //实例
fs.inotify.max_user_watches = 1048576 //最大传输文件数
10. 打印参数查看
[root@centos07 ~]# sysctl -p
11. 编写实时更新
#!/bin/bash
INOTI="inotifywait -mrq -e move,delete,modify,attrib,create /var/www/html" //监控同步文件
RSYNC="rsync -azH --delete /var/www/html/ root@192.168.100.80:/var/www/html/" //进行上行同步
$INOTI | while read DIRECTORY EVENT EILE //检查同步文件是否触发事件
do
$RSYNC //触发事件进行上行同步
done
12.在生成密钥对,将公钥传输到客户端,实现免交互式身份验证
[root@centos08 ~]# ssh-keygen -t RSA
[root@centos08 ~]# ssh-copy-id -i root@192.168.100.80
13. 守护rsync进程,监听端口
[root@centos07 ~]# rsync --daemon
[root@centos07 ~]# netstat -anptu | grep rsync
14. 后台执行脚本文件
[root@centos07 ~]# sh rsync.sh &
15. 在服务器目录创建test.txt文件,
[root@centos07 ~]# echo "aaa" > /var/www/html/a.html
16. 查看客户端目录下是否同步
[root@centos08 ~]# ls /var/www/html/