rsync+inotify实现数据的实时备份实例

1.原后台与新后台数据复制同步

Ip:10.123.11.143 (新)

10.123.11.145 (旧)

2.新服务器上建立rsync服务器:

vi /etc/default/rsync  

将RSYNC_ENABLE=false改为RSYNC_ENABLE=true    

cp /usr/share/doc/rsync/examples/rsyncd.conf /etc/

vi rsyncd.conf

# sample rsyncd.conf configuration file

# GLOBAL OPTIONS

#motd file=/etc/motdlog file=/var/log/rsyncd

# for pid file, do not use /var/run/rsync.pid i

f# you are going to run rsync out of the init.d script.

pid file=/var/run/rsyncd.pid

syslog facility=daemon

#socket options=

# MODULE OPTION

[host21]     

comment = public archive      

path = /opt/woshare       

use chroot = no#     

max connections=10       

lock file = /var/lock/rsyncd

# the default for read only is yes...     

read only = no     

list = yes      

uid = root     

gid = root

#       exclude =

#       exclude from =

#       include =

#       include from =

#       auth users =        

secrets file = /etc/rsyncd.secrets       

strict modes = yes

#       hosts allow =

#       hosts deny =       

ignore errors = yes       

ignore nonreadable = no      

transfer logging = no

#       log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.      

timeout = 600      

refuse options = checksum dry-run       

dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz

echo "syncer:syncer123">/etc/rsyncd.secrets 

A:安装inotify工具inotify-tools   

由于inotify特性需要Linux内核的支持,在安装inotify-tools前要先确认Linux系统内核是否达到了 2.6.13以上,如果Linux内核低于2.6.13版本,就需要重新编译内核加入inotify的支持,也可以用如下方法判断,内核是否支持 inotify:

[root@localhost webdata]# uname -r

2.6.18-164.11.1.el5PAE

[root@localhost webdata]# ll /proc/sys/fs/inotify

总计 0-rw-r--r-- 1 root root 0 04-13 19:56 max_queued_events

-rw-r--r-- 1 root root 0 04-13 19:56 max_user_instances

-rw-r--r-- 1 root root 0 04-13 19:56 max_user_watches

如果有上面三项输出,表示系统已经默认支持inotify,接着就可以开始安装inotify-tools了。

可以到下载相应的inotify-tools版本,

然后开始编译安装:[

root@localhost  ~]# apt-get install inotify-tools

[root@localhost inotify-tools-3.14]# ll /usr/local/bin/inotifywa*

-rwxr-xr-x 1 root root 37264 04-14 13:42 /usr/local/bin/inotifywai

t-rwxr-xr-x 1 root root 35438 04-14 13:42 /usr/local/bin/inotifywatch

inotify-tools安装完成后,会生成inotifywait和inotifywatch两个指令,其中,inotifywait用于等待文件或文件集上的一个特定事件,它可以监控任何文件和目录设置,并且可以递归地监控整个目录树 

B:建立自动上传脚本vi  rsync-new21.sh  

#!/bin/bash

SRC=/opt/woshare/

DST=/opt/woshare/

INWT=/usr/bin/inotifywait

RSYNC=/usr/bin/rsync

$INWT  -mrq --timefmt '%d%m%y %H:%M' --format '%T%w%f' -e modify,delete,move,create,attrib $SRC|while read filedo$RSYNC -qzrtopg --delete --password-file=/etc/rsync-client.pass $SRC syncer@10.123.11.143::host21

Done

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/58712fe9403ebd7c0982ed89078ed4f9.html