
rsync 排除 文件

Rsync is one of the most useful utilities for a server administrator, but it syncs everything by default, which can be annoying if your application creates a lot of temporary files. Here’s how to exclude files when using rsync.
Rsync是服务器管理员最有用的工具之一,但默认情况下它会同步所有内容 ,如果您的应用程序创建了很多临时文件,这可能会很烦人。 这是使用rsync时如何排除文件的方法。
从文件列表中排除 (Excluding from a List in a File)
This is the ideal method for excluding files and folders, since you can always edit the list and tweak things if necessary. Here is the syntax:
这是排除文件和文件夹的理想方法,因为您始终可以编辑列表并在必要时进行调整。 语法如下:
rsync --exclude-from=/path/to/exclusion-file /path/to/source /path/to/dest
rsync --exclude-from=/path/to/exclusion-file /path/to/source /path/to/dest
The tricky thing with rsync is that you need to use a relative path when you are trying to exclude things, because when it tries to match the exclusions it won’t use the first part of the path for the match… it’s weird.
rsync的棘手之处在于,当您尝试排除事物时,您需要使用相对路径,因为当它尝试匹配排除项时,它将不会使用路径的第一部分进行匹配……这很奇怪。
Say, for example, you are trying to backup /data/web/ and send it to another server, so you use a command like rsync -a /data/web/ user@server:/backups/data/web/ to make it happen… but you’d really like to skip syncing the /data/web/cache/ folder. When rsync goes to check your exclusion list for each item that it syncs, it won’t check /data/web/cache/ since your original rsync command is based in the /data/web/ folder. It’ll just check “cache/” against the list. So you’ll need to put “cache” into the list, not the full path. Example:
举例来说,假设您正在尝试备份/ data / web /并将其发送到另一台服务器,所以您使用rsync -a / data / web / user @ server:/ backups / data / web /之类的命令来制作它发生…,但是您真的很想跳过/ data / web / cache /文件夹的同步。 当rsync去检查它同步的每个项目的排除列表时,它不会检查/ data / web / cache /,因为原始的rsync命令基于/ data / web /文件夹。 只需对照列表检查“ cache /”。 因此,您需要将“缓存”而不是完整路径放入列表中。 例:
rsync -a --exclude-from=/data/exclusions /data/web/ /backups/
Now to exclude /data/web/cache and /data/web/temp from rsync using this command, we’d remove the /data/web/ portion of the path and the /data/exclusions file would simply contain this:
现在,要使用此命令从rsync中排除/ data / web / cache和/ data / web / temp,我们将删除路径的/ data / web /部分,并且/ data / exclusions文件将仅包含以下内容:
cache* temp*
cache* temp*
You’ll note that I added the * into the path, to make sure that it matches anything that starts with “cache” at the beginning. You can use this star pattern for more useful reasons if you wanted — say you want to exclude all .txt files from being synced. You’d start the pattern with the star to make sure that always matches, and add this:
您会注意到,我在路径中添加了*,以确保它与开头以“ cache”开头的所有内容匹配。 如果需要,您可以出于更有用的原因使用此星形模式-表示要排除所有.txt文件,使其不被同步。 您将以星号开始模式以确保始终匹配,然后添加以下内容:
*.txt
That would ensure that those types of files are skipped during a sync. It’s pretty simple beyond that.
这样可以确保在同步过程中跳过这些类型的文件。 除此之外,这非常简单。
排除单个项目 (Excluding a Single Item)
This technique is much less useful, but you can use it on the fly if you need to. If you are setting up a script to use rsync, which you usually are, you should take the extra minute to exclude from a file list instead to make future maintenance easier. The syntax is very similar:
该技术用处不大,但是您可以根据需要即时使用它。 如果要设置脚本来使用rsync(通常如此),则应多花一些时间从文件列表中排除,以使将来的维护更加容易。 语法非常相似:
rsync --exclude=relative/path/to/exclusion /source /dest
rsync --exclude=relative/path/to/exclusion /source /dest
The same relative path should apply here as above.
相同的相对路径应与上述相同。
翻译自: https://www.howtogeek.com/168009/how-to-exclude-files-from-rsync/
rsync 排除 文件