HOME 生活记录运维/编程
作者/来源:yixinu.com
栏目:运维/编程
日期:2013-06-10 18:10:57

将服务器

host1 的 /usr/local/nginx-1.2.1/html  目录及目录下的所有文件实时同步到

host2 的 /usr/local/nginx-1.2.1/html  目录下


1、安装

host1

ip:202.202.100.101

需要安装  inotify-tools,安装rsync

root@web:~# cd /usr/local/src/
root@web:/usr/local/src# tar -xzvf inotify-tools-3.14.tar.gz
......
root@web:/usr/local/src# cd inotify-tools-3.14/
root@web:/usr/local/src/inotify-tools-3.14# ./configure
root@web:/usr/local/src/inotify-tools-3.14# make; make install
# 安装完成后会生成inotifywait和inotifywatch两个指令
root@web:/usr/local/src/inotify-tools-3.14# ll /usr/local/bin/inotifywa*
-rwxr-xr-x 1 root root 44972 2013-01-24 10:11 /usr/local/bin/inotifywait*
-rwxr-xr-x 1 root root 45594 2013-01-24 10:11 /usr/local/bin/inotifywatch*


[root@vserver2 src]# tar -xzvf rsync-3.0.9.tar.gz
[root@vserver2 src]# cd rsync-3.0.9
[root@vserver2 src]# ./configure
[root@vserver2 src]# make && make install


###### host1的配置 #############

在主机host1中编辑脚本 inotify.sh

#!/bin/bash
host=202.202.100.102
user=backup
src=/usr/local/nginx-1.2.1/html
 
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src | while read files
do
        /usr/local/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user@$host::webfile
done
编辑密码文件 /etc/server.pass
[root@host1 html]# cat /etc/server.pass
rsyncaaaa
end
改权限为 600
[root@host1 html]# chmod 600 /etc/server.pass
 
启动 inotify.sh 脚本
[root@host1 html]# ~/inotify.sh &



host2

ip:202.202.100.102

需要安装  rsync ,无需安装 inotify-tools


#!/bin/bash
 
cat <<end >>/etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsync.log
 
[webfile]
path = /usr/local/nginx-1.2.1/html
comment = web file
ignore errors
read only = no
write only = no
hosts allow = *
hosts deny = 
list = false
uid = root
gid = root
auth users = backup
secrets file = /etc/server.pass
end
 
cat <<end >>/etc/server.pass
backup:rsyncaaaa
end
 
chmod 600 /etc/server.pass
 
rsync --daemon


#####   配置完成  ######

分享到:

Copyright © 2013-2014 yixinu.com 湘ICP备14004402号

QQ:316686606  Email: 316686606@qq.com