最近做的项目需要把生成的静态页面同步到静态页面服务器上,采用rsync+inotify实现目
录实时同步当然就成了很好的方式了。
环境:ubuntu-9.01
生成静态页面的机器作为客户端采用上传的方式,地址为:192.168.18.12
静态页面服务器作为服务器端,地址为:192.168.18.14
同步目录为:/usr/ROOT
rysnc的下载地址为:http://rsync.samba.org/目前最新的版本为:3.0.7
inotify下载地址为:http://inotify-tools.sourceforge.net/最新版本为:3.13
服务器端安装:
tar -zxvf rsync-3.0.7.tar.gz
./configure --prefix=/usr/local/rsync --指定安装目录
make
make install
配置:1、vi /etc/rsyncd.conf
motd file=/etc/rsyncd.conf
read only=no ----因为是上传所以这里一定要可写
list=yes
uid=root
gid=root
hosts allow=192.168.18.12 ---允许连接ip
host deny=19.168.18.100
max connections=2 ----允许最大连接
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsync.lock
模块
[web]
comment=from 192.168.18.12
path=/usr/ROOT
auth users=allen
secrets file=/etc/rsync.pass ----密码文件
2、vi /etc/rsyncd.motd
welcome to use rsync server!
3、vi /etc/rsync.pass
allen:123456
给密码文件赋权:chmod 400 /etc/rsync.pass
4、vi /etc/services
在最后加入:
rsync 873/tcp
rsync 873/udp
5、vi /etc/xinetd.d/rsync
service{
disable=no
socket_type=staream
wait=no
user=root
server=/usr/local/rsync/bin/rsync
server_args=--daemon
log_on_failure+=USERID
}
到这里配置完成,启动:/usr/local/rsync/bin/rsync --daemon
ps -aux | grep rsync 查看是否启动成功!
客户端安装:
tar -zxvf rsync-3.0.7.tar.gz
cd rsync-3.0.7
./configure --prefix=/usr/local/rsync --指定安装目录
make
make install
客户端只需安装,无需配置,建个密码文件
vi /etc/rsync-client.pass
123456
赋权:chmod 600 /etc/rsync-client.pass
rsync的上传和下载命令:
cd /usr/local/rsync/bin
下载命令:
./rsync -vzrtopg --progress --delete /usr/ROOT
上传命令:
./rsync -vzrtopg --progress --delete /usr/ROOT
在加上定时执行,就可以定时同步文件了。若需要实时同步则需要用inotify,安装之前需查看系统内核:uname -a
只能在高于2.6.13版本的系统上安装,以下的需要安装补丁。
inotify安装:inotify-tool-3.13
tar -zxvf inotify-tool-3.13
cd inotify-tool-3.13
./configure
make
make install
建个执行文件:rsync.sh
#!/bin/sh
src=/usr/ROOT
/usr/local/bin/inotifywait -mrq --timefmt '%d%m%y %H:%M' --format '%T%w%f' \
-e modify,delete,move,create,attrib \
${src}\
|while read file
do
rsync -vartopg --progress –delete –password-file=/etc/rsync-client.pass /usr/ROOT/ allen@192.168.18.14::web
echo "success!"
done
这样就能检测到是否有静态页面的生成或修改、删除而进行自动同步,实现实时同步。
--------------------可能出现的错误解惑
1、@ERROR: Unknown module 'web'
联系客服