rsync可以实现本主机或者跨主机实现数据同步的,基于rsync算法, 将两边的数据文件提取特征码,进行比对。
rsync的功能特性:
1、可以镜像保存整个目录树或文件系统
2、很高的文件传输效率
3、借助ssh实现安全的数据传输(rsync远程复制数据时可以借助ssh来完成)
4、支持匿名传输访问
rsync的算法:
一种是32位滚动校验方式
另一种是128位md4强校验,发送给源主机,比较源于目标是否有不同。
rsync的工作模式:
① shell模式,实现本地文件传输。有点类似(cp命令)
举例:
[root@libin ~]# rsync -v /etc/fstab /tmp
fstabsent 1090 bytes received 31 bytes 2242.00 bytes/sec
total size is 1021 speedup is 0.91
[root@libin ~]# ls /tmp/* | grep fstab
/tmp/fstab②远程shell模式:利用ssh协议承载远程传输过程。
②-1 本地-->远程
②-2 远程<―本地
③列表模式:仅列出源中的内容。(rsync -nv)
举例:
[root@libin ~]# ls /tmp/ | grep fstab
[root@libin ~]# echo $?
1(表示tmp下没有fstab文件)[root@libin ~]# rsync -nv /etc/fstab /tmp
fstabsent 29 bytes received 15 bytes 88.00 bytes/sec
total size is 1021 speedup is 23.20 (DRY RUN)[root@libin ~]# ls /tmp/* | grep fstab
[root@libin ~]# echo $?
1(还是没有fstab文件,仅作测试)④服务器模式:
rsync工作为一个守护进程,等待接受客户端数据同步请求。
工作模型:
![]()
rsync的选项:
-n: 同步测试,不执行真正的同步过程
-v: 详细输出模式
-q: 静默模式
-c: checksum,开启校验功能
-r: 递归复制
-a: 归档,保留文件的原有属性;
-p: 保留文件的权限;
-t: 保留文件的时间戳;
-l: 保留符号链接
-g: 保留属组
-o: 保留属主
-D:保留设备文件
-e ssh: 使用ssh作为传输承载;
-z: 压缩后传输;
--progress: 显示进度条
--stats: 显示如何执行压缩和传输
这里有个需要注意的:若rsync的源目录后面有”/”,表明不复制此目录,仅复制目录下的所有文件或此目录的子目录。
示例一:源目录后带”/”
[root@libin rsync]# rsync -r /tmp/rsync/ /data/rsync
[root@libin rsync]# ls /data/rsync/
1.txt 2.txt 3.txt 4.txt 5.txt示例二:源目录后不带”/”(连目录带目录中的文件和子目录一并复制过来)
[root@libin rsync]# rsync -r /tmp/rsync /data/rsync
[root@libin rsync]# ls /data/rsync/
1.txt 2.txt 3.txt 4.txt 5.txt rsync[root@libin rsync]# cd /data/rsync/rsync/
[root@libin rsync]# ls
1.txt 2.txt 3.txt 4.txt 5.txt
另外rsync还支持ssh协议的传输:
NEXT:
传输成功。
重点看rsync的服务器模式:
演示示例:
客户端:192.168.1.150
服务器:192.168.1.146
要求:rsync服务器端启动需要超级守护进程(xinetd),确保服务器端安装上xinetd了。yum install -y xinetd
服务器端:建立一个目录作为共享的可被客户端存放或读取数据。如/data目录
1、确保有rsync文件
[root@localhost ~]# cd /etc/xinetd.d/
[root@localhost xinetd.d]# ls
chargen-dgram daytime-dgram discard-dgram echo-dgram rsync tftp time-stream
chargen-stream daytime-stream discard-stream echo-stream tcpmux-server time-dgram
2、vim rsync文件,把disabled=yes改成disabled=no
3、编辑rsync,在etc目录生成一个叫做rsyncd.conf。(配置文件分两段:1全局配置段、2、共享配置段[SHARE_NAME])
配置文件示例:
#Global Settings
uid= nobody
gid = nobody
use chroot = no
max connections =10
strict modes = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
#Directory to be synced
[tools]
path = /data
ignore errors = yes (可以忽略传输中的错误)
read only = no
write only = no
hosts allow = 192.168.1.150(白名单)
hosts deny = *
list=true(允许列出来)
4、启动服务
service xinetd start && chkconfig rsync on
LISTEN 0 64 :::873 :::* users:(("xinetd",1859,5))
5、使用命令:
Access via remote shell:(远程shell,上文的第二种模式)
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
Access via rsync daemon:(守护进程,服务器模式)
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
6示例:
6-1 演示向服务器传输数据(Push)
[root@localhost ~]# rsync -v hi.txt 192.168.1.150::tools(这里的SRC要填conf定义的[]的名称)
hi.txtsent 62 bytes received 27 bytes 59.33 bytes/sec
total size is 0 speedup is 0.00[root@libin data]# ls
fstab hi.txt(有了)或者使用:
[root@localhost ~]# rsync /root/howdy.txt rsync://192.168.1.146:873/tools
[root@libin data]# ls
fstab hi.txt howdy.txt6-2 演示向服务器里拉取数据(Pull)
(将整个目录中的文件都拉取过来)
[root@localhost ~]# rsync -v 192.168.1.146::tools
receiving file list ... done
drwxrwxr-x 4096 2014/08/22 20:07:13 .
-rw-r--r-- 1012 2014/08/22 19:54:20 fstab
-rw-r--r-- 0 2014/08/22 20:01:39 hi.txt
-rw-r--r-- 0 2014/08/22 20:07:13 howdy.txtsent 27 bytes received 100 bytes 23.09 bytes/sec
total size is 1012 speedup is 7.97(单拉一个)
[root@localhost ~]# rsync -v 192.168.1.146::tools/hi.txt
receiving file list ... done
-rw-r--r-- 0 2014/08/22 20:01:39 hi.txtsent 27 bytes received 54 bytes 23.14 bytes/sec
total size is 0 speedup is 0.00或者使用这种格式:
(单拉一个文件过来)
[root@localhost ~]# rsync -v rsync://192.168.1.146/tools/howdy.txt
receiving file list ... done
-rw-r--r-- 0 2014/08/22 20:07:13 howdy.txtsent 27 bytes received 57 bytes 18.67 bytes/sec
total size is 0 speedup is 0.007、基于用户认证方式,要求用户输入账号密码。
在rsyncd.conf的共享定义处后面加上两行:
auth users = tom, jerry
secrets file = /etc/rsyncd.secrets这里还需要另外多编辑一个rsyncd.secrets文件,这个文件必须为600.400,但是文件里密码也是明文的。如:
vim /etc/rsyncd.secrets
tom:tom
jerry:jerry[root@localhost tmp]# rsync -r rsync://tom@192.168.1.146:873/tools/ /tmp
Password:[root@localhost tmp]# ls
100.txt httpd-2.4.10.tar.bz2
1.tar mysqli_testclSzix
CentOS-Base.repo mysqli_test.cvsAZTOXQ
fstab mysql.sock
hi.txt new
howdy.txt pear
如果以后经常需要传输文件,要在客户端制定一个周期性任务计划。
inotify: 在服务器端,可用于定义监控指定的目录下的所有文件,一旦有文件的元数据发生改变,即会通知客户端来拉取;
因为文件在本地磁盘的传输速度远比通过nfs协议上要快的多,所以在小文件不算海量的情况下,可以使用rsync+inotify的
-----------------------------------------------------------------------------------------------------下面是整合了rsync+inotify的案例---------------------------
1、先看看inotify-tools在下载完后生成了哪些文件?
[root@libin data]# rpm -ql inotify-tools
/usr/bin/inotifywait
/usr/bin/inotifywatch
/usr/lib64/libinotifytools.so.0
/usr/lib64/libinotifytools.so.0.4.1
/usr/share/doc/inotify-tools-3.14
/usr/share/doc/inotify-tools-3.14/AUTHORS
/usr/share/doc/inotify-tools-3.14/COPYING
/usr/share/doc/inotify-tools-3.14/ChangeLog
/usr/share/doc/inotify-tools-3.14/NEWS
/usr/share/doc/inotify-tools-3.14/README
/usr/share/man/man1/inotifywait.1.gz
/usr/share/man/man1/inotifywatch.1.gz
2、这里生成了两个帮助文档,一个是inotifywait.1.gz,另一个是inotifywatch.1.gz。
先看inotifywatch的帮助文档:
inotifywatch - gather filesystem access statistics using inotify 监视并整合文件系统的访问数据信息
inotifywait:等待文件或文件集上的一个特定事件,它可以监控任何文件和目录设置,并且可以递归地监控整个目录树。
这里举个例子:
我递归监听一下我的tmp目录,时间是20秒
[root@libin ~]# inotifywatch -v -t 20 -r /tmp
Establishing watches...
Setting up watch(es) on /tmp
OK, /tmp is now being watched.
Total of 1936 watches.
Finished establishing watches, now collecting statistics.
Will listen for events for 20 seconds.
在20秒之内,我删除了tmp目录中的1.txt和2.txt两个文件。于是结果显示了:
total close_nowrite open delete filename
6 2 2 2 /tmp/
在举个例子:cd到我的data目录,指定一个20秒钟对data目录的递归监听,与此同时开另一个终端执行rm -rf hi.txt
[root@libin ~]# cd /data/
[root@libin data]# ls
fstab hi.txt howdy.txt
[root@libin data]# inotifywatch -v -t 20 -r /data
同时执行[root@libin data]# rm -rf hi.txt
total close_nowrite open delete filename
5 2 2 1 /data/
另外可以利用rsync命令来拉取服务器端的数据,这时候inotifywatch会就有响应:
此时服务器端:
# inotifywatch -v -t 45 -r /data
Establishing watches...
Setting up watch(es) on /data
OK, /data is now being watched.
Total of 1 watches.
Finished establishing watches, now collecting statistics.
Will listen for events for 45 seconds.
此时客户端执行:
[root@localhost data]# rsync -r tom@192.168.1.146::tools/ /data/
Password:
45秒钟以后:
total close_nowrite open filename
6 3 3 /data/
另外inotify还有另一个命令叫inotifywait,如果安装上了inotify-tools可以用man 查看 man inotifywait
这里有例子分别展示一下:我同时开启两个相同的终端,一台执行inotifywait,另一个执行相应操作
inotifywait [options] DIRECTORY FILE…
1使用inotifywait对某个目录执行监视,看看如果同时在被监视的目录里创建新目录会有什么情况发生?
如
# inotifywait /data
Setting up watches.
Watches established.
另一台执行一个[root@libin data]# mkdir /data/test1
第一台立刻有响应
# inotifywait /data
Setting up watches.
Watches established.
/data/ CREATE,ISDIR test1
2、使用inotifywait对某个目录执行监视,看看如果同时在被监视的目录里对文件执行写入操作会有什么情况发生?
如:
[root@libin data]# inotifywait /data
Setting up watches.
Watches established.
另一台执行:
[root@libin data]# ls
1.txt 2.txt test1
[root@libin data]# cat > 1.txt <<eof
> this is a test
> eof
第一台立刻返回了结果并退出
/data/ OPEN,ISDIR
3、使用rsync的方式如果客户端从服务器端拉取数据?
服务端执行:
[root@libin data]# inotifywait /data
Setting up watches.
Watches established.
客户端执行:
[root@localhost data]# rsync -r tom@192.168.1.146::tools/ /data/
Password 输入密码以后立刻按ENTER键执行
服务器端立刻给出响应:
/data/ OPEN,ISDIR
下面我们要做的操作就是要结合一下rsync和inotify
然后一旦数据有修改等变动,即同步数据至rsync的服务器端。
可以编写一个脚本:
rsync.sh
#!/bin/bash
#
host1=192.168.1.56 ##服务器端IP
src=/data/ ##本地数据存放目录
dst=shared ##服务端数据的目录
user=tom ##允许推送拉取数据的用户
inotifywait -mrq -e modify,delete,create,attrib $src |while read line ;do
rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.passwd $src $user@$host1::$dst
done
~
注意这里的etc/rsyncd.passwd是针对于rsync的客户端来讲的,所以里面只需要用明文来简单传输一个字符串,即密码串,但此密码一定要与服务器端的/etc/rsyncd.passwd中对应的账号密码要保持一致。
rsync的客户端:(主动去同步的)
[root@libin data]# vim /etc/rsyncd.passwd
tom
rsync的服务器端:
[root@localhost data]# vim /etc/rsyncd.passwd
tom:tom(一定要一致)
jerry:jerry
如果需要开机运行,则编辑/etc/rc.local
最后一行加入 /scripts/rsync.sh &