1.1. Rsync介绍
1.1.1 什么是Rsync
Rsync是一款开源的、快速的、多功能的、可实现全量或增量的本地或者远程数据镜像同步复制、备份的优秀工具。Rsync适用于unix、linux、windows等各种平台。Rsync 是一个远程数据同步工具,可通过 LAN/WAN 快速同步多台主机间的文件。Rsync 本来是用以取代scp 的一个工具,它当前由 Rsync.samba.org 维护。Rsync 使用所谓的“Rsync 演算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。运行 Rsync server 的机器也叫 backup server,一个 Rsync server 可同时备份多个 client 的数据;也可以多个Rsync server 备份一个 client 的数据。Rsync 可以搭配 rsh 或 ssh 甚至使用 daemon 模式。Rsync server 会打开一个873的服务通道(port),等待对方 Rsync 连接。连接时,Rsync server 会检查口令是否相符,若通过口令查核,则可以开始进行文件传输。第一次连通完成时,会把整份文件传输一次,下一次就只传送二个文件之间不同的部份。参考资料:http://www.samba.org/ftp/rsync/rsync.html1.1.2 Rsync 简介
Rsync 英文全称:Remote Rynchronization,可实现全量或增量的本地或者远程数据镜像同步复制、备份,类似于scp命令,但是优于scp,可以做增量的备份。Rsync还可以在本地主机的不同分区或目录之间全量及增量的复制数据,这类似与cp命令,同样优于cp命令(增量)。提示:Rsync 还可以实现类似rm的删除功能( --process --delete)在同步备份数据时,默认情况下,Rsync通过其独特的quick check 算法,它仅同步大小或者最后修改时间发生变化的文件或目录,也可以根据全县,属主等属性的变化同步,但是需要指定相应的参数,设置可以实现只同步一个文件里有变化部分的内容。1.1.3 Rsync特性
支持拷贝特殊文件如链接、设备等可以有排除特定文件或目录同步的功能,相当于打包命令tar的排除功能。可以做到保持源文件或目录的权限、时间、软硬链接、属主属组等所有属性的不改变。可以实现增量同步,既只同步发生变化的数据,因为数据传输效率很高。可以使用rcp、rsh、ssh等方式配合传输文件,也可以直接通过socket(进程方式)传输。支持匿名的或认证(无需系统用户)的进程模式传输,可实现方便安全的进行数据备份及镜像。1.1.4 Rsync 的核心算法
假定在名为 α 和 β 的两台计算机之间同步相似的文件 A 与 B,其中 α 对文件A拥有访问权,β 对文件 B 拥有访问权,并且假定主机 α 与 β 之间的网络带宽很小。Rsync 算法将通过下面的五个步骤来完成:①β 将文件 B 分割成一组不重叠的固定大小为 S 字节的数据块。最后一块可能会比 S 小。②β 对每一个分割好的数据块执行两种校验:一种是32位的滚动弱校验,另一种是128位的 MD4 强校验。③β 将这些校验结果发给 α。④α 通过搜索文件A的所有大小为 S 的数据块(偏移量可以任选,不一定非要是 S 的倍数),来寻找与文件B 的某一块有着相同的弱校验码和强校验码的数据块。这项工可以借助滚动校验的特性很快完成。⑤α 发给 β 一串指令来生成文件 A 在 β 上的备份。这里的每一条指令要么是对文件 B 经拥有某一个数据块而不须重传的证明,要么是一个数据块,这个数据块肯定是没有与文件 B 的任何一个数据块匹配上的。2.1 Rsync的工作方式
1、主机本地间的数据传输(类似cp命令的功能)2、借助rcp、ssh等通道来传输数据(类似scp命令的功能)3、以守护进程(socket)的方式传输数据(重要 ※※※※※)2.1.1 本地数据传输模式(local-only mode)
1 Local: rsync [OPTION...] SRC... [DEST]
2.1.2 通过远程shell进行数据传输(remote shell mode)
1 Access via remote shell:2 Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]3 Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
2.1.3 通过守护进程的方式进行数据传输
1 Access via rsync daemon:2 Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]3 rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]4 Push: rsync [OPTION...] SRC... [USER@]HOST::DEST5 rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
Rsync命令同步参数选项
rsync [OPTION...] SRC ... DEST 常用参数选项说明: -v, --verbose 详细模式输出,传输时的进度等信息 -z, --compress 传输时进行压缩以提高传输效率,--compress-level=NUM可按级别压缩 -r, --recursive 对子目录以递归模式,即目录下的所有目录都同样传输,注意是小写r -t, --times 保持文件时间信息 -o, --owner 保持文件属主信息 -p, --perms 保持文件权限 -g, --group 保持文件属组信息 -P, --progress 显示同步的过程及传输时的进度等信息 -a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rtopgDl -D, --devices 保持设备文件信息 -l, --links 保留软链接 -e, --rsh=COMMAND 使用的信道协议, 指定替代rsh的shell程序。例如:ssh --exclude=PATTERN 指定排除不需要传输的文件模式以上为常用的参数:更多参数请自行man rsync 或查看资料地址: http://www.samba.org/ftp/rsync/rsync.html
vza相当于 vzrtopgDl特别提示:
rsync -avz /opt/ /mnt #加/,只同步目录内的内容 rsync -avz /opt /mnt #不加/,会将目录和目录内的内容一起同步3.1 通过Rsync在本地传输数据实践
1 #通过rsync命令,推送56_rsync_a.log到10.0.0.57的家目录 2 [root@Rsync_A ~]# ll 3 total 76 4 -rw-r--r-- 1 root root 0 May 30 20:34 56_rsync_a.log 5 -rw------- 1 root root 967 May 21 02:09 anaconda-ks.cfg 6 -rw-r--r-- 1 root root 41751 May 21 02:08 install.log 7 -rw-r--r-- 1 root root 4688 May 21 02:06 install.log.syslog 8 -rw-r--r-- 1 root root 2832 May 13 11:08 system_init.sh 9 [root@Rsync_A ~]# rsync -avzP 56_rsync_a.log root@10.0.0.57:~10 sending incremental file list11 56_rsync_a.log12 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)1314 sent 75 bytes received 31 bytes 212.00 bytes/sec15 total size is 0 speedup is 0.001617 [root@Rsync_B ~]# ll18 total 6819 -rw-r--r-- 1 root root 0 May 30 20:34 56_rsync_a.log20 -rw------- 1 root root 967 May 21 02:09 anaconda-ks.cfg21 -rw-r--r-- 1 root root 41751 May 21 02:08 install.log22 -rw-r--r-- 1 root root 4688 May 21 02:06 install.log.syslog2324 #通过rsync命令,拉取10.0.0.57主机/tmp目录数据到本地/tmp目录下25 [root@Rsync_B tmp]# ll26 total 427 -rw-r--r-- 1 root root 0 May 30 21:38 57.tmp.log28 drwx------ 2 root root 4096 May 30 21:04 ssh-XpIRDi24922930 [root@Rsync_A ~]# ll /tmp/31 total 832 drwx------ 2 root root 4096 May 30 20:09 ssh-aiWOPN253733 -rw-r--r-- 1 root root 2832 May 13 11:08 system_init.sh34 [root@Rsync_A ~]# rsync -avz root@10.0.0.57:/tmp /tmp35 receiving incremental file list36 tmp/37 tmp/57.tmp.log38 tmp/.ICE-unix/39 tmp/ssh-XpIRDi2492/40 tmp/ssh-XpIRDi2492/agent.24924142 sent 45 bytes received 210 bytes 510.00 bytes/sec43 total size is 0 speedup is 0.0044 [root@Rsync_A ~]# ll /tmp/45 total 1246 drwx------ 2 root root 4096 May 30 20:09 ssh-aiWOPN253747 -rw-r--r-- 1 root root 2832 May 13 11:08 system_init.sh48 drwxrwxrwt 4 root root 4096 May 30 2013 tmp49 [root@Rsync_A ~]# tree /tmp/50 /tmp/51 |-- ssh-aiWOPN253752 | `-- agent.253753 |-- system_init.sh54 `-- tmp55 |-- 57.tmp.log56 `-- ssh-XpIRDi249257 `-- agent.24925859 3 directories, 4 files
3.2借助rcp、ssh等通道来传输数据实践
1 #通过ssh通道推送数据 2 [root@Rsync_A ~]# rsync -avzP -e "ssh -p22" 56_rsync_a.log root@10.0.0.57:/tmp 3 sending incremental file list 4 56_rsync_a.log 5 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) 6 7 sent 75 bytes received 31 bytes 212.00 bytes/sec 8 total size is 0 speedup is 0.00 910 [root@Rsync_B ~]# ll /tmp/11 total 412 -rw-r--r-- 1 root root 0 May 30 20:34 56_rsync_a.log13 -rw-r--r-- 1 root root 0 May 30 21:38 57.tmp.log14 drwx------ 2 root root 4096 May 30 21:04 ssh-XpIRDi24921516 #通过ssh通道拉取数据17 [root@Rsync_A ~]# rsync -avz -e "ssh -p22" root@10.0.0.57:/tmp .18 receiving incremental file list19 tmp/20 tmp/56_rsync_a.log21 tmp/57.tmp.log22 tmp/.ICE-unix/23 tmp/ssh-XpIRDi2492/24 tmp/ssh-XpIRDi2492/agent.24922526 sent 64 bytes received 274 bytes 676.00 bytes/sec27 total size is 0 speedup is 0.0028 [root@Rsync_A ~]# ll29 total 8030 -rw-r--r-- 1 root root 0 May 30 20:34 56_rsync_a.log31 -rw------- 1 root root 967 May 21 02:09 anaconda-ks.cfg32 -rw-r--r-- 1 root root 41751 May 21 02:08 install.log33 -rw-r--r-- 1 root root 4688 May 21 02:06 install.log.syslog34 -rw-r--r-- 1 root root 2832 May 13 11:08 system_init.sh35 drwxrwxrwt 4 root root 4096 May 30 2013 tmp36 [root@Rsync_A ~]# tree tmp/37 tmp/38 |-- 56_rsync_a.log39 |-- 57.tmp.log40 `-- ssh-XpIRDi249241 `-- agent.24924243 1 directory, 3 files
3.3 通过ssh key+rsync实现批量免密码加密分发数据
1 #批量将任意文件服务分发至/tmp目录下 2 #!/bin/sh 3 . /etc/init.d/functions 4 if [ $# -ne 1 ];then 5 echo "Usage:$0 argv" 6 exit 7 fi 8 for ip in `cat iplist` 9 do10 rsync -avzP $1 -e "ssh -p 52113" lican888@$ip:~ >&/dev/null11 ssh -p52113 -t lican888@$ip sudo rsync ~/$1 /etc >&/dev/null12 if [ $? -eq 0 ];then13 action "fenfa $1 successful." /bin/true14 else15 action "fenfa $1 failure." /bin/false16 fi17 done
3.4 以守护进程(socket)的方式传输数据(※※※※※)
主机网络参数设置:
主机名 | IP | GATEWAY | 备注 |
Rsync_A | 10.0.0.56 | 10.0.0.254 | rsync服务端 |
Rsync_B | 10.0.0.57 | 10.0.0.254 | rsync节点 |
3.4.1 开始配置
1)配置rsyncd.conf
1 #确认安装 2 [root@Rsync_A ~]# rpm -qa rsync 3 rsync-3.0.6-4.el5_7.1 ---> 3.0版本,一边比对,一边同步 4 5 [root@Rsync_A ~]# vim /etc/rsyncd.conf 6 #rsync_config_______________start 7 uid = rsync 8 gid = rsync 9 use chroot = no10 max connections = 20011 timeout = 30012 pid file = /var/run/rsyncd.pid13 lock file = /var/run/rsync.lock14 log file = /var/log/rsyncd.log1516 [skyex]17 path = /skyex/18 ignore errors19 read only = false20 list = false21 hosts allow = 10.0.0.0/2422 hosts deny = 0.0.0.0/3223 auth users = rsync_backup24 secrets file = /etc/rsync.password25 #rsync_config_______________end
-->配置多个模块路径的配置文件简便写法:
1 [root@Rsync_A ~]# cat /etc/rsyncd.conf 2 ##rsyncd.conf start## 3 uid = rsync 4 gid = rsync 5 use chroot = no 6 max connections = 200 7 timeout = 300 8 pid file = /var/run/rsyncd.pid 9 lock file = /var/run/rsync.lock10 log file = /var/log/rsyncd.log11 ignore errors12 read only = false13 list = false14 hosts allow = 10.0.0.0/2415 hosts deny = 0.0.0.0/3216 auth users = rsync_backup17 secrets file = /etc/rsync.password18 [skyex]19 path = /backup/20 [skyex01]21 path = /backup01/22 #rsync_config_______________end
2)配置用户目录、密码文件
1 #目录、用户权限创建配置 2 [root@Rsync_A ~]# useradd rsync -s /sbin/nologin -M 3 [root@Rsync_A ~]# grep rsync /etc/passwd 4 rsync:x:502:502::/home/rsync:/sbin/nologin 5 [root@Rsync_A ~]# chown rsync.rsync /skyex/ 6 [root@Rsync_A ~]# ls -ld /skyex/ 7 drwxr-xr-x 2 rsync rsync 167936 May 30 22:10 /skyex/ 8 9 #配置密码文件(格式---> 用户:密码)10 [root@Rsync_A ~]# echo "rsync_backup:skyex" >> /etc/rsync.password11 [root@Rsync_A ~]# cat /etc/rsync.password12 rsync_backup:skyex1314 #更改密码文件权限60015 [root@Rsync_A ~]# chmod 600 /etc/rsync.password16 [root@Rsync_A ~]# ls -ld /etc/rsync.password17 -rw------- 1 root root 19 May 27 22:14 /etc/rsync.password
3.4.2启动Rsync服务
1 #启动rsync 2 [root@Rsync_A ~]# rsync --daemon 3 4 #查看rsync进程 5 [root@Rsync_A ~]# ps -ef|grep rsync 6 root 2779 1 0 22:41 ? 00:00:00 rsync --daemon 7 root 2785 2678 0 22:41 pts/0 00:00:00 grep rsync 8 9 #根据端口查看进程10 [root@Rsync_A ~]# lsof -i tcp:87311 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME12 rsync 2779 root 4u IPv4 8610 0t0 TCP *:rsync (LISTEN)1314 [root@Rsync_A ~]# netstat -lntup |grep 87315 tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2779/rsync
3.4.3 Rsync 客户端配置
1 #配置密码文件2 [root@Rsync_B ~]# echo "skyex">>/etc/rsync.passwored3 [root@Rsync_B ~]# cat /etc/rsync.password4 skyex56 #更改密码文件权限为6007 [root@Rsync_B ~]# chmod 600 /etc/rsync.password8 [root@Rsync_B ~]# ls -ld /etc/rsync.password9 -rw------- 1 root root 6 May 27 22:17 /etc/rsync.password
3.4.4 Rsync推送数据
1 #推送/var/www/html到服务器端 2 [root@Rsync_B script]# cd /var/www/ 3 [root@Rsync_B www]# tar zcvf html_$(date +%F).tar.gz ./html/ 4 ./html/ 5 ./html/d 6 ./html/a 7 ./html/c 8 ./html/e 9 ./html/b10 ./html/f11 [root@Rsync_B www]# ll12 total 813 drwxr-xr-x 2 root root 4096 May 30 23:44 html14 -rw-r--r-- 1 root root 190 May 30 23:59 html_2013-05-30.tar.gz15 [root@Rsync_B www]#1617 #推送数据压缩包至rsync服务器skyex模块内18 [root@Rsync_B www]# rsync -avzP html_2013-05-30.tar.gz rsync_backup@10.0.0.56::skyex19 Password:20 sending incremental file list21 html_2013-05-30.tar.gz22 190 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)2324 sent 277 bytes received 27 bytes 46.77 bytes/sec25 total size is 190 speedup is 0.622627 #免密码输入,推送数据28 [root@Rsync_B www]# rsync -avzP html_2013-05-30.tar.gz rsync_backup@10.0.0.56::skyex --password-file=/etc/rsync.password29 sending incremental file list3031 sent 43 bytes received 8 bytes 102.00 bytes/sec32 total size is 190 speedup is 3.7333 [root@Rsync_B www]#3435 #通过rsync协议进行推送36 [root@Rsync_B www]# rsync -avzP html_2013-05-30.tar.gz rsync://rsync_backup@10.0.0.56/skyex --password-file=/etc/rsync.password 37 sending incremental file list3839 sent 43 bytes received 8 bytes 102.00 bytes/sec40 total size is 190 speedup is 3.7341 [root@Rsync_B www]#
FQA:
1 问题1: no route 2 [root@Client-B www]# rsync -avzP html_2013-05-26.tat.gz rsync_backup@10.0.0.101::skyex --password-file=/etc/rsync.password 3 rsync: failed to connect to 10.0.0.101: No route to host (113) 4 rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6] 5 解答:可能防火墙阻挡了。 telnet ip 873 检查,如果出现connection refuse字样表示防火墙阻挡或者服务没有启动好。 6 7 问题2: 8 root@Client-B www]# rsync -avz html_2013-05-26.tar.gz rsync_backup@10.0.0.101::/skyex --password-file=/etc/rsync.password 9 ERROR: The remote path must start with a module name not a /10 rsync error: error starting client-server protocol (code 5) at main.c(1530) [sender=3.0.6]11 解答: 双冒号后面是模块名称,不是路径,去掉/ 。1213 问题3:14 [root@Client-B www]# rsync -avzp html_2013-05-26.tar.gz rsync_backup@10.0.0.101::skyex15 Password:16 @ERROR: auth failed on module skyex17 rsync error: error starting client-server protocol (code 5) at main.c(1530) [sender=3.0.6]18 [root@C-client www]#19 解答:@ERROR: auth failed on module skyex --->配置文件不正确20 密码设置错误,也可导致此错误。2122 问题4:23 [root@Client-B ~]# rsync -avzP 1.txt rsync_backup@10.0.0.101::skyex --password-file=/etc/rsync.password24 sending incremental file list25 1.txt26 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)27 rsync: mkstemp "/.1.txt.ynR66y" (in skyex) failed: Permission denied (13)2829 sent 62 bytes received 27 bytes 178.00 bytes/sec30 total size is 0 speedup is 0.0031 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]32 [root@Client-B ~]#
3.5 总结
Rsync 服务端部署流程
1、配置rsync配置文件
2、创建同步的本地目录/skyex,并根据需要授权rsync服务的用户可读写/ skyex。目录和/etc/rsync.passwore为配置文件中path = / skyex参数的配置
3、账号及密码文件配置
4、启动rsync服务
Rsync客户端配置流程
1、echo “123”>/etc/rsync.password
2、chmod 600 /etc/rsync.password
提示:客户端的和服务端的/etc/rsync.password没有任何关系。只要/etc/rsync.password和客户端rsync命令中的参数--password-file=/etc/rsync.password中的路径对应即可。
Rsync服务端的排错思路
1、查看rsync服务配置文件路径是否正确,默认路径为/etc/rsyncd.conf
2、查看配置文件里host allow,host deny,允许的ip网段是否允许客户端访问的ip网段。
3、查看配置文件中path参数里的路径是否存在,权限是否正确(正常应为配置文件中的UID参数对应的属主和组)
4、查看rsync服务是否启动。查看命令为:ps -ef|grep rsync,端口是否存在netstat -lnt|grep 873
5、查看iptables防火墙和selinux是否开启允许rsync服务通过,也可以考虑关闭。
6、查看服务端rsync配置的密码文件是否为600权限,密码文件格式是否正确,正确格式为:用户名:密码,文件路径和配置文件里的secrect file路径一致。
Rsync客户端的排错思路
1、查看客户端rsync配置的密码文件是否为600的权限,密码文件格式是否正确 注意:仅需要有密码。并且和服务端的密码一致。
2、用telnet链接rsync服务器IP地址873端口,查看服务是否启动(可测试服务端防火墙是否阻挡)。telnet 10.0.0.141 873
3、客户端执行命令时rsync -avzrtopgP rsync_backup@10.0.0.51::skyex/test /test --password-file=/etc/rsync.password
4、自我模拟排错,偏偏不按照要求来做。