如何从 rsync 中排除子目录?

如何从 rsync 中排除子目录?

我用来rsync递归同步远程文件夹树,如下所示:

/folderA/a1/cache
/folderA/a1/cache/A1
/folderA/a1/cache/A2
/folderA/a1/somefolder
/folderA/a1/someotherfolder
/folderA/a2/somefolder/cache
/folderB/cache/
/folderB/b1/somefolder/cache
/folderB/b1/somefolder/yetanotherfolder/cache
/folderB/b1/somefolder/yetanotherfolder/cache/B1
/folderB/b1/somefolder/yetanotherfolder/cache/B2

我不知道文件夹树会是什么样子,它会随着时间的推移而改变。所以我想要做的就是递归地执行rsync上面的操作,但是排除文件夹“cache”及其包含的任何子文件夹:

/folderA/a1
/folderA/a1/somefolder
/folderA/a1/someotherfolder
/folderA/a2/somefolder
/folderB/
/folderB/b1/somefolder
/folderB/b1/somefolder/yetanotherfolder/

有什么建议么?

答案1

你想要--exclude旗帜。例如,本地 rsync:

rsync -a --exclude cache/ src_folder/ target_folder/

确实就是这么简单——排除规则将匹配cache树中任何位置命名的目录。最后的强制/它匹配目录,因此它不会排除名为cache.

您也可以匹配路径。给定以下树:

a1/cache/
b1/cache/
sub/a1/cache/

--exclude a1/cache/将匹配a1/cachesub/a1/cache但不匹配b1/cache。并且--exclude /a1/cache/只会匹配a1/cache

rsync 过滤还可以做很多事情。查找--exclude--include--filter,尤其是 rsync 手册页上的“FILTER RULES”和“PATTERN MATCHING RULES”部分:

https://www.samba.org/ftp/rsync/rsync.html#FILTER_RULES

相关内容