跳到主要内容

rsync

rsync

目录

[toc]

什么是rsync

rsync是一个功能强大的文件同步工具,可以用于复制、备份和同步文件。

对于运维人员,数据备份是日常重要而又必须做的工作,选择一种好的备份软件效率会大大提升。rsync 就是一款功能强大,高效,安全,速度快的文件增量传输工具。

  1. rsync ---- remote synchronize ,是一款实现远程同步功能的软件;
  2. rsync使用“Rsync算法”来同步文件,该算法只传送两个文件的不同部分,因此速度相当快;
  3. 同步文件的同时,可以保持原来文件的权限、时间 和目录结构;
  4. 对于多个文件来说,内部流水线减少文件等待的延时;
  5. rsync默认监听TCP 873端口,通过远程shell如rsh和ssh复制文件。同时要求必须在远程和本地系统上都安装sync.

参数选项

rsync 可用选项?

rsync是一个功能强大的文件同步工具,它提供了多种选项来满足不同的需求。以下是一些常用的rsync选项:

  • -v, --verbose显示详细输出,让您了解文件同步的进度和操作。

  • -a, --archive归档模式,用于保持文件的属性、权限、时间戳等。

  • -r, --recursive递归复制目录及其内容。

  • -z, --compress:在传输过程中压缩数据,减少网络带宽的使用。

  • -P, --progress显示文件传输的进度条和实时速度。

  • -u, --update只复制源中更新或新增的文件到目标目录。 (这个就是所谓的增量更新吗?)

  • --delete删除目标目录中不在源中存在的文件和目录。

  • --exclude:排除指定的文件、目录或模式,不进行同步。

  • --include:包含指定的文件、目录或模式,即使使用了--exclude

  • --exclude-from:从指定的文件中读取排除模式。

  • --include-from:从指定的文件中读取包含模式。

  • -n, --dry-run模拟运行,显示将要执行的操作但不进行实际的文件同步。

  • -h, --human-readable output numbers in a human-readable format #就是你啦

    ​ --progress show progress during transfer

[root@mkdocs-server rsync]# 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
[root@mkdocs-server rsync]#

案例:rsync如何同步2个目录的内容

要使用rsync同步两个目录的内容,你可以使用以下命令格式:

rsync -av source_dir/ destination_dir/

其中,source_dir是源目录的路径,destination_dir是目标目录的路径。这个命令会将源目录下的所有文件和子目录同步到目标目录。

选项说明:

  • -a:表示以归档模式进行同步,保持文件的属性、权限以及其他元数据。
  • -v:表示显示同步过程中的详细输出。

请注意,在目标目录中不存在的文件和目录将被拷贝到目标目录中。如果目标目录中已经存在相同的文件/目录,rsync将根据更新的时间戳和文件内容来决定是否需要覆盖目标目录中的文件。

以下是一个示例:

rsync -av /path/to/source_dir/ /path/to/destination_dir/

请将/path/to/source_dir/替换为实际的源目录路径,将/path/to/destination_dir/替换为实际的目标目录路径。

希望这可以帮助到你!如果有任何其他问题,请随时提问。

案例:linux里rsync里允许所有主机该如何配置==(已解决)==

要允许所有主机在Linux里使用Rsync,你可以按照以下步骤进行配置:

  1. 打开Rsync配置文件,一般位于/etc/rsyncd.conf
  2. 在配置文件中添加以下内容:
uid = <username>
gid = <groupname>
use chroot = yes
max connections = 4
syslog facility = local5
pid file = /var/run/rsyncd.pid

[share]
path = /path/to/share
comment = Rsync Share
read only = no
list = yes
hosts allow = * #这里改为 * 即可。
  • <username>是具有访问共享目录权限的用户名。
  • <groupname>是该用户所属的权限组。
  • /path/to/share是要共享的目录路径。
  1. 保存配置文件并关闭。
  2. 重启Rsync服务,使用以下命令:
sudo service rsync restart

现在,所有主机都可以通过Rsync访问共享目录。请注意,开放给所有主机的访问权限可能存在安全风险,因此在生产环境中建议限制访问权限。这只是一个基本的配置示例,可以根据你的需求进行调整。

案例:--exclude参数来实现拷贝时排除特定目录或文件

gpt提问:

但是,你可以使用rsync命令来实现拷贝时排除特定目录或文件。rsync是一个功能强大的文件同步工具,可以用于复制、备份和同步文件。

你可以使用以下命令来实现排除特定目录和文件的拷贝:

rsync -av --exclude='.git' --exclude='node_modules' --exclude='README.md' /d/hexo/ /d/hexo-tmp/

上述命令将会拷贝/d/hexo/目录下除.git目录、node_modules目录和README.md文件之外的所有内容到/d/hexo-tmp/目录下。

希望这次可以帮到你。如果还有其他问题,请随时提问。

案例:rsync服务器端要同步多个客户端,配置该怎么写?-2023.11.19==(已解决)==

gpt

rsync服务器端要同步多个客户端,配置该怎么写?

在配置一个rsync服务器,以便同步多个客户端的情况下,你可以使用rsync的配置文件(rsyncd.conf)来定义共享模块(modules)和访问规则。以下是一个简单的配置示例:

  1. 安装rsync:

    确保你的服务器上已经安装了rsync。如果没有,可以使用包管理器进行安装。例如,在Ubuntu上可以使用以下命令:

    bashsudo apt-get update
    sudo apt-get install rsync
  2. 创建rsyncd.conf文件:

    创建一个rsync的配置文件,比如/etc/rsyncd.conf。

    plaintextsudo nano /etc/rsyncd.conf
  3. 配置rsyncd.conf文件:

    在rsyncd.conf文件中,你可以定义多个模块,每个模块对应一个共享资源。

    plaintext# /etc/rsyncd.conf

    uid = nobody
    gid = nogroup
    use chroot = yes
    max connections = 25
    strict modes = yes
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    log file = /var/log/rsync.log

    [module1]
    path = /path/to/module1
    comment = Module 1
    read only = no
    list = yes
    auth users = user1
    secrets file = /etc/rsyncd.secrets

    [module2]
    path = /path/to/module2
    comment = Module 2
    read only = no
    list = yes
    auth users = user2
    secrets file = /etc/rsyncd.secrets

    这个配置文件中定义了两个模块:module1module2。每个模块有自己的路径、注释、读写权限等设置。

  4. 创建rsyncd.secrets文件:

    创建一个包含用户名和密码的文件,用于认证客户端。

    plaintextsudo nano /etc/rsyncd.secrets

    添加以下内容:

    plaintextuser1:password1
    user2:password2

    记得保护好这个文件的权限,以免被非授权用户访问。

  5. 启动rsync服务:

    启动rsync服务,并确保它在系统启动时自动启动。

    bashsudo systemctl start rsync
    sudo systemctl enable rsync

现在,你的rsync服务器已经配置好,可以在客户端使用rsync命令来同步数据。例如:

bashrsync -avz /local/path/ rsync://user1@your-rsync-server/module1/

这个命令将本地路径/local/path/的内容同步到rsync服务器上的module1模块。确保将your-rsync-server替换为实际的服务器地址。同样,你可以使用不同的用户和模块名来同步其他数据。


自己测试过程:

[root@hexo-blog ~]# cat  /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# 进行通信的端口,如果 firewall 打开的话,需要将对应的端口添加进去
port=8730

# 日志文件
log file=/var/log/rsync.log

# rsync 的进程 id
pid file=/var/run/rsync.pid

# 要同步的模块,这里一般以项目名命名
[cmi]

# 同步的目标文件夹
path=/root/rsync

# rsync daemon 在传输前是否切换到指定的 path 目录下,并将其监禁在内,用于增加传输的安全性
use chroot=no

# 指定最大的连接数
max connections=4

# yes 表示只读本地文件无法同步到服务器
read only=no

# 客户端请求显示模块列表时,该模块是否显示出来
list=true

# 服务运行时的用户
uid=root

# 服务运行时的用户组
gid=root

# 进行验证时的用户名,必须是系统存在的用户
auth users = root

# 连接用户时的密码
secrets file=/etc/rsyncd.passwd

# 允许的 ip
hosts allow=*




[cmi-wiki]

# 同步的目标文件夹
path=/root/rsync-wiki

# rsync daemon 在传输前是否切换到指定的 path 目录下,并将其监禁在内,用于增加传输的安全性
use chroot=no

# 指定最大的连接数
max connections=4

# yes 表示只读本地文件无法同步到服务器
read only=no

# 客户端请求显示模块列表时,该模块是否显示出来
list=true

# 服务运行时的用户
uid=root

# 服务运行时的用户组
gid=root

# 进行验证时的用户名,必须是系统存在的用户
auth users = root

# 连接用户时的密码
secrets file=/etc/rsyncd.passwd

# 允许的 ip
hosts allow=*
[root@hexo-blog ~]#

image-20231119171400158

符合预期。

cwRsync:客户端版本和服务器版本

cwRsync_4.1.0_Installer.exe 和 cwRsyncServer_4.1.0_Installer.exe有什么区别?

cwRsync 是 Windows 上的一种 rsync 实现,它使 Windows 用户能够使用 rsync 工具来同步文件。cwRsync 有两种版本:客户端版本和服务器版本。

  1. cwRsync_4.1.0_Installer.exe: 这通常是指客户端安装程序。它用于在 Windows 系统上安装 rsync 客户端,使你能够从远程 rsync 服务器或另一台运行 rsync 的 Windows 机器上同步文件。客户端版本通常用于将本地文件推送到远程服务器或从远程服务器拉取文件到本地系统。(push/pull)

  2. cwRsyncServer_4.1.0_Installer.exe: 这是服务器端安装程序。它用于在 Windows 系统上安装 rsync 服务器组件。当你想要将你的 Windows 机器设置为可以接受来自 rsync 客户端的同步请求时,你需要安装这个版本。服务器版本允许其他 rsync 客户端连接并与你的服务器同步文件。

简而言之:

  • 客户端版本(cwRsync)用于将文件同步到远程服务器或从远程服务器同步文件。
  • 服务器版本(cwRsyncServer)用于设置你的 Windows 机器作为一个 rsync 服务器,以便其他客户端可以与之同步文件。

选择哪个取决于你计划如何使用 rsync:如果你只需要同步文件,并不打算让其他机器与你的机器同步,那么客户端版本就足够了。如果你希望你的机器作为服务器,允许其他 rsync 客户端与之同步,那么你需要服务器版本。

注意:rsync同步路径里不要有中文,否则同步时会报错!

案例:rsync命令同步2个目录后,为啥无法进入到这个目录呢?-2024.8.20(待解决)

现在有个需求: 我当前的环境是win10,且本地已安装了rsync命令。我的D:\docusaurus目录存放了很多小文件,如何利用rsync命令全量拷贝D:\docusaurus到D:\test-docusaurus目录呢?

gpt回答

如果您已经在Windows 10上安装了rsync命令,并且想使用rsync命令在本地拷贝D:\docusaurus目录到D:\test-docusaurus目录,可以按照以下步骤操作:

  1. 打开命令提示符或PowerShell终端。

  2. 使用以下命令进行拷贝:

    rsync -avz --progress /mnt/d/docusaurus /mnt/d/test-docusaurus

    这里使用/mnt/d表示Windows的D盘(D:\),并将D:\docusaurus目录拷贝到D:\test-docusaurus目录下。如果您的D盘不是D,可以相应地更改/mnt/d的路径。

    参数解释:

    • -a:递归拷贝目录,并保持文件属性。
    • -v:显示详细输出信息。
    • -z:压缩传输数据。
    • --progress:显示拷贝进度。
  3. 等待rsync命令完成拷贝操作,您将在命令行中看到拷贝的进度和详细输出信息。

注意事项:

  • 请确保您已经正确安装并配置了Windows上的rsync命令。
  • 在Windows上,rsync命令通过Cygwin或MinGW等工具提供。请确保将rsync命令所在的路径添加到系统的环境变量中,以便在命令提示符或PowerShell终端中执行rsync命令。
  • 在Windows上使用rsync命令时,需要使用类Unix的路径表示方式(例如/mnt/c/path)来表示Windows的文件路径。
  • 请根据您实际的环境和路径进行调整。

自己测试

##之前的:winodws-->linux
rsync.exe -avPzruh --delete --port 8730 --password-file=/cygdrive/D/docusaurus/resource/shell/password.txt /cygdrive/D/docusaurus/build root@47.100.215.163::cmi-docusaurus/




rsync.exe -avPzruh --delete --port 8730 --password-file=/cygdrive/D/docusaurus/resource/shell/password.txt /cygdrive/D/docusaurus/build root@47.100.215.163::cmi-docusaurus/

- `-a`:递归拷贝目录,并保持文件属性。
- `-v`:显示详细输出信息。
- `-z`:压缩传输数据。
- `--progress`:显示拷贝进度。
`-r, --recursive`<u>递归复制目录及其内容。</u>
`-u, --update`<u>只复制源中更新或新增的文件到目标目录。</u>
`-h, --human-readable` <u>output numbers in a human-readable format #就是你啦</u>
`--delete`<u>删除目标目录中不在源中存在的文件和目录。</u>

  • 报错
rsync -avPzruh --delete  /mnt/d/docusaurus /mnt/d/test-docusaurus

image-20240818203405340

  • 再测试
rsync -avPzruh --delete  /cygdrive/D/docusaurus  /cygdrive/D/test-docusaurus

#是不用提前创建这个 test-docusaurus目录的

image-20240818204540108

故障

测试效果:

奇怪,没啥没权限进入呢??

image-20240818205647838

image-20240818205704224

关于我

我的博客主旨:

  • 排版美观,语言精炼;
  • 文档即手册,步骤明细,拒绝埋坑,提供源码;
  • 本人实战文档都是亲测成功的,各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人帮您解决问题,让我们一起进步!

🍀 微信二维码 x2675263825 (舍得), qq:2675263825。

image-20230107215114763

🍀 微信公众号 《云原生架构师实战》

image-20230107215126971

🍀 个人博客站点

http://onedayxyy.cn/

image-20231021104335916

image-20231021104405837

🍀 csdn

https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421

image-20230107215149885

🍀 知乎

https://www.zhihu.com/people/foryouone

image-20230107215203185

最后

好了,关于本次就到这里了,感谢大家阅读,最后祝大家生活快乐,每天都过的有意义哦,我们下期见!