• Linux命令(122)之rsync


    linux命令之rsync

    1.rsync介绍

    linux命令rysnc是一个在linux系统上通过网络在系统间同步文件夹和文件的网络协议。

    前提:rsync需在远程和本地系统上安装

    2.rsync用法

    rsync [参数]

    rsync参数
    参数说明
    -v详细模式输出,传输时的进度等信息
    -z传输时进行压缩以提高传输效率
    -a归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rtopgdl
    -l会把软链接指向的目标文件复制到对端目录中
    -e指定使用ssh还是其他方式来数据同步
    --bwlimit=10限制传输速度
    --exclude=PATH表示指定排除不需要传输的文件,等号后面跟文件名,可以是万用字符模式(如:*.txt)
    --delete表示删除DST中RSC没有的文件

    3.实例

    3.1.本地目录传输

    命令:

    rsync -avz /root/245/ /opt/245

    1. [root@kibana ~]# rsync -avz /root/245/ /opt/245
    2. sending incremental file list
    3. created directory /opt/245
    4. ./
    5. 245.txt
    6. aa.txt
    7. bb.txt
    8. test -> /root/192.168.10.245.txt
    9. sent 308 bytes received 110 bytes 836.00 bytes/sec
    10. total size is 116 speedup is 0.28
    11. [root@kibana 245]# pwd
    12. /opt/245
    13. [root@kibana 245]# ls -l
    14. 总用量 12
    15. -rw-r--r-- 1 root root 52 1114 09:04 245.txt
    16. -rw-r--r-- 1 root root 16 1114 09:05 aa.txt
    17. -rw-r--r-- 1 root root 24 1114 09:06 bb.txt
    18. lrwxrwxrwx 1 root root 24 1114 09:05 test -> /root/192.168.10.245.txt
    19. [root@kibana 245]#

    3.2.增量同步

    命令:

    rsync -avz /root/245/ /opt/245

    1. [root@kibana 245]# pwd
    2. /root/245
    3. [root@kibana 245]# ls
    4. 245.txt aa.txt bb.txt test
    5. [root@kibana 245]# touch cc.txt
    6. [root@kibana 245]# echo cc >>cc.txt
    7. [root@kibana 245]# ll
    8. 总用量 16
    9. -rw-r--r-- 1 root root 52 1114 09:04 245.txt
    10. -rw-r--r-- 1 root root 16 1114 09:05 aa.txt
    11. -rw-r--r-- 1 root root 24 1114 09:06 bb.txt
    12. -rw-r--r-- 1 root root 3 1114 09:26 cc.txt
    13. lrwxrwxrwx 1 root root 24 1114 09:05 test -> /root/192.168.10.245.txt
    14. [root@kibana 245]# rsync -avz /root/245/ /opt/245 #首次,会在本地和远程之间拷贝所有内容;后续,只会传输发生改变的块或者字节。
    15. sending incremental file list
    16. ./
    17. cc.txt
    18. sent 229 bytes received 38 bytes 534.00 bytes/sec
    19. total size is 119 speedup is 0.45
    20. [root@kibana 245]#
    21. [root@kibana 245]# pwd
    22. /opt/245
    23. [root@kibana 245]# ls -l
    24. 总用量 16
    25. -rw-r--r-- 1 root root 52 1114 09:04 245.txt
    26. -rw-r--r-- 1 root root 16 1114 09:05 aa.txt
    27. -rw-r--r-- 1 root root 24 1114 09:06 bb.txt
    28. -rw-r--r-- 1 root root 3 1114 09:26 cc.txt
    29. lrwxrwxrwx 1 root root 24 1114 09:05 test -> /root/192.168.10.245.txt
    30. [root@kibana 245]# cat cc.txt
    31. cc
    32. [root@kibana 245]#

    3.3.本地目录传输(不拷贝软链接)

    命令:

    rsync -rvz /root/245/ /opt/245

    1. [root@kibana 245]# rsync -rvz /root/245/ /opt/245
    2. sending incremental file list
    3. created directory /opt/245
    4. skipping non-regular file "test"
    5. ./
    6. 245.txt
    7. aa.txt
    8. bb.txt
    9. cc.txt
    10. ztj/
    11. ztj/ztj.txt
    12. sent 454 bytes received 190 bytes 1,288.00 bytes/sec
    13. total size is 123 speedup is 0.19
    14. [root@kibana 245]#
    15. [root@kibana opt]# pwd
    16. /opt
    17. [root@kibana opt]# ls
    18. 245
    19. [root@kibana opt]# cd 245/
    20. [root@kibana 245]# ls
    21. 245.txt aa.txt bb.txt cc.txt ztj
    22. [root@kibana 245]# ll
    23. 总用量 16
    24. -rw-r--r-- 1 root root 52 1114 09:32 245.txt
    25. -rw-r--r-- 1 root root 16 1114 09:32 aa.txt
    26. -rw-r--r-- 1 root root 24 1114 09:32 bb.txt
    27. -rw-r--r-- 1 root root 3 1114 09:32 cc.txt
    28. drwxr-xr-x 2 root root 21 1114 09:32 ztj
    29. [root@kibana 245]#

    3.4.本地目录传输(不拷贝ztj目录)

    命令:

    rsync --exclude=ztj -avz /root/245/ /opt/245

    1. [root@kibana 245]# ls
    2. 245.txt aa.txt bb.txt cc.txt test zt
    3. [root@kibana 245]# rsync --exclude=ztj -avz /root/245/ /opt/245
    4. sending incremental file list
    5. ./
    6. 245.txt
    7. aa.txt
    8. bb.txt
    9. cc.txt
    10. test -> /root/192.168.10.245.txt
    11. sent 372 bytes received 98 bytes 940.00 bytes/sec
    12. total size is 119 speedup is 0.25
    13. [root@kibana 245]#
    14. [root@kibana 245]# pwd
    15. /opt/245
    16. [root@kibana 245]# ls
    17. 245.txt aa.txt bb.txt cc.txt test
    18. [root@kibana 245]# ll
    19. 总用量 16
    20. -rw-r--r-- 1 root root 52 1114 09:04 245.txt
    21. -rw-r--r-- 1 root root 16 1114 09:05 aa.txt
    22. -rw-r--r-- 1 root root 24 1114 09:06 bb.txt
    23. -rw-r--r-- 1 root root 3 1114 09:26 cc.txt
    24. lrwxrwxrwx 1 root root 24 1114 09:05 test -> /root/192.168.10.245.txt
    25. [root@kibana 245]#

    3.5.本地目录传输(删除destination path中source path没有的文件)

    命令:

    rsync --exclude=ztj --delete -avz /root/245/ /opt/245

    1. [root@kibana 245]# pwd
    2. /opt/245
    3. [root@kibana 245]# touch test.txt
    4. [root@kibana 245]# ls
    5. 245.txt aa.txt bb.txt cc.txt test test.txt
    6. [root@kibana 245]# rsync --exclude=ztj --delete -avz /root/245/ /opt/245
    7. sending incremental file list
    8. deleting test.txt
    9. sent 179 bytes received 24 bytes 406.00 bytes/sec
    10. total size is 119 speedup is 0.59
    11. [root@kibana 245]# ll
    12. 总用量 16
    13. -rw-r--r-- 1 root root 52 1114 09:04 245.txt
    14. -rw-r--r-- 1 root root 16 1114 09:05 aa.txt
    15. -rw-r--r-- 1 root root 24 1114 09:06 bb.txt
    16. -rw-r--r-- 1 root root 3 1114 09:26 cc.txt
    17. lrwxrwxrwx 1 root root 24 1114 09:05 test -> /root/192.168.10.245.txt
    18. [root@kibana 245]#

    3.6.远程主机同步

    命令:

    rsync -avz /root/245/ root@192.168.10.247:/opt/245/
     

    1. [root@kibana 245]# rsync -avz /root/245/ root@192.168.10.247:/opt/245/
    2. The authenticity of host '192.168.10.247 (192.168.10.247)' can't be established.
    3. ECDSA key fingerprint is SHA256:on9qXOFnb3h2UVou3YekMaIqC8l4pD0IvCMimfQwFsA.
    4. ECDSA key fingerprint is MD5:52:fe:8d:28:ea:f0:33:45:b1:1c:e1:29:d0:b4:e3:2a.
    5. Are you sure you want to continue connecting (yes/no)? yes
    6. Warning: Permanently added '192.168.10.247' (ECDSA) to the list of known hosts.
    7. root@192.168.10.247's password:
    8. sending incremental file list
    9. created directory /opt/245
    10. ./
    11. 245.txt
    12. aa.txt
    13. bb.txt
    14. cc.txt
    15. test -> /root/192.168.10.245.txt
    16. ztj/
    17. ztj/ztj.txt
    18. sent 480 bytes received 156 bytes 181.71 bytes/sec
    19. total size is 123 speedup is 0.19
    20. [root@kibana 245]#
    1. [root@rhel77 ~]# cd /opt/245/
    2. [root@rhel77 245]# ls -l
    3. total 16
    4. -rw-r--r-- 1 root root 52 Nov 14 09:04 245.txt
    5. -rw-r--r-- 1 root root 16 Nov 14 09:05 aa.txt
    6. -rw-r--r-- 1 root root 24 Nov 14 09:06 bb.txt
    7. -rw-r--r-- 1 root root 3 Nov 14 09:26 cc.txt
    8. lrwxrwxrwx 1 root root 24 Nov 14 09:05 test -> /root/192.168.10.245.txt #远程主机拷贝,软链接文件不会同步拷贝过来
    9. drwxr-xr-x 2 root root 21 Nov 14 09:28 ztj
    10. [root@rhel77 245]#

    3.7.指定使用ssh方式同步数据

    命令:

    rsync -avz /root/245/ -e "ssh -p 22" root@192.168.10.247:/opt/245/

    1. [root@kibana 245]# rsync -avz /root/245/ -e "ssh -p 22" root@192.168.10.247:/opt/245/
    2. root@192.168.10.247's password:
    3. sending incremental file list
    4. created directory /opt/245
    5. ./
    6. 245.txt
    7. aa.txt
    8. bb.txt
    9. cc.txt
    10. test -> /root/192.168.10.245.txt
    11. ztj/
    12. ztj/ztj.txt
    13. sent 480 bytes received 156 bytes 424.00 bytes/sec
    14. total size is 123 speedup is 0.19
    15. [root@kibana 245]#
    1. [root@rhel77 ~]# cd /opt/245/
    2. [root@rhel77 245]# ls -l
    3. total 16
    4. -rw-r--r-- 1 root root 52 Nov 14 09:04 245.txt
    5. -rw-r--r-- 1 root root 16 Nov 14 09:05 aa.txt
    6. -rw-r--r-- 1 root root 24 Nov 14 09:06 bb.txt
    7. -rw-r--r-- 1 root root 3 Nov 14 09:26 cc.txt
    8. lrwxrwxrwx 1 root root 24 Nov 14 09:05 test -> /root/192.168.10.245.txt #远程主机拷贝,软链接文件不会同步拷贝过来
    9. drwxr-xr-x 2 root root 21 Nov 14 09:28 ztj
    10. [root@rhel77 245]#

    3.8.rsync帮助命令

    命令:

    rsync --help

    1. [root@kibana 245]# rsync --help
    2. rsync version 3.1.2 protocol version 31
    3. Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others.
    4. Web site: http://rsync.samba.org/
    5. Capabilities:
    6. 64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
    7. socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
    8. append, ACLs, xattrs, iconv, symtimes, prealloc
    9. rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you
    10. are welcome to redistribute it under certain conditions. See the GNU
    11. General Public Licence for details.
    12. rsync is a file transfer program capable of efficient remote update
    13. via a fast differencing algorithm.
    14. Usage: rsync [OPTION]... SRC [SRC]... DEST
    15. or rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
    16. or rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
    17. or rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
    18. or rsync [OPTION]... [USER@]HOST:SRC [DEST]
    19. or rsync [OPTION]... [USER@]HOST::SRC [DEST]
    20. or rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
    21. The ':' usages connect via remote shell, while '::' & 'rsync://' usages connect
    22. to an rsync daemon, and require SRC or DEST to start with a module name.
    23. Options
    24. -v, --verbose increase verbosity
    25. --info=FLAGS fine-grained informational verbosity
    26. --debug=FLAGS fine-grained debug verbosity
    27. --msgs2stderr special output handling for debugging
    28. -q, --quiet suppress non-error messages
    29. --no-motd suppress daemon-mode MOTD (see manpage caveat)
    30. -c, --checksum skip based on checksum, not mod-time & size
    31. -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
    32. --no-OPTION turn off an implied OPTION (e.g. --no-D)
    33. -r, --recursive recurse into directories
    34. -R, --relative use relative path names
    35. --no-implied-dirs don't send implied dirs with --relative
    36. -b, --backup make backups (see --suffix & --backup-dir)
    37. --backup-dir=DIR make backups into hierarchy based in DIR
    38. --suffix=SUFFIX set backup suffix (default ~ w/o --backup-dir)
    39. -u, --update skip files that are newer on the receiver
    40. --inplace update destination files in-place (SEE MAN PAGE)
    41. --append append data onto shorter files
    42. --append-verify like --append, but with old data in file checksum
    43. -d, --dirs transfer directories without recursing
    44. -l, --links copy symlinks as symlinks
    45. -L, --copy-links transform symlink into referent file/dir
    46. --copy-unsafe-links only "unsafe" symlinks are transformed
    47. --safe-links ignore symlinks that point outside the source tree
    48. --munge-links munge symlinks to make them safer (but unusable)
    49. -k, --copy-dirlinks transform symlink to a dir into referent dir
    50. -K, --keep-dirlinks treat symlinked dir on receiver as dir
    51. -H, --hard-links preserve hard links
    52. -p, --perms preserve permissions
    53. -E, --executability preserve the file's executability
    54. --chmod=CHMOD affect file and/or directory permissions
    55. -A, --acls preserve ACLs (implies --perms)
    56. -X, --xattrs preserve extended attributes
    57. -o, --owner preserve owner (super-user only)
    58. -g, --group preserve group
    59. --devices preserve device files (super-user only)
    60. --copy-devices copy device contents as regular file
    61. --specials preserve special files
    62. -D same as --devices --specials
    63. -t, --times preserve modification times
    64. -O, --omit-dir-times omit directories from --times
    65. -J, --omit-link-times omit symlinks from --times
    66. --super receiver attempts super-user activities
    67. --fake-super store/recover privileged attrs using xattrs
    68. -S, --sparse handle sparse files efficiently
    69. --preallocate allocate dest files before writing them
    70. -n, --dry-run perform a trial run with no changes made
    71. -W, --whole-file copy files whole (without delta-xfer algorithm)
    72. -x, --one-file-system don't cross filesystem boundaries
    73. -B, --block-size=SIZE force a fixed checksum block-size
    74. -e, --rsh=COMMAND specify the remote shell to use
    75. --rsync-path=PROGRAM specify the rsync to run on the remote machine
    76. --existing skip creating new files on receiver
    77. --ignore-existing skip updating files that already exist on receiver
    78. --remove-source-files sender removes synchronized files (non-dirs)
    79. --del an alias for --delete-during
    80. --delete delete extraneous files from destination dirs
    81. --delete-before receiver deletes before transfer, not during
    82. --delete-during receiver deletes during the transfer
    83. --delete-delay find deletions during, delete after
    84. --delete-after receiver deletes after transfer, not during
    85. --delete-excluded also delete excluded files from destination dirs
    86. --ignore-missing-args ignore missing source args without error
    87. --delete-missing-args delete missing source args from destination
    88. --ignore-errors delete even if there are I/O errors
    89. --force force deletion of directories even if not empty
    90. --max-delete=NUM don't delete more than NUM files
    91. --max-size=SIZE don't transfer any file larger than SIZE
    92. --min-size=SIZE don't transfer any file smaller than SIZE
    93. --partial keep partially transferred files
    94. --partial-dir=DIR put a partially transferred file into DIR
    95. --delay-updates put all updated files into place at transfer's end
    96. -m, --prune-empty-dirs prune empty directory chains from the file-list
    97. --numeric-ids don't map uid/gid values by user/group name
    98. --usermap=STRING custom username mapping
    99. --groupmap=STRING custom groupname mapping
    100. --chown=USER:GROUP simple username/groupname mapping
    101. --timeout=SECONDS set I/O timeout in seconds
    102. --contimeout=SECONDS set daemon connection timeout in seconds
    103. -I, --ignore-times don't skip files that match in size and mod-time
    104. -M, --remote-option=OPTION send OPTION to the remote side only
    105. --size-only skip files that match in size
    106. --modify-window=NUM compare mod-times with reduced accuracy
    107. -T, --temp-dir=DIR create temporary files in directory DIR
    108. -y, --fuzzy find similar file for basis if no dest file
    109. --compare-dest=DIR also compare destination files relative to DIR
    110. --copy-dest=DIR ... and include copies of unchanged files
    111. --link-dest=DIR hardlink to files in DIR when unchanged
    112. -z, --compress compress file data during the transfer
    113. --compress-level=NUM explicitly set compression level
    114. --skip-compress=LIST skip compressing files with a suffix in LIST
    115. -C, --cvs-exclude auto-ignore files the same way CVS does
    116. -f, --filter=RULE add a file-filtering RULE
    117. -F same as --filter='dir-merge /.rsync-filter'
    118. repeated: --filter='- .rsync-filter'
    119. --exclude=PATTERN exclude files matching PATTERN
    120. --exclude-from=FILE read exclude patterns from FILE
    121. --include=PATTERN don't exclude files matching PATTERN
    122. --include-from=FILE read include patterns from FILE
    123. --files-from=FILE read list of source-file names from FILE
    124. -0, --from0 all *-from/filter files are delimited by 0s
    125. -s, --protect-args no space-splitting; only wildcard special-chars
    126. --address=ADDRESS bind address for outgoing socket to daemon
    127. --port=PORT specify double-colon alternate port number
    128. --sockopts=OPTIONS specify custom TCP options
    129. --blocking-io use blocking I/O for the remote shell
    130. --stats give some file-transfer stats
    131. -8, --8-bit-output leave high-bit chars unescaped in output
    132. -h, --human-readable output numbers in a human-readable format
    133. --progress show progress during transfer
    134. -P same as --partial --progress
    135. -i, --itemize-changes output a change-summary for all updates
    136. --out-format=FORMAT output updates using the specified FORMAT
    137. --log-file=FILE log what we're doing to the specified FILE
    138. --log-file-format=FMT log updates using the specified FMT
    139. --password-file=FILE read daemon-access password from FILE
    140. --list-only list the files instead of copying them
    141. --bwlimit=RATE limit socket I/O bandwidth
    142. --outbuf=N|L|B set output buffering to None, Line, or Block
    143. --write-batch=FILE write a batched update to FILE
    144. --only-write-batch=FILE like --write-batch but w/o updating destination
    145. --read-batch=FILE read a batched update from FILE
    146. --protocol=NUM force an older protocol version to be used
    147. --iconv=CONVERT_SPEC request charset conversion of filenames
    148. --checksum-seed=NUM set block/file checksum seed (advanced)
    149. -4, --ipv4 prefer IPv4
    150. -6, --ipv6 prefer IPv6
    151. --version print version number
    152. (-h) --help show this help (-h is --help only if used alone)
    153. Use "rsync --daemon --help" to see the daemon-mode command-line options.
    154. Please see the rsync(1) and rsyncd.conf(5) man pages for full documentation.
    155. See http://rsync.samba.org/ for updates, bug reports, and answers
    156. [root@kibana 245]#

  • 相关阅读:
    Apache POI报表统计
    1. Tomcat整体架构及其设计精髓分析
    【数据结构与算法】顺序表&手撕vector
    flutter: 获取用户输入
    HTML5期末考核大作业 基于HTML+CSS+JavaScript沪上美食(9页)
    VTN系列多通道振弦、温度、模拟传感信号系列数据采集仪
    【亚马逊云+阿里万网】| 实现网站证书配置和域名解析
    EFCore之执行原生SQL语句
    前端面试题
    杰理语音芯片ic玩具芯片ic的介绍_AD14NAD15N全系列开发
  • 原文地址:https://blog.csdn.net/z19861216/article/details/134390975