追求卓越,成功就会在不经意间追上你
全部博文(112)
分类: LINUX
2012-03-05 17:10:52
rsync
#源码包存放目录
cd /usr/local/src
#下载源码包
wget -c
#解压缩及配置,编译,安装
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(from localhost 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 / -name libiconv.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.9 192.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.67 bytes/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@MySql02 src]# 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
rsync error: error starting client-server protocol (code 5) at main.c(1516) [Receiver=3.0.9]
查看server日志
[root@MySql01 src]# cat /var/log/rsyncd.log
2012/03/05 13:57:22 [8051] rsyncd version 3.0.9 starting, listening on port 873
2012/03/05 13:57:38 [8058] name lookup failed for 192.168.164.133: Name or service not known
2012/03/05 13:57:38 [8058] connect from UNKNOWN (192.168.164.133)
2012/03/05 13:57:38 [8058] rsync: chdir /usr/local/apache/htdocs/efast failed
: No such file or directory (2)
2012/03/05 14:06:05 [8072] name lookup failed for 192.168.164.133: Name or service not known
2012/03/05 14:06:05 [8072] connect from UNKNOWN (192.168.164.133)
2012/03/05 14:06:05 [8072] rsync: chdir /usr/local/apache/htdocs/efast failed
: No such file or directory (2)
由错误日志可知,没有/usr/lcoal/apache/htdocs/efast这个目录。知道为啥错了,就好想办法解决了,哈哈!!!
错误2:
[root@MySql01 inotify-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
rsync error: error starting client-server protocol (code 5) at main.c(1516) [Receiver=3.0.9]
[root@MySql01 inotify-tools-3.14]# cat /var/log/rsyncd.log | tail -n 5
2012/03/05 14:12:49 [8096] building file list
2012/03/05 14:12:49 [8096] sent 211 bytes received 106 bytes total size 44
2012/03/05 14:47:38 [10667] name lookup failed for 192.168.164.129: Name or service not known
2012/03/05 14:47:38 [10667] connect from UNKNOWN (192.168.164.129)
2012/03/05 14:47:38 [10667] auth failed on module test from unknown (192.168.164.129): password mismatch
看到错误信息了么?password mismatch!!!检查一下你的密码配置文件吧,嘿嘿~~~
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@MySql01 src]# uname -r
2.6.18-274.el5
[root@MySql01 src]# ll /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Mar 5 14:18 max_queued_events
-rw-r--r-- 1 root root 0 Mar 5 14:18 max_user_instances
-rw-r--r-- 1 root root 0 Mar 5 14:18 max_user_watches
有了以上信息后就可以下载安装inotify-tools了:
[root@MySql01 src]# wget -c
[root@MySql01 src]# tar -xvf inotify-tools-3.14.tar.gz
[root@MySql01 src]# ./configure
[root@MySql01 src]# make && make install
安装好后,便可以通过相关shell脚本利用inotify的实时监控来同步server的文件到backup机器上
例:把mysql01上/usr/local/test/下的东东实时同步到mysql02上的/usr/local/test下
MySql01:
1.安装rsync,略过
2.安装inotify,略过
3.编写inotify脚本
[root@MySql01 src]# cat inotify_test.sh
#!/bin/bash
#FileName:inotify_test.sh
#Desc:test inotify+rsync
#Created By: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' -e modify,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} was rsynced">>/tmp/rsync.log 2>&1
done
4.把3的脚本添加到启动后台运行
[root@MySql01 src]# chmod +x inotify_test.sh
[root@MySql01 src]# ./inotify_test.sh &
[root@MySql01 src]# echo "inotify-test.sh &">>/etc/rc.d/rc.local
MySql02:
1.安装、配置rsync
安装略过
编辑配置文件:
[root@MySql02 src]# cat /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
[test1]
path = /usr/local/test/
comment = test
ignore errors
read only = no
write only = no
hosts allow = *
hosts deny = 1.1.1.1
list = yes
uid = root
gid = root
auth users = backup
secrets file = /etc/rsync.secret1
编辑密码相关文件:权限一定要为600
[root@MySql02 src]# cat /etc/rsync.secret1
backup:backup
另须在MySql01上的/etc/下也建立一个rsync.secret1文件存放密码,这里权限也要是600
[root@MySql01 src]# cat /etc/rsync.secret1
backup
以daemon 启动并加到开机启动
[root@MySql02 src]# /usr/bin/rsync --daemon
[root@MySql02 src]# echo "/usr/bin/rsync --daemon" >> /etc/rc.d/rc.local
3.等待MySql01更新
测试:
1.
[root@MySql02 test]# pwd
/usr/local/test
[root@MySql02 test]# ll
total 0
[root@MySql01 src]# cd /usr/local/test
[root@MySql01 test]# pwd
/usr/local/test
[root@MySql01 test]# ll
total 0
[root@MySql01 test]# echo "HHHHHHHHHHHHHHHHHHHHHHH" > test.txt
[root@MySql01 test]# sending incremental file list
./
test.txt
24 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/2)
sent 91 bytes received 30 bytes 242.00 bytes/sec
total size is 24 speedup is 0.20
sending incremental file list
sent 43 bytes received 8 bytes 102.00 bytes/sec
total size is 24 speedup is 0.47
[root@MySql02 test]# ll
total 4
-rw-r--r-- 1 root root 24 Mar 5 16:45 test.txt
2.
[root@MySql01 test]# mkdir test
[root@MySql01 test]# sending incremental file list
./
test/
sent 75 bytes received 15 bytes 60.00 bytes/sec
total size is 24 speedup is 0.27
[root@MySql02 test]# ll
total 8
drwxr-xr-x 2 root root 4096 Mar 5 16:46 test
-rw-r--r-- 1 root root 24 Mar 5 16:45 test.txt
[root@MySql01 test]# rm -rf test
[root@MySql01 test]# sending incremental file list
./
deleting test/
sent 50 bytes received 11 bytes 122.00 bytes/sec
total size is 24 speedup is 0.39
[root@MySql02 test]# ll
total 4
-rw-r--r-- 1 root root 24 Mar 5 16:45 test.txt