网站实时镜像

3.网站实时镜像
问题
公司的网站服务器有两个镜像站点,分别放在北京和上海的IDC机房。现在要求利用rsync同步机制实现“服务器A–>服务器B”的实时镜像同步。
需要完成的配置任务如下:
1)双方的目录均为 /var/www/html/
2)以 svr5 为同步发起方,配置 inotify+rsync 同步操作
3)以 pc205 为同步目标,基于SSH方式进行验证
方案
使用两台RHEL6虚拟机,其中一台作为服务器A(192.168.4.5),另外一台作为服务器B(192.168.4.205),两台主机都安装httpd网站软件,如图-3所示。
在这里插入图片描述
图-3
安装并启用inotify-tools工具,就可以在同步发起端实现对指定目录的监控,一旦出现更改、增加文件等操作,立即触发相应的命令操作(本例中即上行同步)。根据监控结果触发同步操作,其中用到了一部分Shell控制语句,最好建立专用脚本来实现,本例中只需理解脚本的用途即可。
步骤
实现此案例需要按照如下步骤进行。
步骤一:准备网页环境
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 [email protected]
//上传公钥
[email protected]’s password: //验证对方密码
Now try logging into the machine, with “ssh ‘[email protected]’”, and check in:

.ssh/authorized_keys

to make sure we haven’t added extra keys that you weren’t expecting.

[root@svr5 ~]# ssh [email protected] //验证免密码登录
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 ~]#

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章