实验需求:在web服务器上搭建rsync服务,客户端上传网页文件,在客户端写脚本同步web服务器上的网页数据


一.WEB服务器(192.168.100.1)上搭建rsync服务

1.开启服务

#vim /etc/xinetd.d/rsync

……

   disable = no                   //把disable = yes改成no    

……

或者

# chkconfig rsync on


#service xinetd start


2.创建rsync主配置文件

# vim /etc/rsyncd.conf              //默认不存在

uid = root

gid = root

use chroot = yes

pid file = /var/run/rsyncd.pid

log file = /var/log/rsyncd.log


[webfile]

path = /var/www/html              //共享网站根目录

comment = Rsync share test

read only = no                  //需要写入权限一定设置为no


3.重启xinetd服务

# service xinetd restart



二.客户端安装inotify工具inotify-tools,并通过脚本实现rsync触发同步

inotify-tools 是为linux下inotify文件监控工具提供的一套c的开发接口库函数,同时还提供了一系列的命令行工具,这些工具可以用来监控文件系统的事件。inotify-tools是用c编写的,除了要求内核支持inotify外,不依赖于其他。inotify-tools提供两种工具,一是inotifywait,它是用来监控文件或目录的变化,二是inotifywatch,它是用来统计文件系统访问的次数。如果列出的内核版本不低于2.6.13,系统就支持 inotify。还可以检查机器的 /usr/include/sys/inotify.h 文件。如果它存在,表明内核支持 inotify。


1.安装软件

# tar -zxvf inotify-tools-3.14.tar.gz

# cd inotify-tools-3.14

# ./configure 

# make

# make install

# ls /usr/local/bin/inotify*

/usr/local/bin/inotifywait  /usr/local/bin/inotifywatch      //有2个工具,可以通过命令加--help查看使用说明


2.创建网页文件上传目录和认证口令文件

# mkdir /data

# echo 123456 > /root/rsync_pass    //创建认证口令文件并将同步密码写入

# chmod 600 /root/rsync_pass


3.使用rsync+inotifywait工具编写脚本实现触发同步

# vim rsync_webfile.sh 

/bin/bash

host1=192.168.100.1

src=/data/                  //加/只上传data下的文件,不加/连data目录一起上传

dst1=webfile

user1=root

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib  $src\

| while read files

        do

        /usr/bin/rsync -az --delete --password-file\=/root/rsync_pass  $src $user1@$host1::$dst1

                echo "${files} was rsynced" >>/tmp/rsync.log 2>&1

         done           



4.上传网页文件测试

# echo test > /data/test.html

# chmod +x rsync_webfile.sh

# ./rsync_webfile.sh &