Linux命令(122)之rsync

简介: Linux命令(122)之rsync

nux命令之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

[root@kibana ~]# rsync -avz /root/245/ /opt/245
sending incremental file list
created directory /opt/245
./
245.txt
aa.txt
bb.txt
test -> /root/192.168.10.245.txt

sent 308 bytes received 110 bytes 836.00 bytes/sec
total size is 116 speedup is 0.28
[root@kibana 245]# pwd
/opt/245
[root@kibana 245]# ls -l
总用量 12
-rw-r--r-- 1 root root 52 11月 14 09:04 245.txt
-rw-r--r-- 1 root root 16 11月 14 09:05 aa.txt
-rw-r--r-- 1 root root 24 11月 14 09:06 bb.txt
lrwxrwxrwx 1 root root 24 11月 14 09:05 test -> /root/192.168.10.245.txt
[root@kibana 245]#

3.2.增量同步
命令:

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

[root@kibana 245]# pwd
/root/245
[root@kibana 245]# ls
245.txt aa.txt bb.txt test
[root@kibana 245]# touch cc.txt
[root@kibana 245]# echo cc >>cc.txt
[root@kibana 245]# ll
总用量 16
-rw-r--r-- 1 root root 52 11月 14 09:04 245.txt
-rw-r--r-- 1 root root 16 11月 14 09:05 aa.txt
-rw-r--r-- 1 root root 24 11月 14 09:06 bb.txt
-rw-r--r-- 1 root root 3 11月 14 09:26 cc.txt
lrwxrwxrwx 1 root root 24 11月 14 09:05 test -> /root/192.168.10.245.txt
[root@kibana 245]# rsync -avz /root/245/ /opt/245 #首次,会在本地和远程之间拷贝所有内容;后续,只会传输发生改变的块或者字节。
sending incremental file list
./
cc.txt

sent 229 bytes received 38 bytes 534.00 bytes/sec
total size is 119 speedup is 0.45
[root@kibana 245]#
[root@kibana 245]# pwd
/opt/245
[root@kibana 245]# ls -l
总用量 16
-rw-r--r-- 1 root root 52 11月 14 09:04 245.txt
-rw-r--r-- 1 root root 16 11月 14 09:05 aa.txt
-rw-r--r-- 1 root root 24 11月 14 09:06 bb.txt
-rw-r--r-- 1 root root 3 11月 14 09:26 cc.txt
lrwxrwxrwx 1 root root 24 11月 14 09:05 test -> /root/192.168.10.245.txt
[root@kibana 245]# cat cc.txt
cc
[root@kibana 245]#

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

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

[root@kibana 245]# rsync -rvz /root/245/ /opt/245
sending incremental file list
created directory /opt/245
skipping non-regular file "test"
./
245.txt
aa.txt
bb.txt
cc.txt
ztj/
ztj/ztj.txt

sent 454 bytes received 190 bytes 1,288.00 bytes/sec
total size is 123 speedup is 0.19
[root@kibana 245]#
[root@kibana opt]# pwd
/opt
[root@kibana opt]# ls
245
[root@kibana opt]# cd 245/
[root@kibana 245]# ls
245.txt aa.txt bb.txt cc.txt ztj
[root@kibana 245]# ll
总用量 16
-rw-r--r-- 1 root root 52 11月 14 09:32 245.txt
-rw-r--r-- 1 root root 16 11月 14 09:32 aa.txt
-rw-r--r-- 1 root root 24 11月 14 09:32 bb.txt
-rw-r--r-- 1 root root 3 11月 14 09:32 cc.txt
drwxr-xr-x 2 root root 21 11月 14 09:32 ztj
[root@kibana 245]#

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

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

[root@kibana 245]# ls
245.txt aa.txt bb.txt cc.txt test zt
[root@kibana 245]# rsync --exclude=ztj -avz /root/245/ /opt/245
sending incremental file list
./
245.txt
aa.txt
bb.txt
cc.txt
test -> /root/192.168.10.245.txt

sent 372 bytes received 98 bytes 940.00 bytes/sec
total size is 119 speedup is 0.25
[root@kibana 245]#
[root@kibana 245]# pwd
/opt/245
[root@kibana 245]# ls
245.txt aa.txt bb.txt cc.txt test
[root@kibana 245]# ll
总用量 16
-rw-r--r-- 1 root root 52 11月 14 09:04 245.txt
-rw-r--r-- 1 root root 16 11月 14 09:05 aa.txt
-rw-r--r-- 1 root root 24 11月 14 09:06 bb.txt
-rw-r--r-- 1 root root 3 11月 14 09:26 cc.txt
lrwxrwxrwx 1 root root 24 11月 14 09:05 test -> /root/192.168.10.245.txt
[root@kibana 245]#

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

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

[root@kibana 245]# pwd
/opt/245
[root@kibana 245]# touch test.txt
[root@kibana 245]# ls
245.txt aa.txt bb.txt cc.txt test test.txt
[root@kibana 245]# rsync --exclude=ztj --delete -avz /root/245/ /opt/245
sending incremental file list
deleting test.txt

sent 179 bytes received 24 bytes 406.00 bytes/sec
total size is 119 speedup is 0.59
[root@kibana 245]# ll
总用量 16
-rw-r--r-- 1 root root 52 11月 14 09:04 245.txt
-rw-r--r-- 1 root root 16 11月 14 09:05 aa.txt
-rw-r--r-- 1 root root 24 11月 14 09:06 bb.txt
-rw-r--r-- 1 root root 3 11月 14 09:26 cc.txt
lrwxrwxrwx 1 root root 24 11月 14 09:05 test -> /root/192.168.10.245.txt
[root@kibana 245]#

3.6.远程主机同步
命令:

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

[root@kibana 245]# rsync -avz /root/245/ root@192.168.10.247:/opt/245/
The authenticity of host '192.168.10.247 (192.168.10.247)' can't be established.
ECDSA key fingerprint is SHA256:on9qXOFnb3h2UVou3YekMaIqC8l4pD0IvCMimfQwFsA.
ECDSA key fingerprint is MD5:52:fe:8d:28:ea:f0:33:45:b1:1c:e1:29:d0:b4:e3:2a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.10.247' (ECDSA) to the list of known hosts.
root@192.168.10.247's password:
sending incremental file list
created directory /opt/245
./
245.txt
aa.txt
bb.txt
cc.txt
test -> /root/192.168.10.245.txt
ztj/
ztj/ztj.txt

sent 480 bytes received 156 bytes 181.71 bytes/sec
total size is 123 speedup is 0.19
[root@kibana 245]#

[root@rhel77 ~]# cd /opt/245/
[root@rhel77 245]# ls -l
total 16
-rw-r--r-- 1 root root 52 Nov 14 09:04 245.txt
-rw-r--r-- 1 root root 16 Nov 14 09:05 aa.txt
-rw-r--r-- 1 root root 24 Nov 14 09:06 bb.txt
-rw-r--r-- 1 root root 3 Nov 14 09:26 cc.txt
lrwxrwxrwx 1 root root 24 Nov 14 09:05 test -> /root/192.168.10.245.txt #远程主机拷贝,软链接文件不会同步拷贝过来
drwxr-xr-x 2 root root 21 Nov 14 09:28 ztj
[root@rhel77 245]#
3.7.指定使用ssh方式同步数据
命令:

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

[root@kibana 245]# rsync -avz /root/245/ -e "ssh -p 22" root@192.168.10.247:/opt/245/
root@192.168.10.247's password:
sending incremental file list
created directory /opt/245
./
245.txt
aa.txt
bb.txt
cc.txt
test -> /root/192.168.10.245.txt
ztj/
ztj/ztj.txt
sent 480 bytes received 156 bytes 424.00 bytes/sec
total size is 123 speedup is 0.19
[root@kibana 245]#

[root@rhel77 ~]# cd /opt/245/
[root@rhel77 245]# ls -l
total 16
-rw-r--r-- 1 root root 52 Nov 14 09:04 245.txt
-rw-r--r-- 1 root root 16 Nov 14 09:05 aa.txt
-rw-r--r-- 1 root root 24 Nov 14 09:06 bb.txt
-rw-r--r-- 1 root root 3 Nov 14 09:26 cc.txt
lrwxrwxrwx 1 root root 24 Nov 14 09:05 test -> /root/192.168.10.245.txt #远程主机拷贝,软链接文件不会同步拷贝过来
drwxr-xr-x 2 root root 21 Nov 14 09:28 ztj
[root@rhel77 245]#
3.8.rsync帮助命令
命令:

rsync --help

[root@kibana 245]# rsync --help
rsync version 3.1.2 protocol version 31
Copyright (C) 1996-2015 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, prealloc

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
--info=FLAGS fine-grained informational verbosity
--debug=FLAGS fine-grained debug verbosity
--msgs2stderr special output handling for debugging
-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)
--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
--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
-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
--munge-links munge symlinks to make them safer (but unusable)
-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
-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)
-g, --group preserve 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
-t, --times preserve modification times
-O, --omit-dir-times omit directories from --times
-J, --omit-link-times omit symlinks from --times
--super receiver attempts super-user activities
--fake-super store/recover privileged attrs using xattrs
-S, --sparse handle sparse files efficiently
--preallocate allocate dest files before writing them
-n, --dry-run perform a trial run with no changes made
-W, --whole-file copy files whole (without delta-xfer algorithm)
-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
--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
--remove-source-files sender removes synchronized files (non-dirs)
--del an alias for --delete-during
--delete delete extraneous files from destination dirs
--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-missing-args ignore missing source args without error
--delete-missing-args delete missing source args from destination
--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
--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 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
--usermap=STRING custom username mapping
--groupmap=STRING custom groupname mapping
--chown=USER:GROUP simple username/groupname mapping
--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
-M, --remote-option=OPTION send OPTION to the remote side only
--size-only skip files that match in size
--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
--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
--list-only list the files instead of copying them
--bwlimit=RATE limit socket I/O bandwidth
--outbuf=N|L|B set output buffering to None, Line, or Block
--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

————————————————
版权声明:本文为CSDN博主「小黑要上天」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/z19861216/article/details/134390975

目录
相关文章
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
1246 1
二、Linux文本处理与文件操作核心命令
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
979 137
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
1964 58
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
1899 2
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
1172 16
|
缓存 监控 Linux
Linux内存问题排查命令详解
Linux服务器卡顿?可能是内存问题。掌握free、vmstat、sar三大命令,快速排查内存使用情况。free查看实时内存,vmstat诊断系统整体性能瓶颈,sar实现长期监控,三者结合,高效定位并解决内存问题。
1286 0
Linux内存问题排查命令详解
|
Linux 网络安全 开发工具
技术栈:这50条最常用的 Linux 命令你一定要会!
建议多在终端中实践,遇到不懂的命令就用 man 或 --help 了解详情!
2437 0
|
安全 Linux Shell
Linux系统中sudo命令的高效运用技巧。
用户可以通过sudo -l来列出自己目前可执行的命令列表,这有助于用户了解自己的权限范围。
560 0
|
监控 Linux Shell
linux命令
常用 Linux 命令汇总
|
Linux C++
每天一个linux命令(8):cp 命令
cp 命令是 Linux 中用于复制文件或目录的命令。它的名字来源于英文单词 copy。这个命令非常常用,特别是在需要备份文件或创建文件副本时。
937 0