linux服务之rsync

http://www.cnblogs.com/itech/archive/2010/06/13/1757952.html

rsync与mfs好像有点类似,都是传输块的chunk,chunk的

1)软件简介

Rsync 是一个远程数据同步工具,可通过 LAN/WAN 快速同步多台主机间的文件。Rsync 本来是用以取代rcp 的一个工具,它当前由 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 会检查口令是否相符,若通过口令查核,则可以开始进行文件传输。第一次连通完成时,会把整份文件传输一次,下一次就只传送二个文件之间不同的部份。

Rsync 支持大多数的类 Unix 系统,无论是 Linux、Solaris 还是 BSD 上都经过了良好的测试。此外,它在windows 平台下也有相应的版本,比较知名的有 cwRsync 和 Sync2NAS。

Rsync 的基本特点如下:

可以镜像保存整个目录树和文件系统;
可以很容易做到保持原来文件的权限、时间、软硬链接等;
无须特殊权限即可安装;
优化的流程,文件传输效率高;
可以使用 rcp、ssh 等方式来传输文件,当然也可以通过直接的 socket 连接;
支持匿名传输。


2)核心算法

假定在名为 α 和 β 的两台计算机之间同步相似的文件 A 与 B,其中 α 对文件A拥有访问权,β 对文件 B 拥有访问权。并且假定主机 α 与 β 之间的网络带宽很小。那么 Rsync 算法将通过下面的五个步骤来完成:

β 将文件 B 分割成一组不重叠的固定大小为 S 字节的数据块。最后一块可能会比 S 小。
β 对每一个分割好的数据块执行两种校验:一种是32位的滚动弱校验,另一种是128位的 MD4 强校验。
β 将这些校验结果发给 α。
α 通过搜索文件 A 的所有大小为 S 的数据块(偏移量可以任选,不一定非要是 S 的倍数),来寻找与文件B 的某一块有着相同的弱校验码和强校验码的数据块。这项工作可以借助滚动校验的特性很快完成。
α 发给 β 一串指令来生成文件 A 在 β 上的备份。这里的每一条指令要么是对文件 B 经拥有某一个数据块而不须重传的证明,要么是一个数据块,这个数据块肯定是没有与文件 B 的任何一个数据块匹配上的。

how rsync works       https://rsync.samba.org/how-rsync-works.html

从实际应用的角度看Rsync是如何工作的

核心算法

3) 文件级别的RSync(只传输变化的文件)工作过程:(我的理解)

* 机器A构造FileList,FileList包含了需要与机器B sync的所有文件信息对name->id,(id用来唯一表示文件例如MD5);
* 机器A将FileList发送到机器B;
* 机器B上运行的后台程序处理FileList,构建NewFileList,其中根据MD5的比较来删除机器B上已经存在的文件的信息对,只保留机器B上不存在或变化的文件;
* 机器A得到NewFileList,对NewFileList中的文件从新传输到机器B;

4)进一步的优化存储和传输

文件级别的Rsync + Rsync对单个文件分块比较和传输 : 实现了文件的高效传输。

如果Server上存储有所有文件的MD5码索引的数据库 + hardlink技术 : 实现Server的重复文件删除,单个文件只存储一份的技术。

如果server有单个文件只存储一份的技术,(有所有文件的MD5数据库),则在Rsync传输的过程中只传输Rsync Server没有的文件,如果Rsync Server由此文件直接使用。

[root@c64 web]# ps -ef|grep rsync
root      2305     1  0 May14 ?        00:00:00 rsync --daemon
root      4422  4222  0 16:07 pts/0    00:00:00 grep rsync
[root@c64 web]# cd /proc/2305/fd

一切皆文件,文件类型为普通,目录,管道,socket,链接,块设备,字符设备
该进程打开了6个文件描述符,三个为字符设备即012,三个为socket即345,socket的两类协议簇,即inet与unix。其中3是ipv4的,4是unix5是ipv6的。
查看man netstat会看得比较清楚

[root@c64 fd]# ll
total 0
lrwx------. 1 root root 64 May 14 17:24 0 -> /dev/null
lrwx------. 1 root root 64 May 14 17:24 1 -> /dev/null
lrwx------. 1 root root 64 May 14 17:24 2 -> /dev/null
lrwx------. 1 root root 64 May 14 17:24 3 -> socket:[108650]
lrwx------. 1 root root 64 May 14 17:24 4 -> socket:[108646]
lrwx------. 1 root root 64 May 14 17:24 5 -> socket:[108651]
[root@c64 fd]# lsof -p 2305
COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF   NODE NAME
rsync   2305 root  cwd    DIR              253,0     4096 310522 /root/newbuild/build-finished/web
rsync   2305 root  rtd    DIR              253,0     4096      2 /
rsync   2305 root  txt    REG              253,0   410568 270129 /usr/bin/rsync
rsync   2305 root  mem    REG              253,0 99158576 307507 /usr/lib/locale/locale-archive
rsync   2305 root  mem    REG              253,0    18712 130647 /lib64/libattr.so.1.1.0
rsync   2305 root  mem    REG              253,0  1916568 130597 /lib64/libc-2.12.so
rsync   2305 root  mem    REG              253,0    36360 130658 /lib64/libpopt.so.0.0.0
rsync   2305 root  mem    REG              253,0    31280 130675 /lib64/libacl.so.1.1.0
rsync   2305 root  mem    REG              253,0   154504 142339 /lib64/ld-2.12.so
rsync   2305 root  mem    REG              253,0    26060 264009 /usr/lib64/gconv/gconv-modules.cache
rsync   2305 root    0u   CHR                1,3      0t0   3842 /dev/null
rsync   2305 root    1u   CHR                1,3      0t0   3842 /dev/null
rsync   2305 root    2u   CHR                1,3      0t0   3842 /dev/null
rsync   2305 root    3u  IPv4             108650      0t0    TCP *:rsync (LISTEN)
rsync   2305 root    4u  unix 0xffff88001f4ba9c0      0t0 108646 socket
rsync   2305 root    5u  IPv6             108651      0t0    TCP *:rsync (LISTEN)

rsync -av --delete --progress rsync@192.168.2.250::5a --password-file=/etc/rsync.passwd
mount 192.168.2.82:/mnt/virtual/file1/file2/

rsync -av --progress  rsync://192.168.2.82/rsy ./

-a equals -rlptgoD

13:38:36 21 ~/fef:#rsync -av --delete --progress rsync@125.76.228.16::dbback ./ --password-file=/etc/rsync.pass

Usages with just one SRC arg and no DEST arg will list the source files instead of copying.
只有一个SRC且无DEST参数的用法将只列出源文件来代替拷贝。

默认是安装的
yum install rsync

默认rsync服务器是关闭的
vi /etc/xinet.d/rsync
将disable 改为no

创建配置文件
rsync的主要有以下三个配置文件rsyncd.conf(主配置文件)、rsyncd.secrets(密码文件)、rsyncd.motd(rysnc服务器信息)
服务器配置文件(/etc/rsyncd.conf),该文件默认不存在,请创建它。
具体步骤如下:
#touch /etc/rsyncd.conf  #创建rsyncd.conf,这是rsync服务器的配置文件。
#touch /etc/rsyncd.secrets  #创建rsyncd.secrets ,这是用户密码文件。
#chmod 600 /etc/rsyncd/rsyncd.secrets  #将rsyncd.secrets这个密码文件的文件属性设为root拥有, 且权限要设为600, 否则无法备份成功!
#touch /etc/rsyncd.motd

[Wed Apr 15 11:08:11 1018 /dev/pts/0 192.168.2.250 /etc/xinetd.d]#vi /etc/rsyncd.conf
uid = root
gid = root
port = 873
hosts allow = 1.85.49.230
#hosts deny = 0.0.0.0/32
use chroot =
max connections =
timeout=

## 下面这些绿色文件是安装完RSYNC服务后自动生成的文件,当然也可以手动配置到指定路径
pid file = /var/run/rsyncd.pid              ##pid文件的存放
lock file = /var/run/rsync.lock             ##锁文件的存放位置
log file = /var/log/rsyncd.log              ##日志记录文件的存放
#motd file = /etc/rsyncd.motd              ##欢迎

## 上面这段是全局配置,下面的模块可以有
[dbback]                                ## 模块名字,自己命名
path = /VM/db/                           ## 指定文件目录所在位置,这是必须指定
comment = rsync files                             ## 注释
ignore errors                                     ## 忽略IO
read only = yes
list = no                                         ## 是否把rsync 服务器上提供同步数据的目录显示

## 下面这一行,同步验证时用的账号,如果没有这项就是匿名同步,client同步时不用用户名也能同步。
auth users = rsync
secrets file = /etc/rsync.passwd                  ## 指定认证文件

[Wed Apr 15 11:14:30 1020 /dev/pts/0 192.168.2.250 /etc/xinetd.d]#vi /etc/rsync.passwd
[Wed Apr 15 11:16:24 1021 /dev/pts/0 192.168.2.250 /etc/xinetd.d]#chmod 600 /etc/rsync.passwd
[Wed Apr 15 11:16:36 1022 /dev/pts/0 192.168.2.250 /etc/xinetd.d]#vi /etc/rsyncd.motd
[Wed Apr 15 11:18:39 1025 /dev/pts/0 192.168.2.250 /etc/xinetd.d]#cat /etc/rsyncd.motd
welcome to our rsync server
[Wed Apr 15 11:18:45 1026 /dev/pts/0 192.168.2.250 /etc/xinetd.d]#cat /etc/rsync.passwd
rsync:123456


启动rsync服务器
[Wed Apr 15 11:22:03 1031 /dev/pts/0 192.168.2.250 /etc/xinetd.d]#lsof -i :873
启动rsync服务端(以守护进程形式,独立启动)
[Wed Apr 15 11:22:19 1033 /dev/pts/0 192.168.2.250 /etc/xinetd.d]#rsync --daemon
[Wed Apr 15 11:24:06 1034 /dev/pts/0 192.168.2.250 /etc/xinetd.d]#lsof -i :873
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rsync   6113 root    3u  IPv4  34616      0t0  TCP *:rsync (LISTEN)
rsync   6113 root    5u  IPv6  34617      0t0  TCP *:rsync (LISTEN)

开机自动启动

[root@master ~]# echo "/usr/bin/rsync --deamon" >> /etc/rc.d/rc.local

Tip:这里的启动方式比较特殊,如果你要重启,需要kill掉rsync的进程,再重新运行!
另外还有另外一种启动rsync的方式,CentOS 默认以 xinetd 方式运行 rsync 服务。rsync 的 xinetd 配置文件在 /etc/xinetd.d/rsync
要配置以 xinetd 运行的 rsync 服务需要执行如下的命令:

启动rsync服务端(以xinetd超级进程启动)        ##现在基本都不用了
[Wed Apr 15 11:27:38 1041 /dev/pts/0 192.168.2.250 /etc/xinetd.d]#yum install xinetd
[Wed Apr 15 11:29:14 1047 /dev/pts/0 192.168.2.250 /etc/xinetd.d]#service xinetd start
Starting xinetd:                                           [  OK  ]
[Wed Apr 15 11:29:48 1051 /dev/pts/0 192.168.2.250 /etc/xinetd.d]#lsof -i :873
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
xinetd  6148 root    5u  IPv6  34911      0t0  TCP *:rsync (LISTEN)

Rsync的命令格式可以为以下六种:
  rsync [OPTION]... SRC DEST
  rsync [OPTION]... SRC [USER@]HOST:DEST
  rsync [OPTION]... [USER@]HOST:SRC DEST
  rsync [OPTION]... [USER@]HOST::SRC DEST
  rsync [OPTION]... SRC [USER@]HOST::DEST
  rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
常用为以下两种:
(4)、rsync [OPTION]... [USER@]HOST::SRC   DEST
从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。
如:rsync -av root@172.16.78.192::www /databack
(5)、rsync [OPTION]... SRC   [USER@]HOST::DEST
从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。
如:rsync -av /databack root@172.16.78.192::www


防火墙上配置好映射端口
客户端安装
[root@aster5 ~]# yum install rsync
[root@aster5 ~]# vi /etc/rsync.pass
[root@aster5 oi]# cat /etc/rsync.pass
123456

[root@aster5 oi]# chmod 600 /etc/rsync.pass
[root@aster5 oi]# rsync -av --delete --progress rsync@125.76.228.16::dbback ./ --password-file=/etc/rsync.pass
welcome to our rsync server

receiving incremental file list
deleting rsyslog.conf
./

sent 69 bytes  received 1643 bytes  1141.33 bytes/sec
total size is 69585  speedup is 40.65

--delete:本地与服务器完全一样,如果本地存在不一样的,则删除,慎用
--password-file:指定密码文件,如果不指定,则需手动输入
-a :参数,相当于-rlptgoD,-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件;
-z :传输时压缩;
-P :传输进度;
-v :传输时的进度等信息
以后同步时,只会同步新增内容。
最后将上述命令加入计划任务定时同步即可。

通过计划任务做定期备份

也可以通过inotify做实时同步

什么是sersync?

sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。目前使用的比较多的同步解决方案是inotify-tools+rsync ,另外一个是google开源项目Openduckbill(依赖于inotify- tools),这两个都是基于脚本语言编写的。相比较上面两个项目,本项目优点是:
sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤,所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。
单纯使用rsync和使用serync+rsync的区别:

使用rsync+crontab做定时同步时,主服务器端开启rsync守护进程,而镜像服务器是运行rsync客户端,平时一般会利用crontab定时获取rsync服务器上的数据。
但使用rsync+sersync做实时同步时,用于推送文件的服务器运行sersync服务,用于接收文件的服务器则运行rsync守护进程,简单来说就是sersync会利用rsync命令将文件推送到rsync服务器,实际线上使用一般会把sersync作为主服务器,rsync作为镜像服务器,实现数据同步备份,web镜像等功能。

前提条件:
在使用sersync之前,我们必须安装配置好rsync服务器。这里我们需要注意的是,纯粹的使用rsync做单向同步时,rsync的守护进程是运行在文件推送的服务器上,而接收的服务器是运行rsync客户端。使用sersync做文件实时同步刚好相反,用于接收文件的服务器运行rsync守护进程。

我如何通过rsync只复制目录结构,忽略掉文件呢?

[root@aster5 oi]# rsync -av --delete --progress --include '*/' --exclude '*'  rsync@125.76.228.16::dbback ./ --password-file=/etc/rsync.pass
welcome to our rsync server

receiving incremental file list
./
awk/
awk/test1/
cmd/
frag/
frag/aa/
grep/
prog/
prog/rec/
sed/

sent 111 bytes  received 331 bytes  884.00 bytes/sec
total size is 0  speedup is 0.00

rsync可以直接通过873端口的tcp连接传文件,也可以通过22端口的ssh来进行文件传递,但你也可以通过下列命令改变它的端口:
rsync --port 8730 otherhost::
或者
rsync -e 'ssh -p 2002' otherhost:


命令行选项,优先级最高

其次显式地在rsyncd.conf中定义

最后没有在命令行指定,也没有在配文中指定的,就用默认值。

man rsync.conf

man rsyncd.conf

FILE FORMAT

The file consists of modules and parameters. A module begins with the name of the module in square brackets and continues until the next module begins. Modules contain parameters of the form "name = value".
文件由模块和参数组成。一个模块以在方括号里的模块名开始一直到下一个模块开始。模块包括形如"name = value"的参数。
The file is line-based — that is, each newline-terminated line represents either a comment, a module name or a parameter.
文件是基于行的,就是说,每一个以新行符结束的行要么代表一个注释,一个模块名或一个参数。
Only the first equals sign in a parameter is significant. Whitespace before or after the first equals sign is discarded. Leading, trailing and  internal
whitespace  in module and parameter names is irrelevant. Leading and trailing whitespace in a parameter value is discarded. Internal whitespace within a
parameter value is retained verbatim.
在参数中只有第一个等号是重要的。第一个等号之前或之后的空白符被丢弃。在模块和参数名开头,结尾和内部的空白符都是不相关的。在参数值的开头和结尾的空白符被丢弃。
参数值里的内部空白符逐字保留。
Any line beginning with a hash (#) is ignored, as are lines containing only whitespace.
任何以#号开始的行被忽略,这是只包括空白符的行
Any line ending in a is “continued” on the next line in the customary UNIX fashion.
任何以结束的行以传统的unix风格表示在下一行继续。
The values following the equals sign in parameters are all either a string (no quotes needed) or a boolean,  which  may  be  given  as  yes/no,  0/1  or
true/false. Case is not significant in boolean values, but is preserved in string values.
在参数等号后面的值要么是一个字符串(不需要引用),要么是一个布尔值,布尔值可以以yes/no,0/1或者true/false给出。在布尔值里这种情况不重要,但在字符串值里被保存。


LAUNCHING THE RSYNC DAEMON
The rsync daemon is launched by specifying the --daemon option to rsync.
rsync守护由指定--daemon选项给rsync来启动。
The daemon must run with root privileges if you wish to use chroot, to bind to a port numbered under 1024 (as is the default 873), or to set file owner-
ship.  Otherwise, it must just have permission to read and write the appropriate data, log, and lock files.
如果你希望使用chroot,绑定1024以下的一个端口号,或者设置文件属主的话,守护必须以root权限来运行。否则守护必须有权限读写恰当的数据,日志和锁文件。
You can launch it either via inetd, as a stand-alone daemon, or from an rsync client via a remote shell.  If run as a stand-alone daemon then  just  run
the command “rsync --daemon” from a suitable startup script.
你要么通过inetd启动它作为一个独立的守护,要么通过一个来自rsync客户端的远程shell来启动。如果运行为一个独立的守护,只要从一个合适的启动脚本运行命令"rsync --daemon"。
When run via inetd you should add a line like this to /etc/services:
当通过inetd运行时,你应该添加像一下面的行到/etc/services:
         rsync           873/tcp
and a single line something like this to /etc/inetd.conf:
和添加像下面的一个单行到/etc/inetd.conf:
         rsync   stream  tcp     nowait  root   /usr/bin/rsync rsyncd --daemon
Replace  “/usr/bin/rsync”  with  the path to where you have rsync installed on your system.  You will then need to send inetd a HUP signal to tell it to
reread its config file.
替换路径中的"/usr/bin/rsync"为在系统中已安装的rsync路径。你需要发送给inetd一个HUP信号来告诉它来重新读配置文件。
Note that you should not send the rsync daemon a HUP signal to force it to reread the rsyncd.conf file. The file is re-read on each client connection.
注意应该禁止给rsync守护发送HUP信号来强迫它重读rsyncd.conf文件。文件重读在每一个客户连接时。


GLOBAL PARAMETERS

The first parameters in the file (before a [module] header) are the global parameters.
在文件中(在一个模块头之前)的第一个参数是全局参数。
You may also include any module parameters in the global part of the config file in which case the supplied value will override the default for that parameter.
你可以包括任何模块参数在配置文件的全局部分,在这种情况下给出的值会覆盖参数的默认值。
You may use references to environment variables in the values of parameters.
你可以在参数值里引用环境变量。
String parameters will have %VAR% references expanded as late as possible (when the string is used in the program), allowing for the use of variables that rsync sets at connection time, such as RSYNC_USER_NAME.
字符串参数将%VAR%引用尽可能晚地扩展(当串在程序中使用时),允许
Non-string parameters (such as true/false settings) are expanded when read from the config file.
当读配置文件时,非字符串参数被扩展
If a variable does not exist in the environment, or if a sequence of characters is not a valid reference (such as an un-paired percent sign), the raw characters are passed through unchanged.
如果一个变量在环境中不存在,或字符顺序不是一个有效的引用(例如不配对的%),原始字符不加修改地被传递。
This helps with backward compatibility and safety (e.g. expanding a non-existent %VAR% to an empty string in a path could result in a very unsafe path).

The safest way to insert a literal % into a value is to use %%.
插入字符%到值里最安全的方法是使用%%。
motd file
    This parameter allows you to specify a "message of the day" to display to clients on each connect. This usually contains site information and any legal notices. The default is no motd file. This can be overridden by the --dparam=motdfile=FILE command-line option when starting the daemon.

pid file
    This parameter tells the rsync daemon to write its process ID to that file. If the file already exists, the rsync daemon will abort rather than overwrite the file. This can be overridden by the --dparam=pidfile=FILE command-line option when starting the daemon.

port
    You can override the default port the daemon will listen on by specifying this value (defaults to 873). This is ignored if the daemon is being run by inetd, and is superseded by the --port command-line option.

address
    You can override the default IP address the daemon will listen on by specifying this value. This is ignored if the daemon is being run by inetd, and is superseded by the --address command-line option.

socket options
    This parameter can provide endless fun for people who like to tune their systems to the utmost degree. You can set all sorts of socket options which may make transfers faster (or slower!).
这个参数能为想要最大限度地调优它们系统的人提供无尽的欢乐。你可以设置所有socket 选项的排序,这些socket选项可以使传输更快(或更慢)。
Read the man page for the setsockopt() system call for details on some of the options you may be able to set. By default no special socket options are set.
阅读你可能要设置的一些选项的setsockopt()系统调用的man手册页细节。
These settings can also be specified via the --sockopts command-line option.
这些设置能通过--sockopts 命令行选项被指定。
listen backlog
    You can override the default backlog value when the daemon listens for connections. It defaults to 5.

原文地址:https://www.cnblogs.com/createyuan/p/4428625.html