Linux Rsync

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.html

1.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::DEST
5 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 list
11 56_rsync_a.log
12 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
13
14 sent 75 bytes received 31 bytes 212.00 bytes/sec
15 total size is 0 speedup is 0.00
16
17 [root@Rsync_B ~]# ll
18 total 68
19 -rw-r--r-- 1 root root 0 May 30 20:34 56_rsync_a.log
20 -rw------- 1 root root 967 May 21 02:09 anaconda-ks.cfg
21 -rw-r--r-- 1 root root 41751 May 21 02:08 install.log
22 -rw-r--r-- 1 root root 4688 May 21 02:06 install.log.syslog
23
24 #通过rsync命令,拉取10.0.0.57主机/tmp目录数据到本地/tmp目录下
25 [root@Rsync_B tmp]# ll
26 total 4
27 -rw-r--r-- 1 root root 0 May 30 21:38 57.tmp.log
28 drwx------ 2 root root 4096 May 30 21:04 ssh-XpIRDi2492
29
30 [root@Rsync_A ~]# ll /tmp/
31 total 8
32 drwx------ 2 root root 4096 May 30 20:09 ssh-aiWOPN2537
33 -rw-r--r-- 1 root root 2832 May 13 11:08 system_init.sh
34 [root@Rsync_A ~]# rsync -avz root@10.0.0.57:/tmp /tmp
35 receiving incremental file list
36 tmp/
37 tmp/57.tmp.log
38 tmp/.ICE-unix/
39 tmp/ssh-XpIRDi2492/
40 tmp/ssh-XpIRDi2492/agent.2492
41
42 sent 45 bytes received 210 bytes 510.00 bytes/sec
43 total size is 0 speedup is 0.00
44 [root@Rsync_A ~]# ll /tmp/
45 total 12
46 drwx------ 2 root root 4096 May 30 20:09 ssh-aiWOPN2537
47 -rw-r--r-- 1 root root 2832 May 13 11:08 system_init.sh
48 drwxrwxrwt 4 root root 4096 May 30 2013 tmp
49 [root@Rsync_A ~]# tree /tmp/
50 /tmp/
51 |-- ssh-aiWOPN2537
52 | `-- agent.2537
53 |-- system_init.sh
54 `-- tmp
55 |-- 57.tmp.log
56 `-- ssh-XpIRDi2492
57 `-- agent.2492
58
59 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
 9
10 [root@Rsync_B ~]# ll /tmp/
11 total 4
12 -rw-r--r-- 1 root root 0 May 30 20:34 56_rsync_a.log
13 -rw-r--r-- 1 root root 0 May 30 21:38 57.tmp.log
14 drwx------ 2 root root 4096 May 30 21:04 ssh-XpIRDi2492
15
16 #通过ssh通道拉取数据
17 [root@Rsync_A ~]# rsync -avz -e "ssh -p22" root@10.0.0.57:/tmp .
18 receiving incremental file list
19 tmp/
20 tmp/56_rsync_a.log
21 tmp/57.tmp.log
22 tmp/.ICE-unix/
23 tmp/ssh-XpIRDi2492/
24 tmp/ssh-XpIRDi2492/agent.2492
25
26 sent 64 bytes received 274 bytes 676.00 bytes/sec
27 total size is 0 speedup is 0.00
28 [root@Rsync_A ~]# ll
29 total 80
30 -rw-r--r-- 1 root root 0 May 30 20:34 56_rsync_a.log
31 -rw------- 1 root root 967 May 21 02:09 anaconda-ks.cfg
32 -rw-r--r-- 1 root root 41751 May 21 02:08 install.log
33 -rw-r--r-- 1 root root 4688 May 21 02:06 install.log.syslog
34 -rw-r--r-- 1 root root 2832 May 13 11:08 system_init.sh
35 drwxrwxrwt 4 root root 4096 May 30 2013 tmp
36 [root@Rsync_A ~]# tree tmp/
37 tmp/
38 |-- 56_rsync_a.log
39 |-- 57.tmp.log
40 `-- ssh-XpIRDi2492
41 `-- agent.2492
42
43 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 do
10 rsync -avzP $1 -e "ssh -p 52113" lican888@$ip:~ >&/dev/null
11 ssh -p52113 -t lican888@$ip sudo rsync ~/$1 /etc >&/dev/null
12 if [ $? -eq 0 ];then
13 action "fenfa $1 successful." /bin/true
14 else
15 action "fenfa $1 failure." /bin/false
16  fi
17 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 = no
10 max connections = 200
11 timeout = 300
12 pid file = /var/run/rsyncd.pid
13 lock file = /var/run/rsync.lock
14 log file = /var/log/rsyncd.log
15
16 [skyex]
17 path = /skyex/
18 ignore errors
19 read only = false
20 list = false
21 hosts allow = 10.0.0.0/24
22 hosts deny = 0.0.0.0/32
23 auth users = rsync_backup
24 secrets file = /etc/rsync.password
25 #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.lock
10 log file = /var/log/rsyncd.log
11 ignore errors
12 read only = false
13 list = false
14 hosts allow = 10.0.0.0/24
15 hosts deny = 0.0.0.0/32
16 auth users = rsync_backup
17 secrets file = /etc/rsync.password
18 [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.password
11 [root@Rsync_A ~]# cat /etc/rsync.password
12 rsync_backup:skyex
13
14 #更改密码文件权限600
15 [root@Rsync_A ~]# chmod 600 /etc/rsync.password
16 [root@Rsync_A ~]# ls -ld /etc/rsync.password
17 -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:873
11 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
12 rsync 2779 root 4u IPv4 8610 0t0 TCP *:rsync (LISTEN)
13
14 [root@Rsync_A ~]# netstat -lntup |grep 873
15 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.passwored
3 [root@Rsync_B ~]# cat /etc/rsync.password
4 skyex
5
6 #更改密码文件权限为600
7 [root@Rsync_B ~]# chmod 600 /etc/rsync.password
8 [root@Rsync_B ~]# ls -ld /etc/rsync.password
9 -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/b
10 ./html/f
11 [root@Rsync_B www]# ll
12 total 8
13 drwxr-xr-x 2 root root 4096 May 30 23:44 html
14 -rw-r--r-- 1 root root 190 May 30 23:59 html_2013-05-30.tar.gz
15 [root@Rsync_B www]#
16
17 #推送数据压缩包至rsync服务器skyex模块内
18 [root@Rsync_B www]# rsync -avzP html_2013-05-30.tar.gz rsync_backup@10.0.0.56::skyex
19 Password:
20 sending incremental file list
21 html_2013-05-30.tar.gz
22 190 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
23
24 sent 277 bytes received 27 bytes 46.77 bytes/sec
25 total size is 190 speedup is 0.62
26
27 #免密码输入,推送数据
28 [root@Rsync_B www]# rsync -avzP html_2013-05-30.tar.gz rsync_backup@10.0.0.56::skyex --password-file=/etc/rsync.password
29 sending incremental file list
30
31 sent 43 bytes received 8 bytes 102.00 bytes/sec
32 total size is 190 speedup is 3.73
33 [root@Rsync_B www]#
34
35 #通过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 list
38
39 sent 43 bytes received 8 bytes 102.00 bytes/sec
40 total size is 190 speedup is 3.73
41 [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 解答: 双冒号后面是模块名称,不是路径,去掉/ 。
12
13 问题3:
14 [root@Client-B www]# rsync -avzp html_2013-05-26.tar.gz rsync_backup@10.0.0.101::skyex
15  Password:
16  @ERROR: auth failed on module skyex
17 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  密码设置错误,也可导致此错误。
21
22 问题4:
23 [root@Client-B ~]# rsync -avzP 1.txt rsync_backup@10.0.0.101::skyex --password-file=/etc/rsync.password
24 sending incremental file list
25 1.txt
26 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)
28
29 sent 62 bytes received 27 bytes 178.00 bytes/sec
30 total size is 0 speedup is 0.00
31 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、自我模拟排错,偏偏不按照要求来做。
 
原文地址:https://www.cnblogs.com/xiaoleiel/p/8340077.html