如何使用Rsync仅复制特定的子目录(在几个目录中具有相同的名称) [英] How to use Rsync to copy only specific subdirectories (same names in several directories)
问题描述
我在服务器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 andcpio
together to copy to new directory with content I need and after that useRsync
. 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 (forcompany1
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 thesecompany
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
andawk
):+ 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屋!
- company1
- company1