rsync:rsync+notify测试及相关错误整理

来源:互联网 发布:什么五笔输入法mac版 编辑:程序博客网 时间:2024/05/16 18:25

#源码包存放目录

cd /usr/local/src

 

#下载源码包

wget -chttp://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz

 

#解压缩及配置,编译,安装

tar -xzvf rsync-3.0.9.tar.gz

cd rsync-3.0.9

./configure

make

make install

 

 

使用举例:

ex1: copy rsync-3.0.9 to aa (localhost)

[root@Monitor01 src]# mkdir -p aa

[root@Monitor01 src]# rsync -av rsync-3.0.9 aa

sending incremental file list

rsync-3.0.9/

rsync-3.0.9/.gitignore

rsync-3.0.9/COPYING

rsync-3.0.9/Doxyfile

rsync-3.0.9/INSTALL

rsync-3.0.9/Makefile

......

[root@Monitor01 src]# rsync -av rsync-3.0.9/aa

[root@Monitor01 src]# ll aa

total 5320

-rw-rw-r-- 1 root root    5142 Jan 18 2009 access.c

-rw-r--r-- 1 root root   20808 Feb 27 11:52 access.o

-rw-rw-r-- 1 root root    3174 Nov 3  2007 aclocal.m4

-rw-rw-r-- 1 root root   30756 May 30 2011 acls.c

-rw-r--r-- 1 root root    4480 Feb 27 11:52 acls.o

......

 

ex2:copy rsync-3.0.9 to host--Monitor02(fromlocalhost to remote host)

[root@Monitor02 src]# rsync -av rsync-3.0.9 aa

rsync: error while loading shared libraries:libiconv.so.2: cannot open shared object file: No such file or directory

遇到以上错误,解决方法如下:

找到libiconv.so.2的路径

[root@Monitor02 src]# find / -namelibiconv.so*

/usr/local/lib/libiconv.so

/usr/local/lib/libiconv.so.2.5.1

/usr/local/lib/libiconv.so.2

/usr/local/src/libiconv-1.14/lib/.libs/libiconv.so

/usr/local/src/libiconv-1.14/lib/.libs/libiconv.so.2.5.1

/usr/local/src/libiconv-1.14/lib/.libs/libiconv.so.2

…….

把相关路径添加到动态链接库配置文件

[root@Monitor02 src]# echo"/usr/local/lib/">>/etc/ld.so.conf

更新相关库文件

[root@Monitor02 src]# /sbin/ldconfig

 

[root@Monitor01 src]# rsync -av rsync-3.0.9192.168.164.131:/usr/local/src/aa

root@192.168.164.131's password:

sending incremental file list

rsync-3.0.9/

rsync-3.0.9/.gitignore

rsync-3.0.9/COPYING

......

sent 6278442 bytes  received 5228 bytes  837822.67 bytes/sec

total size is 6260141  speedup is 1.00

[root@Monitor01 src]#

 

 

ex3: C/S module

server: Monitor01 192.168.164.129

client:Monitor02 192.168.164.131

server:

[root@Monitor01 rsync-3.0.9]# vim/etc/rsyncd.conf

#FileName:rsync.conf

#Desc:An example for rsync configuration

#Created By:fedoracle

#Date:2012/02/27

 

uid=nobody

gid=nobody

use chroot=no

max connections=10

strict modes=yes

 

pid file=/var/run/rsyncd.pid

lock file=/var/run/rsyncd.lock

log file=/var/log/rsyncd.log

 

[efast]

path=/usr/local/apache/htdocs/efast/program #要备份目录

comment=test efast

ignore errors

read only=no

write only=no

hosts allow=*

hosts deny=192.168.111.111

list=true

uid=root

gid=root

auth users=backup #用于C/S间连接的用户

secrets file=/etc/rsync.secrets #auth users参数中指定的用户的用户名及密码,格式为user:pw

 

[root@Monitor01 rsync-3.0.9]# vim /etc/rsync.secrets

backup:backup

[root@Monitor02 src]# chmod 600 /etc/rsync.secrets#权限一定要设置好!

 

client:

[root@Monitor02 src]# vim /etc/rsync.secrets

backup

[root@Monitor02 src]# chmod 600 /etc/rsync.secrets#权限一定要设置好!

 

测试:

[root@Monitor02 src]# rsync -vzrtopg --delete--progress --exclude "*access*" --exclude "*debug*"backup@192.168.164.129::efast /test --password-file=/etc/rsync.secrets

......

[root@Monitor02 src]# rsync -vzrtopg --delete--progress --exclude "*access*" --exclude "*debug*"backup@192.168.164.129::efast /test --password-file=/etc/rsync.secrets

receiving incremental file list

 

sent 405 bytes received 102610 bytes  68676.67bytes/sec

total size is 47611598  speedup is 462.18

[root@Monitor02 src]#

……

成功!

 

备份策略:每天一点进行同步

crontab -e

00 1 * * * /usr/bin/rsync -vzrtopg --delete--progress --exclude "*access*" --exclude "*debug*"backup@192.168.164.129::efast /test --password-file=/etc/rsync.secrets

 

 

错误1

[root@MySql02src]# rsync -vzrtopg --delete --progress --exclude "*access*"--exclude "*debug*" backup@192.168.164.129::test /usr/local/src/aa--password-

file=/etc/rsync.secret

@ERROR:chdir failed

rsyncerror: error starting client-server protocol (code 5) at main.c(1516)[Receiver=3.0.9]

查看server日志

[root@MySql01src]# cat /var/log/rsyncd.log

2012/03/0513:57:22 [8051] rsyncd version 3.0.9 starting, listening on port 873

2012/03/0513:57:38 [8058] name lookup failed for 192.168.164.133: Name or service notknown

2012/03/0513:57:38 [8058] connect from UNKNOWN (192.168.164.133)

2012/03/0513:57:38 [8058] rsync: chdir /usr/local/apache/htdocs/efast failed

:No such file or directory (2)

2012/03/0514:06:05 [8072] name lookup failed for 192.168.164.133: Name or service notknown

2012/03/0514:06:05 [8072] connect from UNKNOWN (192.168.164.133)

2012/03/0514:06:05 [8072] rsync: chdir /usr/local/apache/htdocs/efast failed

:No such file or directory (2)

由错误日志可知,没有/usr/lcoal/apache/htdocs/efast这个目录。知道为啥错了,就好想办法解决了,哈哈!!!

 

错误2

[root@MySql01inotify-tools-3.14]# rsync -vzrtopg --delete --progress --exclude"*access*" --exclude "*debug*" backup@192.168.164.129::test/usr/local/src/aa

--password-file=/etc/rsync.secret

@ERROR:auth failed on module test

rsyncerror: error starting client-server protocol (code 5) at main.c(1516)[Receiver=3.0.9]

[root@MySql01inotify-tools-3.14]# cat /var/log/rsyncd.log | tail -n 5

2012/03/0514:12:49 [8096] building file list

2012/03/0514:12:49 [8096] sent 211 bytes  received106 bytes  total size 44

2012/03/0514:47:38 [10667] name lookup failed for 192.168.164.129: Name or service notknown

2012/03/0514:47:38 [10667] connect from UNKNOWN (192.168.164.129)

2012/03/0514:47:38 [10667] auth failed on module test from unknown (192.168.164.129):password mismatch

看到错误信息了么?passwordmismatch!!!检查一下你的密码配置文件吧,嘿嘿~~~

 

 

rsync+inotify

实时备份:

Inotify

不需要对被监视的目标打开文件描述符,而且如果被监视目标在可移动介质上,那么在 umount 该介质上的文件系统后,被监视目标对应的 watch 将被自动删除,并且会产生一个 umount 事件。

Inotify既可以监视文件,也可以监视目录。

Inotify使用系统调用而非 SIGIO 来通知文件系统事件。

Inotify使用文件描述符作为接口,因而可以使用通常的文件 I/O 操作select 和 poll来监视文件系统的变化。

Inotify可以监视的文件系统事件包括:

IN_ACCESS,即文件被访问

IN_MODIFY,文件被 write

IN_ATTRIB,文件属性被修改,如 chmod、chown、touch等

IN_CLOSE_WRITE,可写文件被 close

IN_CLOSE_NOWRITE,不可写文件被 close

IN_OPEN,文件被 open

IN_MOVED_FROM,文件被移走,如 mv

IN_MOVED_TO,文件被移来,如 mv、cp

IN_CREATE,创建新文件

IN_DELETE,文件被删除,如 rm

IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己

IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己

IN_UNMOUNT,宿主文件系统被 umount

IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)

IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO)

注:上面所说的文件也包括目录。

 

 

在系统中执行如下命令,如果能看到相关结果,说明你当前的系统对inotify支持良好,如果没有相关信息,请自行编译内核使其支持inotify机制。

[root@MySql01src]# uname -r

2.6.18-274.el5

[root@MySql01src]# ll /proc/sys/fs/inotify

total0

-rw-r--r--1 root root 0 Mar  5 14:18max_queued_events

-rw-r--r--1 root root 0 Mar  5 14:18max_user_instances

-rw-r--r--1 root root 0 Mar  5 14:18max_user_watches

 

有了以上信息后就可以下载安装inotify-tools了:

[root@MySql01src]# wget -c http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.g

[root@MySql01src]# tar -xvf inotify-tools-3.14.tar.gz

[root@MySql01src]# ./configure

[root@MySql01src]# make && make install

 

安装好后,便可以通过相关shell脚本利用inotify的实时监控来同步server的文件到backup机器上

 

例:把mysql01上/usr/local/test/下的东东实时同步到mysql02上的/usr/local/test下

MySql01:

1.安装rsync,略过

2.安装inotify,略过

3.编写inotify脚本

[root@MySql01src]# cat inotify_test.sh

#!/bin/bash

#FileName:inotify_test.sh

#Desc:testinotify+rsync

#CreatedBy:xiaozhenggang(fedoracle)

#Date:2012/03/05

 

HOST=192.168.164.133

SRC=/usr/local/test/

DST=test1

USER=backup

 

/usr/local/bin/inotifywait-mrq --timefmt '%d/%m/%y/ %H:%M' --format '%T %w%f%e' -emodify,delete,create,attrib $SRC | while read files

do

   /usr/bin/rsync -vzrtopg --delete --progress--password-file=/etc/rsync.secret1 $SRC $USER@$HOST::$DST

   echo "${files} wasrsynced">>/tmp/rsync.log 2>&1

done

 

4.把3的脚本添加到启动后台运行

[root@MySql01src]# chmod +x inotify_test.sh

[root@MySql01src]# ./inotify_test.sh &

[root@MySql01src]# echo "inotify-test.sh &">>/etc/rc.d/rc.local

 

MySql02:

1.安装、配置rsync

安装略过

编辑配置文件:

[root@MySql02src]# cat /etc/rsyncd.conf

uid= nobody

gid= nobody

usechroot = no

maxconnections = 10

strictmodes = yes

pidfile = /var/run/rsyncd.pid

lockfile = /var/run/rsync.lock

logfile = /var/log/rsync.log

 

[test1]

path= /usr/local/test/

comment= test

ignoreerrors

readonly = no

writeonly = no

hostsallow = *

hostsdeny = 1.1.1.1

list= yes

uid= root

gid= root

authusers = backup

secretsfile = /etc/rsync.secret1

 

编辑密码相关文件:权限一定要为600

[root@MySql02src]# cat /etc/rsync.secret1

backup:backup

另须在MySql01上的/etc/下也建立一个rsync.secret1文件存放密码,这里权限也要是600

[root@MySql01src]# cat /etc/rsync.secret1

backup

以daemon 启动并加到开机启动

[root@MySql02src]# /usr/bin/rsync --daemon

[root@MySql02src]# echo "/usr/bin/rsync --daemon" >> /etc/rc.d/rc.local

 

3.等待MySql01更新

 

测试:

1.

[root@MySql02test]# pwd

/usr/local/test

[root@MySql02test]# ll

total0

 

[root@MySql01src]# cd /usr/local/test

[root@MySql01test]# pwd

/usr/local/test

[root@MySql01test]# ll

total0

[root@MySql01test]# echo "HHHHHHHHHHHHHHHHHHHHHHH" > test.txt

[root@MySql01test]# sending incremental file list

./

test.txt

          24 100%    0.00kB/s   0:00:00 (xfer#1, to-check=0/2)

sent91 bytes  received 30 bytes  242.00 bytes/sec

totalsize is 24  speedup is 0.20

sendingincremental file list

sent43 bytes  received 8 bytes  102.00 bytes/sec

totalsize is 24  speedup is 0.47

 

[root@MySql02test]# ll

total4

-rw-r--r--1 root root 24 Mar  5 16:45 test.txt

 

2.

[root@MySql01test]# mkdir test

[root@MySql01test]# sending incremental file list

./

test/

 

sent75 bytes  received 15 bytes  60.00 bytes/sec

totalsize is 24  speedup is 0.27

 

[root@MySql02test]# ll

total8

drwxr-xr-x2 root root 4096 Mar  5 16:46 test

-rw-r--r--1 root root   24 Mar  5 16:45 test.txt

 

[root@MySql01test]# rm -rf test

[root@MySql01test]# sending incremental file list

./

deletingtest/

sent50 bytes  received 11 bytes  122.00 bytes/sec

totalsize is 24  speedup is 0.39

[root@MySql02test]# ll

total4

-rw-r--r--1 root root 24 Mar  5 16:45 test.txt

0 0