1.配置rsync同步的远程主机(做网站镜像,基于lamp或者lnmp平台)
一.使用基于SSH的服务方式时,服务器端无需配置rsync,只要安装好openssh-server,丙配置启动sshd服务即可。这种方式可以直接以linux系统用户进行Rsync验证,非常方便和灵活。在RHEL5中,使用自带的openssh-server软件包,按照默认配置即可。基于安全考虑,可以根据需要对配置进行适当调整
[root@www1 ~]# useradd sshuser
[root@www1 ~]# passwd sshuser
[root@www1 ~]# vi /etc/ssh/sshd_config
  39 PermitRootLogin no
  42 AllowUsers sshuser  
  109 UseDNS no
[root@www1 ~]# netstat -anp | grep :22
tcp        0      0 :::22                       :::*                        LISTEN      14726/sshd     
[root@www1 ~]# chkconfig --level 2345 sshd on
………………………………………………………………………………
二.基于rsync的--daemon模式
  使用rsync程序的服务器模式时,需要为rsync程序配置rsyncd.conf文件,并以"--daemon"的选项将rsync运行为服务进程。这种法师可以使用匿名用户,或者基于文本文件的用户数据进行验证。通常用于建立只读模式的下载备份源。(在RHEL5,默认已安装有rsync-2.6.8-3.1)
1.建立rsyncd.conf配置文件(缺省没有该文件,需要手工建立.   man  rsyncd.conf)
[root@www1 ~]# vi /etc/rsyncd.conf 
uid = nobody
gid = nobody
use chroot = yes
address = 192.168.6.34
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.6.0/24
[webroot]
        path = /var/www/html
        comment = Directory for web server
        read only = yes
        dont compress = */gz *.tgz *.zip *.rar *.z *.bz2
        auth users = backuper
        secrets file = /etc/rsyncd_db
2.建立rsync用户数据库,创建一个用于备份操作的用户,并设置好可靠的密码。×该用户必须对备份源目录 拥有读取权限(other),才能进行远程同步。
[root@www1 ~]# vi /etc/rsyncd_db 
backuper:123
[root@www1 ~]# chmod 600 /etc/rsyncd_db 
3.启动rsync服务(若rsync服务需要被频繁使用,建议采用独立守护进程的方式运行。若rsync服务的使用频率不高,则可以交由xinetd超级服务器管理,需安装有xinetd-2.3.14-10.el5.i386.rpm)以节省系统资源。
第一种:独立启动的守护进程
[root@www1 ~]# rsync --daemon 
[root@www1 ~]# netstat -anp | grep rsync
tcp        0      0 192.168.6.34:873            0.0.0.0:*                   LISTEN      5667/rsync          
unix  2      [ ]         DGRAM                    14825  5667/rsync      
[root@www1 ~]# echo "/usr/bin/rsync --daemon &" >> /etc/rc.local
第二种:由xinetd管理的客户进程
[root@www1 ~]# vi /etc/xinetd.d/rsync 

# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable = no
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}
[root@www1 ~]# chkconfig --list rsync
rsync           启用
[root@www1 ~]# service xinetd restart
停止 xinetd:                                              [确定]
启动 xinetd:                                              [确定]
三.使用rsync文件同步工具
区别:当连接open-server服务时,使用一个“冒号”分隔
[root@www2 ~]# rsync -avz sshuser@www1.benet.com:/var/www/html/ /webbak/www
      当连接rsync --daemon服务时,使用“两个冒号”分隔(webroot为远程备份的目录模块名称)
[root@www2 ~]# rsync-avz backuper@www1.benet.com::webroot /webbak/www
或者[root@www2 ~]# rsync -avz rsync://backuper@www1.benet.com/webroot /webbak/www
四.结合NFS和inotify-tool实现实时同步
1.配置NFS发布共享
[root@www2 ~]# mkdir -p /webbak/www1
[root@www2 ~]# vi /etc/exports 
/webbak/www1    192.168.6.34(rw,no_root_squash)
[root@www2 ~]# chkconfig --level 2345 portmap on
[root@www2 ~]# chkconfig --level 2345 nfs on
[root@www2 ~]# service portmap start
启动 portmap:                                             [确定]
[root@www2 ~]# service nfs start
2.配置本地的备份源主机
1)安装inotify-tool
[root@www1 ~]# cd /aaa/
[root@www1 aaa]# ls
inotify-tools-3.14  inotify-tools-3.14.tar.gz
[root@www1 aaa]# tar inotify-tools-3.14.tar.gz 
[root@www1 aaa]# cd inotify-tools-3.14
[root@www1 inotify-tools-3.14]# ./configure ; make ; make install    
2)挂在NFS共享目录
[root@www1 ~]# chkconfig --level 2345 portmap on
[root@www1 ~]# service portmap start
启动 portmap:                                             [确定]
[root@www1 ~]# vi /etc/fstab

/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/VolGroup00/LogVol01 swap                    swap    defaults        0 0
# Beginning of the block added by the VMware software
.host:/                 /mnt/hgfs               vmhgfs  defaults,ttl=5     0 0
# End of the block added by the VMware software
192.168.6.47:/webbak/www1       /media/nfsdir   nfs     defaults 0 0   //添加该行
3)创建挂装目录
[root@www1 ~]# mkdir -p /media/nfsdir
[root@www1 ~]# mount /media/nfsdir/
[root@www1 ~]# df -hT
文件系统      类型    容量  已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
              ext3     19G  4.6G   13G  27% /
/dev/sda1     ext3     99M   12M   82M  13% /boot
tmpfs        tmpfs    252M     0  252M   0% /dev/shm
192.168.6.47:/webbak/www1
               nfs     19G  3.9G   14G  22% /media/nfsdir
4)编写inotify监控及触发同步脚本(监控/var/www/html/目录下的文件修改,创建,移动,删除时间,当出现监控的事件时,立即触发rsync命令执行增量更新)
[root@www1 ~]# vi /opt/rsync.sh
#!/bin/bash
inotifywait -mrq -e modify,create,move,delete /var/www/html/ | while read DIR EVENT FILE
do
        rsync -aHvz --delete /var/www/html/ /media/nfsdir
done
4)执行触发同步脚本
[root@www1 ~]# chmod 700 /opt/rsync.sh 
[root@www1 ~]# echo "/opt/rsync.sh &" >> /etc/rc.local 
[root@www1 ~]# /opt/rsync.sh &                        (fs + 编号   调出后台程序)
[1] 20633

最后测试,发现,当www1对/var/www/html/下进行动作时,立即同步到www2服务器