使用rsync时排除所有包含特定文件名的目录

7
我希望 rsync 排除所有包含特定名称文件(例如“.rsync-exclude”)的目录,而与“.rsync-exclude”文件的内容无关。
如果文件“.rsync-exclude”只包含“*”,我可以使用命令“rsync -r SRC DEST --filter ='dir-merge, - .rsync-exclude'”进行排除。
然而,该目录应独立于“.rsync-exclude”文件的内容而被排除(至少可以将“.rsync-exclude”文件留空)。有什么办法吗?
4个回答

5

rsync不支持此功能(至少手册中没有提到任何内容),但您可以分两步完成:

  1. run find to find the .rsync-exclude files
  2. pipe this list to --exclude-from (or use a temporary file)

       --exclude-from=FILE
          This option is related to the --exclude option, but it specifies a FILE that contains exclude  patterns
          (one per line).  Blank lines in the file and lines starting with ';' or '#' are ignored.  If FILE is -,
          the list will be read from standard input.
    

如果您不介意将一些内容放入文件中,则可以使用以下方法:

       -F     The -F option is a shorthand for adding two --filter rules to your command.  The first time it is  used
          is a shorthand for this rule:

             --filter='dir-merge /.rsync-filter'

          This  tells  rsync  to  look for per-directory .rsync-filter files that have been sprinkled through the
          hierarchy and use their rules to filter the files in the transfer.  If -F is repeated, it is  a  short-
          hand for this rule:

             --filter='exclude .rsync-filter'

          This filters out the .rsync-filter files themselves from the transfer.

          See the FILTER RULES section for detailed information on how these options work.

4

虽然这是一个老问题,但我也有同样的疑问。

你可以添加以下过滤器:

--filter="dir-merge,n- .rsync-exclude"

现在您可以在任何文件夹中放置一个名为.rsync-exclude的文件,逐行写下您想要排除的文件和文件夹名称,例如:
#.rsync-exclude file

folderYouWantToExclude
allFilesThatStartWithXY*
someSpecialImage.png

您可以在其中使用模式。

但是您不能做的是:

#.rsync-exclude file

folder/someFileYouWantToExlude

希望能有所帮助!干杯!

--filter="dir-merge,n- .rsync-exclude" 中,应该是 n- 还是 -nn 是什么意思? - Gifford N.
@GiffordN。说实话,这太久远了,而且我现在也找不到答案,抱歉 :/ 但我猜它定义了rsync应该在每个文件夹中使用哪个文件进行过滤。 - Lorenz Haase
1
我找到了,就像rsync手册中的FILTER RULES部分所描述的那样,应该是n-。此外,_n指定规则不会被子目录继承。_而-表示从文件中读取的规则默认具有排除修改器设置,例如dir-merge,n- .non-inherited-per-dir-excludes - Gifford N.

0
如果您不介意使用tar而不是纯粹的rsync,那么您可以使用tar的--exclude-tag=FILE选项。显然,这样做无法获得rsync的增量备份优势,所以可能不完全符合您的要求。
tar cv --exclude-tag=.rsync-exclude ...

-3
rsync -avz --exclude 'dir' /source /destination 

1
请在答案中添加简短描述以帮助用户理解。 - Terru_theTerror

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接