rsync 学习笔记

    技术2022-05-19  26

    rsync 学习笔记

     

    本文章由 vincentzhwg 所写,转载请注明作者:vincentzhwg

     

    rsync 在同步时至少一方是本机。

     

    1:rsync用法的基本格式

    rsync [OPTION...] SRC... [DEST]

     

    2:示例用法说明:

    2.1:

    rsync -t *.c foo:src/

    传输当前文件夹内的以 .c 结尾的文件到机器 foo 的 src 文件夹中。当文件在 foo 的 src 文件夹中已存在时,将使用远程更新协议只传输文件中不同的部分。

     

    2.2:

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

    使用压缩方式(可以减少传输数据的容量),递归方式(即包含目录下的所有目录与文件)地把 foo 机器上的 src/bar 文件夹下的所有文件传输到本地机器上的 /dta/tmp 目录下,且文件保持其原有属性,如所有者,权限,链接等。在来源

     

    2.3:

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

    相比上例,这只是在来源处的最后加多了一个 / 符号,这样可以避免在目标处新建多一层目录。你可以这样认为,来源处最后加 / 符号,代表复制来源处目录下的内容,若不加则是使用来源处该目录名在目标处下建立同样一个目录。但加与不加 / 符号的两种情况,都会传输来源处目录的属性到目标处的目录上。换句话说就是,下面两种命令,都会起到同样的作用:

    rsync -av /src/foo /dest

    rsync -av /src/foo/ dest/foo

    同样注意到, host 与 module 不需要末尾追加 / 符号来复制到默认文件夹。如下两条命令都是把远程内容复制到 /dest 目录下:

    rsync -av host: /dest

    rsync -av host::module /dest

    你同样可以只单纯在本机上使用 rsync 命令,这样在目标与来源处不需要加上 : 符号,在这种情况下,命令行为有些像改进版的 copy 命令。

    2.4:

    rsync somehost.mydomain.com::

    列出来源于某一个特定 daemon 的所有可用模块,通过留空 module 部分

     

    3:高级用法

    3.1:

    从远程主机上请求多份文件的语法,可通过一开始指定附加的远程主机参数,或忽略主机名。下面的命令都可以起到类似的作用

    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}

    3.2:

    旧版的 rsync 需要在来源处加上引用,如下

    rsync -av host:'dir1/file1 dir2/file2' /dest

    rsync host::'modname/dir1/file1 modname/dir2/file2' /dest

    3.3:

    如果需要传输的文件名中包含有空格,一种方法是加上 --protect-args(-s) 参数选项,另一种方法是参照如下命令:

    rsync -av host:'file/ name/ with/ spaces' /dest

     

    4:参数选项概括

    -v, --verbose               参数选项更加详细的说明

    -q, --quiet                 抑制非错误信息的输出

        --no-motd               suppress daemon-mode MOTD (see caveat)

    -c, --checksum              跳过基础测试

    -a, --archive               压缩模式

        --no-OPTION             关闭某一个隐藏的选项

    -r, --recursive             递归模式

    -R, --relative              使用相对路径,即会保持完全路径信息。举例: rsync -R foo/bar/foo.c remote:/tmp/ ,则会在远程机上创建 /tmp/foo/bar/foo.c 文件。

        --no-implied-dirs       不使用路径中的一些目录

    -b, --backup                创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。 

        --backup-dir=DIR        将备份文件(如~filename)存放在在目录下

        --suffix=SUFFIX         定义备份文件前缀 

    -u, --update                仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件) 

        --inplace               update destination files in-place

        --append                append data onto shorter files

        --append-verify         --append w/old data in file checksum

    -d, --dirs                  非递归方式传送文件夹

    -l, --links                 保留软链接

    -L, --copy-links            对待常规文件一样处理软链结 

        --copy-unsafe-links     仅仅拷贝指向SRC路径目录树以外的链结

        --safe-links            忽略指向SRC路径目录树以外的链结 

    -k, --copy-dirlinks         transform symlink to dir into referent dir

    -K, --keep-dirlinks         treat symlinked dir on receiver as dir

    -H, --hard-links            保留硬链接

    -p, --perms                 保留权限

    -E, --executability         保留可执行性

        --chmod=CHMOD           修改文件或目录的权限值

    -A, --acls                  preserve ACLs (implies -p)

    -X, --xattrs                保留额外属性

    -o, --owner                 保留所属用户值

    -g, --group                 保留所属组别值

        --devices               preserve device files (super-user only)

        --specials              preserve special files

    -D                          same as --devices --specials

    -t, --times                 保留时间信息

    -O, --omit-dir-times        omit directories from --times

        --super                 receiver attempts super-user activities

        --fake-super            store/recover privileged attrs using xattrs

    -S, --sparse                对稀疏文件进行特殊处理以节省DST的空间

    -n, --dry-run               测试运行,不会做任何修改

    -W, --whole-file            拷贝文件,不进行增量检测

        --rsync-path=PROGRAM    specify the rsync to run on remote machine

        --existing              skip creating new files on receiver

        --ignore-existing       skip updating files that exist on receiver

        --remove-source-files   sender removes synchronized files (non-dir)

        --del                   an alias for --delete-during

        --delete                delete extraneous files from dest dirs

        --delete-before         receiver deletes before transfer (default)

        --delete-during         receiver deletes during xfer, not before

        --delete-delay          find deletions during, delete after

        --delete-after          receiver deletes after transfer, not before

        --delete-excluded       also delete excluded files from dest dirs

        --ignore-errors         delete even if there are I/O errors

        --force                 force deletion of dirs 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

        --min-size=SIZE         don't transfer any file smaller than SIZE

        --partial               keep partially transferred files

        --partial-dir=DIR       put a partially transferred file into DIR

        --delay-updates         put all updated files into place at end

    -m, --prune-empty-dirs      prune empty directory chains from 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          不跳过那些有同样的时间和长度的文件

        --size-only             skip files that match in size

        --modify-window=NUM     compare mod-times with reduced accuracy

    -T, --temp-dir=DIR          在DIR中创建临时文件

    -y, --fuzzy                 find similar file for basis if no dest file

        --compare-dest=DIR      also compare received 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-level=NUM    explicitly set compression level

        --skip-compress=LIST    skip compressing files with suffix in LIST

    -C, --cvs-exclude           使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件

    -f, --filter=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; wildcard chars only

        --address=ADDRESS       bind address for outgoing socket to daemon

        --port=PORT             specify double-colon alternate port number

        --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        以便于人阅读的方式输出数值

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

        --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 dest

        --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 (see below for -h comment)

     

    sync can also be run as a daemon, in which case the following options are accepted:

     

        --daemon                run as an rsync daemon

        --address=ADDRESS       bind to the specified address

        --bwlimit=KBPS          limit I/O bandwidth; KBytes per second

        --config=FILE           specify alternate rsyncd.conf file

        --no-detach             do not detach from the parent

        --port=PORT             listen on alternate port number

        --log-file=FILE         override the "log file" setting

        --log-file-format=FMT   override the "log format" setting

        --sockopts=OPTIONS      specify custom TCP options

    -v, --verbose               increase verbosity

    -4, --ipv4                  prefer IPv4

    -6, --ipv6                  prefer IPv6

    -h, --help                  show this help (if used after --daemon)


    最新回复(0)