如何使用Rsync仅复制特定的子目录(在几个目录中具有相同的名称) [英] How to use Rsync to copy only specific subdirectories (same names in several directories)

查看:240
本文介绍了如何使用Rsync仅复制特定的子目录(在几个目录中具有相同的名称)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器1上有这样的目录结构:

I have such directories structure on server 1:


  • data

    • company1

      • unique_folder1

      • other_folder

      • ...


      • unique_folder1

      • ...

      我想在服务器2上复制此文件夹结构,但仅复制unique_folder1的目录/子目录。即结果必须是:

      And I want duplicate this folder structure on server 2, but copy only directories/subdirectories of unique_folder1. I.e. as result must be:


      • 数据

        • company1

          • unique_folder1


          • unique_folder1

          我知道 rsync 对此非常有用。
          我尝试了包含/排除选项,但没有成功。

          I know that rsync is very good for this. I've tried 'include/exclude' options without success.

          例如我试过了:

          rsync -avzn --list-only --include '*/unique_folder1/**' --exclude '*' -e ssh user@server.com:/path/to/old/data/ /path/to/new/data/
          

          但是,结果,我看不到任何文件/目录:

          But, as result, I don't see any files/directories:

          receiving file list ... done
          sent 43 bytes  received 21 bytes  42.67 bytes/sec
          total size is 0  speedup is 0.00 (DRY RUN)
          

          怎么了?有想法吗?

          其他信息:
          我可以对两个服务器进行sudo访问。我的一个想法-使用 find 命令和 cpio 命令一起复制到新目录,其中包含我需要的内容使用 Rsync 的代码。但这很慢,文件很多,等等。

          Additional information: I have sudo access to both servers. One idea I have - is to use find command and cpio together to copy to new directory with content I need and after that use Rsync. But this is very slow, there are a lot of files, etc.

          推荐答案

          我已经找到了原因。对我而言-尚不清楚 Rsync 是否以这种方式工作。

          正确的命令(对于 company1 目录)必须为:

          I've found the reason. As for me - it wasn't clear that Rsync works in this way.
          So correct command (for company1 directory only) must be:

          rsync -avzn --list-only --include 'company1/' --include 'company1/unique_folder1/***' --exclude '*' -e ssh user@server.com:/path/to/old/data/ /path/to/new/data
          

          即我们需要包括每个父 company 目录。当然,我们不能在命令行中手动编写所有这些 company 目录,因此我们将列表保存到文件中并使用它。

          I.e. we need include each parent company directory. And of course we cannot write manually all these company directories in the command line, so we save the list into the file and use it.

          1。在服务器1上生成包含文件,因此其内容将是(我用过 ls awk ):

          1.Generate include file on server 1, so its content will be (I've used ls and awk):

          + company1/  
          + company1/unique_folder1/***  
          ...  
          + companyN/  
          + companyN/unique_folder1/***  
          

          2。将include.txt复制到服务器2并使用以下命令:

          2.Copy include.txt to server 2 and use such command:

          rsync -avzn                                        \
                --list-only                                  \
                --include-from '/path/to/new/include.txt'    \
                --exclude '*'                                \
                -e ssh user@server.com:/path/to/old/data/    \
                /path/to/new/data
          

          这篇关于如何使用Rsync仅复制特定的子目录(在几个目录中具有相同的名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆