AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 770728
Accepted
Tom Auger
Tom Auger
Asked: 2016-04-16 11:30:01 +0800 CST2016-04-16 11:30:01 +0800 CST 2016-04-16 11:30:01 +0800 CST

rsync - 排除除少数以外的所有目录

  • 772

似乎有很多关于此的 SF 和 SO 问题,但似乎没有一个符合我的要求。

source_dir/
  some_dir/
  another_dir/
  some_file.php
  some_other_file.txt
  include_this_dir/
  include_that_dir/
  yet_another_dir/

所以我想 rsync 其中两个目录,同时排除其余目录。我们排除除这两个目录之外的所有目录很重要,因为以后可能会有其他文件添加到 source_dir 中,并且需要在没有明确列出的情况下排除。

我试过了:

rsync -av --dry-run --delete --exclude="source_dir/*" (or just "*") --include-from="include.txt" source_dir dest_dir

我的 include.txt 有

  include_this_dir/
  include_that_dir/

我也尝试添加

  source_dir/

没有喜悦。根本没有任何内容。

rsync
  • 3 3 个回答
  • 15230 Views

3 个回答

  • Voted
  1. Best Answer
    Darryl E. Clarke
    2016-07-06T05:11:41+08:002016-07-06T05:11:41+08:00

    一个简单的过滤器应该可以解决问题。以适当的示例构建先前的答案-明确包括父级,以及所有(**)子文件夹和文件。然后排除其他所有内容。这是filter.txt:

    + /include_this_dir/
    + /include_this_dir/**
    + /include_that_dir/
    + /include_that_dir/**
    - /**
    

    使用命令行:

    rsync -av --dry-run --filter="merge filter.txt" source_dir/ dest_dir/
    

    会导致:

    sending incremental file list
    created directory dest_dir
    ./
    include_that_dir/
    include_that_dir/somefile.txt
    include_that_dir/subdir/
    include_this_dir/
    
    sent 202 bytes  received 65 bytes  534.00 bytes/sec
    total size is 0  speedup is 0.00 (DRY RUN)
    
    • 20
  2. M. Glatki
    2016-04-18T06:49:24+08:002016-04-18T06:49:24+08:00

    从rsync手册部分引用FILTER RULES:

    [..] 第一个匹配模式被执行:如果它是一个排除模式,则跳过该文件;如果它是包含模式,则不会跳过该文件名;如果没有找到匹配的模式,则不跳过文件名。

    因此,从文件中读取过滤规则并相应地对它们进行排序可能是您的最佳选择。例子:

    CW+ source_dir/include_this_dir/
    CW+ source_dir/include_that_dir/
    CW- source_dir/
    

    您可以使用参数指定过滤规则文件--filter=。

    • 2
  3. Landis Reed
    2020-03-02T15:09:19+08:002020-03-02T15:09:19+08:00

    创建 --included 目录并复制其内容目录和/或文件的新命令字符串,但只有 2 个 DIRS DEEP。为什么?

    rsync -avid --progress ~/MyDocuments/'Inet Publishing'/ --recursive --include='LandisTwo (2019)' --include='LandisTwo (2020) {backup}' --include='*.*' --exclude='**/' e6420:/home/landis/Media/2TBackup/Backups/MyDocuments/"Inet\ Publishing"/ | tee -a ~/logs/Backup-InetPublishing_2020.txt
    
    • 我发现目录已创建,但没有文件被复制到它们(无递归)。父目录中的文件被复制,但不是子目录 - 正在处理它。

    这适用于我,从工作站到服务器,将我的个人站点备份(rysnc)到另一台服务器,以及在 rysnc'ing parent ../Inet\ Publishing 时包含所有站点的整个目录(-avin 中的 'n'是“试运行”,删除。)。

    --exclude */ 是 'Inet Publishing/' 中的所有其他目录,并且似乎必须在 --include per man rsync process order 之后(如果在 --include 之前,则所有内容都是 --excluded 并且不再处理任何令牌,至少我是这样阅读它的,而且在实践中,当我想出这个时,我是这样理解的。)。

    这实际上是我使用 cron 运行的脚本之一。*显然,目标主机可以是 IP,我在 /etc/hosts 中定义了主机并拥有 ssh 密钥对,所以我使用主机名。

    rsync -avin --progress ~/MyDocuments/'Inet Publishing'/ --include='LandisTwo (2019)' --include='LandisTwo (2020) {backup}' --exclude='*/' e6420:/home /landis/Media/2TBackup/Backups/MyDocuments/ | tee -a ~/logs/Backup-InetPublishing_2020.txt
    • 0

相关问题

  • 强制 rsync 进入非交互模式

  • 比较(diff)完整目录结构的最佳方法?

  • 如何使用 rsync 保持完整路径?

  • Rsyncing到USB驱动器时不时地失败

  • 使用 rsync 维护名称更改的目录的副本

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve