【Linux之rsync远程同步】

rsync is a utility for efficiently transferring and synchronizing files across computer systems, by checking the timestamp and size of files.It is commonly found on Unix-like systems and functions as both a file synchronization and file transfer program. The rsync algorithm is a type of delta encoding, and is used for minimizing network usage. Zlib may be used for additional compression,and SSH or stunnel can be used for data security.

Rsync is typically used for synchronizing files and directories between two different systems. For example, if the command rsync local-file user@remote-host:remote-file is run, rsync will use SSH to connect as user to remote-host.Once connected, it will invoke the remote host's rsync and then the two programs will determine what parts of the file need to be transferred over the connection.

Rsync can also operate in a daemon mode, serving files in the native rsync protocol (using the "rsync://" syntax).

rsync是类unix系统下的数据镜像备份工具——remote sync。一款快速增量备份工具 Remote Sync,远程同步 支持本地复制,或者与其他SSH、rsync主机同步。

rsync,remote synchronize顾名思意就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加信息。 rsync是用 “rsync 算法”提供了一个客户机和远程文件服务器的文件同步的快速方法,而且可以通过ssh方式来传输文件,这样其保密性也非常好,另外它还是免费的软件。

扫描二维码关注公众号,回复: 252119 查看本文章

rsync特性如下:

可以镜像保存整个目录树和文件系统。

可以很容易做到保持原来文件的权限、时间、软硬链接等等。

无须特殊权限即可安装。

快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。

安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。

支持匿名传输,以方便进行网站镜象。

rsync有六种不同的工作模式:

1)拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。如:rsync -a /data /backup

2)使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。如:rsync -avz *.c foo:src

3)使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式。如:rsync -avz foo:src/bar /data

4)从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。如:rsync -av [email protected]::www /databack

5)从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。如:rsync -av /databack [email protected]::www

6)列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。如:rsync -v rsync://192.168.78.192/www

rsync 命令来同步系统文件之前要先登录remote 主机认证,认证过程中用到的协议有2种:ssh 协议和rsync协议

1. ssh 认证协议

rsync   server 端不用启动rsync的daemon进程,只要获取remote host的用户名和密码就可以直接 rsync 同步文件

rsync   server 端因为不用启动daemon进程,所以也不用配置文件 /etc/rsyncd.conf

ssh 认证协议跟scp 的原理是一样的,如果在同步过程中不需要收入密码就 用 ssh-keygen -t rsa  打通通道

这种方式默认是省略了 -e ssh 的,与下面等价:

rsync -avz /SRC -e ssh [email protected]:/DEST #-a 文件宿主变化,时间戳不变 -z:压缩数据传输

当遇到要修改端口的时候,我们可以:

rsync -avz /SRC -e "ssh -p36000" [email protected]:/DEST  #修改了ssh 协议的端口,默认是22

2. rsync 认证协议

rsync 认证协议,需要在rsync server端启动daemon进程,并设置对应的配置文件: /etc/rsyncd.conf 

rysnc 认证协议

猜你喜欢

转载自gaojingsong.iteye.com/blog/2379888