步骤一:准备网页环境
1)在svr5上,启用httpd网站服务、部署测试网页
[root@svr5 ~]# yum -y install httpd
.. ..
[root@svr5 ~]# service httpd restart
停止 httpd: [确定]
正在启动 httpd: [确定]
[root@svr5 ~]# chkconfig httpd on
[root@svr5 ~]# echo "Welcome to Tarena" > /var/www/html/index.html
[root@svr5 ~]# elinks -dump http://192.168.4.5 //访问测试网页
Welcome to Tarena
2)在pc205上,启用httpd网站服务,先不用部署网页
[root@pc205 ~]# yum -y install httpd
.. ..
[root@pc205 ~]# service httpd restart
停止 httpd: [确定]
正在启动 httpd: [确定]
[root@pc205 ~]# chkconfig httpd on
[root@pc205 ~]# ls /var/www/html/* //网页目录为空
ls: 无法访问/var/www/html/*: 没有那个文件或目录
步骤二:配置、启用实时同步脚本
1)在svr5上,安装inotify-tools工具包
[root@svr5 ~]# tar xf inotify-tools-3.13.tar.gz
[root@svr5 ~]# cd inotify-tools-3.13
[root@svr5 inotify-tools-3.13]# ./configure
.. ..
[root@svr5 ~]# make && make install
2)创建并部署SSH公钥,实现免密码验证
[root@svr5 ~]# ssh-keygen //创建密钥对
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): //回车
Enter passphrase (empty for no passphrase): //回车
Enter same passphrase again: //回车
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
.. ..
[root@svr5 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.4.205
//上传公钥
root@192.168.4.205's password: //验证对方密码
Now try logging into the machine, with "ssh 'root@192.168.4.205'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root@svr5 ~]# ssh root@192.168.4.205 //验证免密码登录
Last login: Thu Dec 24 00:53:00 2015 from 192.168.4.5
[root@pc205 ~]# exit //返回客户机
logout
Connection to 192.168.4.205 closed.
3)建立inotify实时同步脚本文件
为了方便脚本的移植使用,在脚本中定义了两个变量:TARGET_DIR用来指定监控的目标文件夹,而RSYNC_CMD用来指定需要触发的同步操作。注意给脚本添加x执行权限,实际使用时根据需要变更这两个变量的值即可
[root@svr5 ~]# vim /root/isync.sh //新建脚本
#!/bin/bash
TARGET_DIR="/var/www/html" #//指定监控目录
RSYNC_CMD="rsync -az --delete /var/www/html/ 192.168.4.205:/var/www/html/"
#//指定同步操作
inotifywait -mrq -e modify,move,create,delete,attrib /opt | while read -n5 X
do
$RSYNC_CMD
done &
[root@svr5 ~]# chmod +x /root/isync.sh //添加执行权限
4)启动实时同步脚本程序
此脚本一旦运行后,会一直在后台运行;如果有必要,可以将此脚本添加为开机自启动任务。
[root@svr5 ~]# /root/isync.sh //执行脚本
[root@svr5 ~]#
步骤三:测试实时同步效果
1)在svr5上向/var/www/html/目录下添加一个文件
[root@svr5 ~]# touch /var/www/html/a.html
[root@svr5 ~]# ls -lh /var/www/html/*.html
-rw-r--r--. 1 root root 0 12月 17 09:02 /var/www/html/a.html
-rw-r--r--. 1 root root 18 12月 17 08:37 /var/www/html/index.html
2)在pc205上观察/var/www/html目录下的变化
[root@pc205 ~]# ls -lh /var/www/html/*.html
-rw-r--r--. 1 root root 0 12月 17 09:02 /var/www/html/a.html
-rw-r--r--. 1 root root 18 12月 17 08:37 /var/www/html/index.html
[root@pc205 ~]#
3)在svr5上删除刚添加的文件a.html
[root@svr5 ~]# rm -rf /var/www/html/a.html
[root@svr5 ~]# ls -lh /var/www/html/*.html
-rw-r--r--. 1 root root 18 12月 17 08:37 /var/www/html/index.html
4)在pc205上再次观察/var/www/html目录下的变化
[root@pc205 ~]# ls -lh /var/www/html/*.html
-rw-r--r--. 1 root root 18 12月 17 08:37 /var/www/html/index.html
[root@pc205 ~]#