天天看点

第11天 RSYNC同步功能实现

rsync服务端配置

[root@gw404 confbak]# cat /etc/rsyncd.conf 

uid = rsync

gid = rsync

use chroot = no

max connections = 4

timeout = 100

#syslog facility = local5

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsyncd.lock

log file = /var/log/rsyncd.log

ignore errors

read only = false

list = false

hosts allow = 0.0.0.0/21

hosts deny = 0.0.0.0/32

[gwftp]

 path = /b/confbak

 comment = gw bak.

 auth users = rsync_backup

 secrets file = /etc/rsync.password

[root@gw404 confbak]# cat /etc/rsync.password 

rsync_backup:123123

 客户端同步操作

cat /etc/rsync.pass 

123123

客户端同步定时执行,脚本:

#!/bin/bash

CMD="/usr/bin/rsync"

RSYNC_USER="rsync_backup"

RSYNC_PASSWROD="123123"

ARGS="-az --delete"

SRC=\'#\'" /span>

DST="/wwwroot"

mkdir -p $DST

$CMD $ARGS $RSYNC_USER@$SRC $DST

实时同步实现(KERNEL 2.6.13 inotify)

./configure --prefix=/usr/local/inotify

make

make install

实时同步脚本1:

INOTIFY_CMD="/usr/local/inotify/bin/inotifywait  -mrq -e modify,create,attrib,move,delete /var/www/html/"

RSYNC_CMD="/usr/bin/rsync -azH --delete /var/www/html/ [email protected]::back  --password-file=/etc/rysnc.pass"

$INOTIFY_CMD | while read DIRECTORY EVENT FILE

do

    if [ $(pgrep rsync | wc -l) -le 0 ] ; then

        $RSYNC_CMD

    fi

done

实时同步脚本2:

host1=172.16.2.2

src=/www/webpages

dst1=web01

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

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

本文转自 jxwpx 51CTO博客,原文链接:http://blog.51cto.com/jxwpx/1286714,如需转载请自行联系原作者