Linux系统——rsync命令

目录

一、rsync介绍

1.rsync简介 

2.rsync同步基本说明

二、rsync用法

1.rsync三种工作方式

2.rsync选项详解

不带任何选项

-t

-I(i)

-v

-z

-r

-l(L)

-p——perserve permissions(保持权限)

-g和-o

-D

-a

-delete、-delete-excluded、-delete-after

-exclude、-exclude-from

–partial

–progress


一、rsync介绍

rsync官方网站: https://www.samba.org/ftp/rsync/rsync.html

1.rsync简介 

rsync是可以实现增量备份的工具。配合任务计划,rsync能实现定时或间隔同步,配合inotify或sersync,可以实现触发式的实时同步。

rsync可以实现scp的远程拷贝(rsync不支持远程到远程的拷贝,但scp支持)、cp的本地拷贝、rm删除和"ls -l"显示文件列表等功能。但需要注意的是,rsync的最终目的或者说其原始目的是实现两端主机的文件同步,因此实现的scp/cp/rm等功能仅仅只是同步的辅助手段,且rsync实现这些功能的方式和这些命令是不一样的。事实上,rsync有一套自己的算法,其算法原理以及rsync对算法实现的机制可能比想象中要复杂一些。平时使用rsync实现简单的备份、同步等功能足以,没有多大必要去深究这些原理性的内容。但是想要看懂rsync命令的man文档、使用"-vvvv"分析rsync执行过程,以及实现rsync更强大更完整的功能,没有这些理论知识的支持是绝对不可能实现的。

2.rsync同步基本说明

rsync的目的是实现本地主机和远程主机上的文件同步(包括本地推到远程远程拉到本地两种同步方式),也可以实现本地不同路径下文件的同步,但不能实现远程路径1到远程路径2之间的同步(scp可以实现)。

不考虑rsync的实现细节,就文件同步而言,涉及了源文件和目标文件的概念,还涉及了以哪边文件为同步基准。例如,想让目标主机上的文件和本地文件保持同步,则是以本地文件为同步基准,将本地文件作为源文件推送到目标主机上。反之,如果想让本地主机上的文件和目标主机上的文件保持同步,则目标主机上的文件为同步基准,实现方式是将目标主机上的文件作为源文件拉取到本地。当然,要保持本地的两个文件相互同步,rsync也一样能实现,这就像Linux中cp命令一样,以本地某文件作为源,另一文件作为目标文件,但请注意,虽然rsync和cp能达到相同的目的,但它们的实现方式是不一样的。

既然是文件同步,在同步过程中必然会涉及到源和目标两文件之间版本控制的问题,例如是否要删除源主机上没有但目标上多出来的文件,目标文件比源文件更新(newer than source)时是否仍要保持同步,遇到软链接时是拷贝软链接本身还是拷贝软链接所指向的文件,目标文件已存在时是否要先对其做个备份等等。

rsync同步过程中由两部分模式组成:决定哪些文件需要同步的检查模式以及文件同步时的同步模式。

  • 检查模式是指按照指定规则来检查哪些文件需要被同步,例如哪些文件是明确被排除不传输的。默认情况下,rsync使用"quick check"算法快速检查源文件和目标文件的大小、mtime(修改时间)是否一致,如果不一致则需要传输。当然,也可以通过在rsync命令行中指定某些选项来改变quick check的检查模式,比如"--size-only"选项表示"quick check"将仅检查文件大小不同的文件作为待传输文件。rsync支持非常多的选项,其中检查模式的自定义性是非常有弹性的。
  • 同步模式是指在文件确定要被同步后,在同步过程发生之前要做哪些额外工作。例如上文所说的是否要先删除源主机上没有但目标主机上有的文件,是否要先备份已存在的目标文件,是否要追踪链接文件等额外操作。rsync也提供非常多的选项使得同步模式变得更具弹性。

相对来说,为rsync手动指定同步模式的选项更常见一些,只有在有特殊需求时才指定检查模式,因为大多数检查模式选项都可能会影响rsync的性能。

二、rsync用法

1.rsync三种工作方式

Local:  rsync [OPTION...] SRC... [DEST]
 
Access via remote shell:
  Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
  Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
 
Access via rsync daemon:
  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
  • 本地文件系统上实现同步。命令行语法格式为上述"Local"段的格式。
  • 本地主机使用远程shell和远程主机通信。命令行语法格式为上述"Access via remote shell"段的格式。
  • 本地主机通过网络套接字连接远程主机上的rsync daemon。命令行语法格式为上述"Access via rsync daemon"段的格式。

前两者的本质是通过管道通信,即使是远程shell。而方式(3)则是让远程主机上运行rsync服务,使其监听在一个端口上,等待客户端的连接。

但是,还有第四种工作方式:通过远程shell也能临时启动一个rsync daemon,这不同于第三种方式,它不要求远程主机上事先启动rsync服务,而是临时派生出rsync daemon,它是单用途的一次性daemon,仅用于临时读取daemon的配置文件,当此次rsync同步完成,远程shell启动的rsync daemon进程也会自动消逝。此通信方式的命令行语法格式同"Access via rsync daemon",但要求options部分必须明确指定"--rsh"选项或其短选项"-e"。

以下是对rsync语法的简单说明,由于rsync支持一百多个选项,所以此处只介绍几个常用选项。完整的选项说明以及rsync的使用方法见我翻译的"man rsync"。

Local:  rsync [OPTION...] SRC... [DEST]
 
Access via remote shell:
  Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
  Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
 
Access via rsync daemon:
  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

其中,第一个路径参数一定是源文件路径,即作为同步基准的一方,可以同时指定多个源文件路径。最后一个路径参数则是目标文件路径,也就是待同步方。路径的格式可以是本地路径,也可以是使用user@host:path或user@host::path的远程路径,如果主机和path路径之间使用单个冒号隔开,表示使用的是远程shell通信方式,而使用双冒号隔开的则表示的是连接rsync daemon。另外,连接rsync daemon时,还提供了URL格式的路径表述方式rsync://user@host/path。

如果仅有一个SRC或DEST参数,则将以类似于"ls -l"的方式列出源文件列表(只有一个路径参数,总会认为是源文件),而不是复制文件。

如果对rsync不熟悉,可暂先只了解本地以及远程shell格式的user@host:path路径格式。例如:

[root@localhost ~]# rsync /etc/fstab /tmp                # 在本地同步
[root@localhost ~]# rsync -r /etc 172.16.10.5:/tmp       # 将本地/etc目录拷贝到远程主机的/tmp下,以保证远程/tmp目录和本地/etc保持同步
[root@localhost ~]# rsync -r 172.16.10.5:/etc /tmp       # 将远程主机的/etc目录拷贝到本地/tmp下,以保证本地/tmp目录和远程/etc保持同步
[root@localhost ~]# rsync /etc/                          # 列出本地/etc/目录下的文件列表
[root@localhost ~]# rsync 172.16.10.5:/tmp/              # 列出远程主机上/tmp/目录下的文件列表

另外,使用rsync一定要注意的一点是,源路径如果是一个目录的话,带上尾随斜线和不带尾随斜线是不一样的,不带尾随斜线表示的是整个目录包括目录本身,带上尾随斜线表示的是目录中的文件,不包括目录本身。例如:

[root@localhost ~]# rsync -a /etc /tmp
#在/tmp目录下创建etc目录
[root@localhost ~]# rsync -a /etc/ /tmp
#不会在/tmp目录下创建etc目录,源路径/etc/中的所有文件都直接放在/tmp目录下

2.rsync选项详解

[root@localhost ~]#rsync -h
rsync  version 3.0.9  protocol version 30
Copyright (C) 1996-2011 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
    64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
    append, ACLs, xattrs, iconv, symtimes

rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU
General Public Licence for details.

rsync is a file transfer program capable of efficient remote update
via a fast differencing algorithm.

Usage: rsync [OPTION]... SRC [SRC]... DEST
  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
  or   rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
  or   rsync [OPTION]... [USER@]HOST:SRC [DEST]
  or   rsync [OPTION]... [USER@]HOST::SRC [DEST]
  or   rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
The ':' usages connect via remote shell, while '::' & 'rsync://' usages connect
to an rsync daemon, and require SRC or DEST to start with a module name.

Options
 -v, --verbose               increase verbosity
#显示rsync过程中详细信息。可以使用"-vvvv"获取更详细信息。
 -q, --quiet                 suppress non-error messages
     --no-motd               suppress daemon-mode MOTD (see manpage caveat)
 -c, --checksum              skip based on checksum, not mod-time & size
 -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
#归档模式,表示递归传输并保持文件属性。等同于"-rtopgDl"。
     --no-OPTION             turn off an implied OPTION (e.g. --no-D)
 -r, --recursive             recurse into directories
#递归到目录中去。
 -R, --relative              use relative path names
#使用相对路径。意味着将命令行中指定的全路径而非路径最尾部的文件名发送给服务端,包括它们的属性。用法见下文示例。
     --no-implied-dirs       don't send implied dirs with --relative
 -b, --backup                make backups (see --suffix & --backup-dir)
#对目标上已存在的文件做一个备份,备份的文件名后默认使用"~"做后缀。
     --backup-dir=DIR        make backups into hierarchy based in DIR
#指定备份文件的保存路径。不指定时默认和待备份文件保存在同一目录下。
     --suffix=SUFFIX         set backup suffix (default ~ w/o --backup-dir)
 -u, --update                skip files that are newer on the receiver
#仅在源mtime比目标已存在文件的mtime新时才拷贝。注意,该选项是接收端判断的,不会影响删除行为。
     --inplace               update destination files in-place (SEE MAN PAGE)
     --append                append data onto shorter files
     --append-verify         like --append, but with old data in file checksum
 -d, --dirs                  transfer directories without recursing
#以不递归的方式拷贝目录本身。默认递归时,如果源为"dir1/file1",则不会拷贝dir1目录,使用该选项将拷贝dir1但不拷贝file1。
 -l, --links                 copy symlinks as symlinks
 -L, --copy-links            transform symlink into referent file/dir
#如果文件是软链接文件,则拷贝软链接本身而非软链接所指向的对象。
     --copy-unsafe-links     only "unsafe" symlinks are transformed
     --safe-links            ignore symlinks that point outside the source tree
 -k, --copy-dirlinks         transform symlink to a dir into referent dir
 -K, --keep-dirlinks         treat symlinked dir on receiver as dir
 -H, --hard-links            preserve hard links
 -p, --perms                 preserve permissions
#显示文件传输的进度信息。(实际上"-P"="--partial --progress",其中的"--progress"才是显示进度信息的)。保持perms属性(权限,不包括特殊权限)。
 -E, --executability         preserve the file's executability
     --chmod=CHMOD           affect file and/or directory permissions
 -A, --acls                  preserve ACLs (implies --perms)
 -X, --xattrs                preserve extended attributes
 -o, --owner                 preserve owner (super-user only)
#保持owner属性(属主)。
 -g, --group                 preserve group
#保持group属性(属组)。
     --devices               preserve device files (super-user only)
     --copy-devices          copy device contents as regular file
     --specials              preserve special files
 -D                          same as --devices --specials
#是"--device --specials"选项的组合,即也拷贝设备文件和特殊文件。
 -t, --times                 preserve modification times
#保持mtime属性。强烈建议任何时候都加上"-t",否则目标文件mtime会设置为系统时间,导致下次更新
 -O, --omit-dir-times        omit directories from --times
     --super                 receiver attempts super-user activities
     --fake-super            store/recover privileged attrs using xattrs
 -S, --sparse                handle sparse files efficiently
 -n, --dry-run               perform a trial run with no changes made
#仅测试传输,而不实际传输。常和"-vvvv"配合使用来查看rsync是如何工作的。
 -W, --whole-file            copy files whole (without delta-xfer algorithm)
#rsync将不再使用增量传输,而是全量传输。在网络带宽高于磁盘带宽时,该选项比增量传输更高效。
 -x, --one-file-system       don't cross filesystem boundaries
 -B, --block-size=SIZE       force a fixed checksum block-size
 -e, --rsh=COMMAND           specify the remote shell to use
#指定所要使用的远程shell程序,默认为ssh。
     --rsync-path=PROGRAM    specify the rsync to run on the remote machine
     --existing              skip creating new files on receiver
#要求只更新目标端已存在的文件,目标端还不存在的文件不传输。注意,使用相对路径时如果上层目录不存在也不会传输。
     --ignore-existing       skip updating files that already exist on receiver
#要求只更新目标端不存在的文件。和"--existing"结合使用有特殊功能
     --remove-source-files   sender removes synchronized files (non-dirs)
#要求删除源端已经成功传输的文件。
     --del                   an alias for --delete-during
     --delete                delete extraneous files from destination dirs
#以SRC为主,对DEST进行同步。多则删之,少则补之。注意"--delete"是在接收端执行的,所以它是在
     --delete-before         receiver deletes before transfer, not during
     --delete-during         receiver deletes during the transfer
     --delete-delay          find deletions during, delete after
     --delete-after          receiver deletes after transfer, not during
     --delete-excluded       also delete excluded files from destination dirs
     --ignore-errors         delete even if there are I/O errors
     --force                 force deletion of directories even if not empty
     --max-delete=NUM        don't delete more than NUM files
     --max-size=SIZE         don't transfer any file larger than SIZE
#限制rsync传输的最大文件大小。可以使用单位后缀,还可以是一个小数值(例如:"--max-size=1.5m")
     --min-size=SIZE         don't transfer any file smaller than SIZE
#限制rsync传输的最小文件大小。这可以用于禁止传输小文件或那些垃圾文件。
     --partial               keep partially transferred files
     --partial-dir=DIR       put a partially transferred file into DIR
     --delay-updates         put all updated files into place at transfer's end
 -m, --prune-empty-dirs      prune empty directory chains from the file-list
     --numeric-ids           don't map uid/gid values by user/group name
     --timeout=SECONDS       set I/O timeout in seconds
     --contimeout=SECONDS    set daemon connection timeout in seconds
 -I, --ignore-times          don't skip files that match in size and mod-time
     --size-only             skip files that match in size
#默认算法是检查文件大小和mtime不同的文件,使用此选项将只检查文件大小。
     --modify-window=NUM     compare mod-times with reduced accuracy
 -T, --temp-dir=DIR          create temporary files in directory DIR
 -y, --fuzzy                 find similar file for basis if no dest file
     --compare-dest=DIR      also compare destination files relative to DIR
     --copy-dest=DIR         ... and include copies of unchanged files
     --link-dest=DIR         hardlink to files in DIR when unchanged
 -z, --compress              compress file data during the transfer
#传输时进行压缩提高效率。
     --compress-level=NUM    explicitly set compression level
     --skip-compress=LIST    skip compressing files with a suffix in LIST
 -C, --cvs-exclude           auto-ignore files the same way CVS does
 -f, --filter=RULE           add a file-filtering RULE
 -F                          same as --filter='dir-merge /.rsync-filter'
                             repeated: --filter='- .rsync-filter'
     --exclude=PATTERN       exclude files matching PATTERN
#指定排除规则来排除不需要传输的文件。
     --exclude-from=FILE     read exclude patterns from FILE
     --include=PATTERN       don't exclude files matching PATTERN
     --include-from=FILE     read include patterns from FILE
     --files-from=FILE       read list of source-file names from FILE
 -0, --from0                 all *-from/filter files are delimited by 0s
 -s, --protect-args          no space-splitting; only wildcard special-chars
     --address=ADDRESS       bind address for outgoing socket to daemon
     --port=PORT             specify double-colon alternate port number
#连接daemon时使用的端口号,默认为873端口。
     --sockopts=OPTIONS      specify custom TCP options
     --blocking-io           use blocking I/O for the remote shell
     --stats                 give some file-transfer stats
 -8, --8-bit-output          leave high-bit chars unescaped in output
 -h, --human-readable        output numbers in a human-readable format
     --progress              show progress during transfer
 -P                          same as --partial --progress
 -i, --itemize-changes       output a change-summary for all updates
     --out-format=FORMAT     output updates using the specified FORMAT
     --log-file=FILE         log what we're doing to the specified FILE
     --log-file-format=FMT   log updates using the specified FMT
     --password-file=FILE    read daemon-access password from FILE
#daemon模式时的密码文件,可以从中读取密码实现非交互式。注意,这不是远程shell认证的密码,而是rsync模块认证的密码。
     --list-only             list the files instead of copying them
     --bwlimit=KBPS          limit I/O bandwidth; KBytes per second
     --write-batch=FILE      write a batched update to FILE
     --only-write-batch=FILE like --write-batch but w/o updating destination
     --read-batch=FILE       read a batched update from FILE
     --protocol=NUM          force an older protocol version to be used
     --iconv=CONVERT_SPEC    request charset conversion of filenames
     --checksum-seed=NUM     set block/file checksum seed (advanced)
 -4, --ipv4                  prefer IPv4
 -6, --ipv6                  prefer IPv6
     --version               print version number
(-h) --help                  show this help (-h is --help only if used alone)

Use "rsync --daemon --help" to see the daemon-mode command-line options.
Please see the rsync(1) and rsyncd.conf(5) man pages for full documentation.
See http://rsync.samba.org/ for updates, bug reports, and answers

虽然选项非常多,但最常用的选项组合是"avz",即压缩和显示部分信息,并以归档模式传输。

不带任何选项

rsync main.c machineB:/home/userB
  • 只要目的端的文件内容和源端不一样,就会触发数据同步,rsync会确保两边的文件内容一样。
  • 但rsync不会同步文件的“modify time”,凡是有数据同步的文件,目的端的文件的“modify time”总是会被修改为最新时刻的时间。
  • rsync不会太关注目的端文件的rwx权限,如果目的端没有此文件,那么权限会保持与源端一致;如果目的端有此文件,则权限不会随着源端变更。
  • 只要rsync有对源文件的读权限,且对目标路径有写权限,rsync就能确保目的端文件同步到和源端一致。
  • rsync只能以登陆目的端的账号来创建文件,它没有能力保持目的端文件的输主和属组和源端一致。(除非你使用root权限,才有资格要求属主一致、属组一致)

-t

rsync -t main.c machineB:/home/userB
  • 使用-t选项后,rsync总会想着一件事,那就是将源文件的“modify time”同步到目标机器。
  • 带有-t选项的rsync,会变得更聪明些,它会在同步前先对比两边文件的时间戳和文件大小,如果一致,则就认为两边文件一样,对此文件就不再采取更新动作了。
  • 因为rsync的聪明,也会反被聪明误。如果目的端的文件的时间戳、大小和源端完全一致,但是内容恰巧不一致时,rsync是发现不了的。这就是传说中的“坑”!
  • 对于rsync自作聪明的情况,解决办法就是使用-I选项。

-I(i)

rsync -I main.c machineB:/home/userB
  • -I选项会让rsync变得很乖很老实,它会挨个文件去发起数据同步。
  • -I选项可以确保数据的一致性,代价便是速度上会变慢,因为我们放弃了“quick check”策略。(quick check策略,就是先查看文件的时间戳和文件大小,依次先排除一批认为相同的文件)
  • 无论情况如何,目的端的文件的modify time总会被更新到当前时刻。

-v

rsync -vI main.c machineB:/home/userB                                                   
main.c

sent 81 bytes  received 42 bytes  246.00 bytes/sec
total size is 11  speedup is 0.09

你增加越多的v,就可以获得越多的日志信息。 

rsync -vvvvt abc.c machineB:/home/userB 
cmd= machine=machineB user= path=/home/userB
cmd[0]=ssh cmd[1]=machineB cmd[2]=rsync cmd[3]=--server cmd[4]=-vvvvte. cmd[5]=. cmd[6]=/home/userB 
opening connection using: ssh machineB rsync --server -vvvvte. . /home/userB 
note: iconv_open("ANSI_X3.4-1968", "ANSI_X3.4-1968") succeeded.
(Client) Protocol versions: remote=28, negotiated=28
(Server) Protocol versions: remote=30, negotiated=28
[sender] make_file(abc.c,*,2)
[sender] flist start=0, used=1, low=0, high=0
[sender] i=0  abc.c mode=0100664 len=11 flags=0
send_file_list done
file list sent
send_files starting
server_recv(2) starting pid=31885
recv_file_name(abc.c)
received 1 names
[receiver] i=0   abc.c mode=0100664 len=11
recv_file_list done
get_local_name count=1 /home/userB
recv_files(1) starting
generator starting pid=31885 count=1
delta transmission enabled
recv_generator(abc.c,0)
abc.c is uptodate
generate_files phase=1
send_files phase=1
recv_files phase=1
generate_files phase=2
send files finished
total: matches=0  hash_hits=0  false_alarms=0 data=0
generate_files finished
recv_files finished
client_run waiting on 14318

sent 36 bytes  received 16 bytes  104.00 bytes/sec
total size is 11  speedup is 0.21
_exit_cleanup(code=0, file=main.c, line=1031): entered
_exit_cleanup(code=0, file=main.c, line=1031): about to call exit(0)

-z

这是个压缩选项,只要使用了这个选项,rsync就会把发向对端的数据先进行压缩再传输。对于网络环境较差的情况下建议使用。

一般情况下,-z的压缩算法会和gzip的一样。

-r

rsync superman machineB:/home/userB
skipping directory superman

如果你不额外告诉rsync你需要它帮你同步文件夹的话,它是不会主动承担的,这也正是rsync的懒惰之处。

所以,如果你真的想同步文件夹,那就要加上-r选项,即recursive(递归的、循环的),像这样:

rsync -r superman machineB:/home/userB

我们在上面的讲解中说过,如果时间戳和文件大小完全一致,只有文件内容不同,且你没有使用-I选项的话,那么,rsync是不会进行数据同步的。

那么,提个问题:“因为在Linux的世界里,文件夹也是文件,如果这类文件(文件夹)也只有内容不同,而时间戳和文件大小都相同,rsync会发现么?”

实验大家可以自己动手做,结论在这里告诉大家:

对于文件夹,rsync是会明察秋毫的,只要你加了-r选项,它就会恪尽职守的进入到文件夹里去检查,而不会只对文件夹本身做“quick check”的。

-l(L)

ll
total 128
-rw-rw-r--  1 userA userA 11 Dec 26 07:00 abc.c
lrwxrwxrwx  1 userA userA  5 Dec 26 11:35 softlink -> abc.c


rsync softlink machineB:/home/userB
skipping non-regular file "softlink"

rsync一旦发现某个文件是软链接,就会无视它,除非我们增加-l选项。

rsync -l softlink machineB:/home/userB

使用了-l选项后,rsync会完全保持软链接文件类型,原原本本的将软链接文件复制到目的端,而不会“follow link”到指向的实体文件。

-p——perserve permissions(保持权限)

  • 如果目的端没有此文件,那么在同步后会将目的端文件的权限保持与源端一致;
  • 如果目的端已存在此文件,那么只会同步文件内容,权限保持原有不变。

如果你使用了-p选项,则无论如何,rsync都会让目的端保持与源端的权限一致的。

-g和-o

这两个选项是一对,用来保持文件的属组(group)和属主(owner),作用应该很清晰明了。不过要注意的一点是,改变属主和属组,往往只有管理员权限才可以。

-D

原文解释是“preserve devices(root only)”,从字面意思看,就是保持设备文件的原始信息。

-a

  • -a选项是rsync里比较霸道的一个选项,因为你使用-a选项,就相当于使用了-rlptgoD这一坨选项。以一敌七,唯-a选项也。(在看了前文之后,你应该可以很轻松的理解这七个选项的作用了)
  • -a选项的学名应该叫做archive option,中文叫做归档选项。使用-a选项,就表明你希望采取递归方式来同步,且尽可能的保持各个方面的一致性。
  • 但是-a选项也有不足,那就是-a无法同步“硬链接”情况。如果有这方面需求,要加上-H选项。

-delete、-delete-excluded、-delete-after

三个选项都是和“删除”有关的:

  • –delete:如果源端没有此文件,那么目的端也别想拥有,删除之。(如果你使用这个选项,就必须搭配-r选项一起)
  • –delete-excluded:专门指定一些要在目的端删除的文件。
  • –delete-after:默认情况下,rsync是先清理目的端的文件再开始数据同步;如果使用此选项,则rsync会先进行数据同步,都完成后再删除那些需要清理的文件。

-exclude、-exclude-from

如果你不希望同步一些东西到目的端的话,可以使用–exclude选项来隐藏,rsync还是很重视大家隐私的,你可以多次使用–exclude选项来设置很多的“隐私”。

如果你要隐藏的隐私太多的话,在命令行选项中设置会比较麻烦,rsync还是很体贴,它提供了–exclude-from选项,让你可以把隐私一一列在一个文件里,然后让rsync直接读取这个文件就好了。

–partial

 这就是传说中的断点续传功能。默认情况下,rsync会删除那些传输中断的文件,然后重新传输。但在一些特别情况下,我们不希望重传,而是续传。

–progress

使用这个选项,rsync会显示出传输进度信息

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值