Sch*_*ure 3 linux rsync filter
我想用rsync/home备份我的目录。我已阅读 rsync 的手册页并决定使用过滤规则来完成此任务。
我想要实现的目标: 排除目录中的所有文件和目录Repos,但保留所有pull_all.sh文件和output目录--- 无论它们位于Repos目录中的位置。
到目前为止,我最终得到了以下过滤器列表,但这仅备份pull_all.sh文件而不output备份目录:
# Files prefixed with "+ " are included. Files prefixed with "- " are excluded.\n#\n# The order of included and excluded files matters! For instance, if a folder\n# is excluded first, no subdirectory can be included anymore. Therefore,\n# mention included files first. Then, mention excluded files.\n#\n# See section "FILTER RULES" of rsync manual for more details.\n\n\n# Included Files\n\n# TODO: This rules do not work properly!\n+ output/***\n+ pull_all.sh\n- Repos/**\n\n# Excluded Files\n\n- .android\n- .cache\n...\nRun Code Online (Sandbox Code Playgroud)\n\n我在脚本中使用过滤器列表run_rsync.sh:
#!/bin/bash\n\ndate="$(date +%Y-%m-%d)"\nhostname="$(hostname)"\n\n# debug_mode="" # to disable debug mode\ndebug_mode="--list-only"\n\n# Note: With trailing "/" at source directory, source directory is not created at destination.\nrsync ${debug_mode} --archive --delete --human-readable --filter="merge ${hostname}.rsync.filters" --log-file=logfiles/$date-$hostname-home.log --verbose /home backup/\nRun Code Online (Sandbox Code Playgroud)\n\n不幸的是,现有的 StackExchange 线程并没有解决我的问题:
\n\n\n\n这里出了什么问题?
\n\n[更新] 以下是主目录的外观以及要保留哪些文件以及要忽略哪些文件的示例:
\n\nuser@hostname:~$ tree /home/ | head\n/home/\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 user\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Desktop -> keep this\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file1 -> keep this\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 file2 -> keep this\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Documents -> keep this\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Repos\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pull_all.sh -> keep this\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 subdir1\n \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 output -> keep this\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 subdir2\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 another_subdir\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 output -> keep this\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 subdir3 -> do not keep (because does not contain any "output")\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file3 -> do not keep\nRun Code Online (Sandbox Code Playgroud)\n
稍微重申一下我对您的要求的解释,
pull_all.sh文件,无论我们在哪里找到它们output目录及其内容,无论我们在哪里找到它们Repos我们已经说明的目录之外的目录这可以指定如下
rsync --dry-run --prune-empty-dirs -av
--include 'pull_all.sh'
--include 'Repos/**/output/***'
--include '*/'
--exclude 'Repos/***'
/home backup/
Run Code Online (Sandbox Code Playgroud)
一些笔记
--include '*/'是必需的,以便rsync考虑向下进入Repos目录树(以查找pull_all.sh文件),否则最终语句将排除该文件--exclude。*是不同的:
*匹配除/字符之外的任何内容**匹配任何内容,包括/字符dir/***是相当于指定dir/and的快捷方式dir/**。--prune-empty-dirs标志停止rsync创建空目录,这尤其重要,因为我们需要处理Repos目录树以查找pull_all.sh项目output。--dry-run当您对结果满意时删除。