非常Linux——rsync之一

概述

rsync是什么?它是一个快速的、多功能远程(本地)文件复制工具

语法

本地文件复制语法:rsync [OPTION...] SRC... [DEST]

通过访问远程shell复制文件语法(ssh):

Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]

Push: rsync [OPTION...] SRC... [USER@]HOST:DEST

通过访问rsync服务进程复制文件语法:

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

Notice:如果只有一个SRC参数而没有DEST参数,则只会打印出SRC文件列表(类似ls -l)

简介

Rsync是一个快速的,多功能的文件复制工具。它可以通过远程Shell或rsync后台进程复制本地文件至远程主机或从远程主机复制到本地,甚至是本地复制到本地都是可以的,但不能从远程主机复制文件至另一个远程主机。Rsync用的是著名的delta-transfer 算法,这样使得rsync只复制源文件和目的文件的差异部分,因此减少了网络宽带资源的占用并提高了复制的速度。Rsync广泛用于备份和镜像,并可以当作是一个进化版的复制命令(cp)来使用。

Rsync使用"quick check"算法来查询哪些文件需要复制至目标端,该算法还会检查文件是大小的变化还是属性权限的变化,如果有属性改变,那么任何指定需要复制的属性(由Options指定)都会被复制至目标端。

Rsync的其他特性:

  • 支持复制links,devices,owners,groups,和permissions
  • exclude和exclude-from选项与GNU tar类似
  • CVS exclude模式可以忽略连CVS都会忽略的文件。
  • 可以使用任意透明的远程shell,包括ssh,rsh作为传输
  • 不需要超级管理员权限
  • 支持匿名或身份验证的rsync的后台服务(非常适合用于镜像)

Rsync可以使用两种方式连接远程主机:
  1. 使用远程shell程序作为传输(如ssh,rsh),当SRC或DEST文件路径中使用了单个':'号,即表示使用的是这种方式来传输资料。
  2. 直接通过TCP连接至rsync后台服务。当SRC或DEST文件路径中含有两个':'号,或rsync://URL方式,即表示使用的是这种方式来传输资料。
Notice:如果SRC或DEST都是本地文件目录,则是本地的文件复制操作了。
Notice:要使用Rsync的远程复制功能,必须在客户端及服务器端都安装Rsync

连接至rsync后台服务

连接至rsync后台服务使用的默认TCP端口号为873。这显然需要启动rsync的后台服务进程。

使用rsync后台服务连接方式与使用远程shell程序连接的区别是:

  • 使用两个':'或rsync://URL的方式指定路径来表示使用rsync后台服务连接方式。
  • 在路径后的第一个单词(主机后面)实际是一个module名字。
  • 在连接时,远程后台服务会打印message of the day(motd)
  • 如果没有指定远程后台服务路径,那么将打印可以访问的路径列表
  • 必须不能指定-e或--rsh选项
一些远程后台服务的modules需要身份验证的,这样的话,在连接的时候就需要你提供密码了。你也可以通过设置环境变量RSYNC_PASSWORD或使用--password-file选项来设定密码,这在脚本中使用rsync是非常有用的。

WARNING:在某些系统中,环境变量是所有用户可见的,因此在这些系统中推荐使用--password-file选项。

你可以通过网络代理来建立一个rsync连接。这需要设置RSYNC_PROXY环境变量了。环境变量的内容为hostname:port。当然了,网络代理的配置必须支持连接至端口873。

你也可以通过使用一个程序作为代理来建立rsync后台服务连接。这需要通过设置环境变量RSYNC_CONNECT_PROGPROXY。环境变量内容可以使用'%H'转义为命令行中的指定的主机名。例如:

         export RSYNC_CONNECT_PROG=’ssh proxyhost nc %H 873’
         rsync -av targethost1::module/src/ /dest/
         rsync -av rsync:://targethost2/module/src/ /dest/


上面环境变量指定的指令ssh在主机proxyhost上运行nc(netcat),nc能把所有指向873端口的数据传送至targethost(%H)


USING RSYNC-DAEMON FEATURES VIA A REMOTE-SHELL CONNECTION
       It is sometimes useful to use various features of an rsync daemon (such as named modules) without actually allowing any new socket  connections
       into a system (other than what is already required to allow remote-shell access).  Rsync supports connecting to a host using a remote shell and
       then spawning a single-use “daemon” server that expects to read its config file in the home dir of the remote user.  This can be useful if  you
       want  to  encrypt  a daemon-style transfer’s data, but since the daemon is started up fresh by the remote user, you may not be able to use fea-
       tures such as chroot or change the uid used by the daemon.  (For another way to encrypt a daemon transfer, consider using ssh to tunnel a local
       port to a remote machine and configure a normal rsync daemon on that remote host to only allow connections from “localhost”.)


       From  the user’s perspective, a daemon transfer via a remote-shell connection uses nearly the same command-line syntax as a normal rsync-daemon
       transfer, with the only exception being that you must explicitly set the remote shell  program  on  the  command-line  with  the  --rsh=COMMAND
       option.  (Setting the RSYNC_RSH in the environment will not turn on this functionality.)  For example:


           rsync -av --rsh=ssh host::module /dest


       If  you  need  to  specify  a different remote-shell user, keep in mind that the user@ prefix in front of the host is specifying the rsync-user
       value (for a module that requires user-based authentication).  This means that you must give the ’-l user’ option to ssh  when  specifying  the
       remote-shell, as in this example that uses the short version of the --rsh option:


           rsync -av -e "ssh -l ssh-user" rsync-user@host::module /dest


       The “ssh-user” will be used at the ssh level; the “rsync-user” will be used to log-in to the “module”.


STARTING AN RSYNC DAEMON TO ACCEPT CONNECTIONS
       In order to connect to an rsync daemon, the remote system needs to have a daemon already running (or it needs to have configured something like
       inetd to spawn an rsync daemon for incoming connections on a particular port).  For full information on how to start a daemon  that  will  han-
       dling  incoming  socket connections, see the rsyncd.conf(5) man page — that is the config file for the daemon, and it contains the full details
       for how to run the daemon (including stand-alone and inetd configurations).


       If you’re using one of the remote-shell transports for the transfer, there is no need to manually start an rsync daemon.


使用举例

rsync -t *.c foo:src/

把当前目录下所有后缀为c的文件复制至foo主机的src目录下,若在foot:src中已存在相同文件,则传输差别内容用于复制。-t表示连同mtime一起复制。

rsync -avz foo:src/bar /data/tmp

把foo:src/bar目录复制至本地/data/tmp目录。文件传输使用了archive模式,它能确保软链接,设备文件,属性,权限,拥有者属性也会一起复制至目标端。另外,-z表示以压缩后的数据来传输。

rsync -av /src/foo /dest
rsync -av /src/foo/ /dest/foo

上面两条指令的执行结果是一样的。如果SRC路径后面是一个'/'表示复制该目录下的内容,不包括此目录。如果没有'/',则表示连带目录一起复制至目标端。注意,这两种方式都会复制foo目录的属性至目标端。

rsync -av host:file1 :file2 host:file{3,4} /dest/
rsync -av host::modname/file{1,2} host::modname/file3 /dest/
rsync -av host::modname/file1 ::modname/file{3,4}

上面三条指令表示当有多个SRC时,而每个SRC的主机是一样的,那么后面的SRC可以省略host部分,并且在指定文件时可以使用{}来指定多个文件。

rsync -av host:’dir1/file1 dir2/file2’ /dest
rsync host::’modname/dir1/file1 modname/dir2/file2’ /dest

上面两条指令表示可以用单引号括起路径部分。

rsync -av host:’file\ name\ with\ spaces’ /dest

上面指令表示如果文件名中包含有空格时,需要使用'\'转义空格。或者使用--protect-args(-s)选项

rsync -Cavz . arvidsjaur:backup

备份当前目录至arvidsjaur:backup

rsync -az -e ssh --delete ~ftp/pub/samba nimbus:"~ftp/pub/tridge"

镜像~ftp/pub/samba和nimbus:"~ftp/pub/tridge"这两个目录


常用选项

-v,--verbose 列出详细信息
-q,--quiet 安静模式,不显示任何信息
-a,--archive 等于-rlptgoD(但不包括-H,-A,-X)
-r,--recursive 目录递归
-u,--update 如果目标端文件较源文件新,则跳过该文件
-l,--links 将软链接以软链接来复制
-p,--perms 复制权限属性

-t,--times 复制mtime

-g,--group 复制群组属性

-o,--owner 复制拥有者属性(只有root用户有权限操作)

-D 与--devices --specials相同

--devices 可复制设备文件(character or block file)

--specials 可复制特殊文件(sockets or fifos)

-e,--rsh=COMMAND 指定使用哪个远程shell程序

--delete 复制时同步删除文件,需要与--delete-WHEN一起使用,如果没有--delete-WHEN,那么默认是用--delete-during

--delete-before 在复制前先同步删除文件(尽量弃用)

--delete-during,--del 复制的同时也同步删除文件(常用)

--delete-delay 复制完成后再删除文件(常用)

--delete-after 与--delete-delay一样,在复制完成后再删除文件,但使用的算法较差(尽量弃用)

--delete-excluded 指定不删除哪些文件


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值