前段时间,我需要了解我正在编写的脚本的rsync 输出。在编写该脚本的过程中,我四处搜索并找到了@mit 写的above。我使用这些信息以及其他来源的文档来创建我自己的位标志入门以及如何让rsync 为所有操作输出位标志(默认情况下不这样做)。
我在这里发布这些信息,希望它可以帮助其他人(像我一样)通过搜索偶然发现此页面并需要更好地解释 rsync。
结合--itemize-changes 标志和 -vvv 标志,rsync 为我们提供了与目标相比在源目录中识别的所有文件系统更改的详细输出目录。然后可以对rsync 产生的位标志进行解码以确定发生了什么变化。要解码每个位的含义,请使用下表。
rsync的输出中每个位的位置和值的解释:
YXcstpoguax path/to/file
|||||||||||
||||||||||╰- x: The extended attribute information changed
|||||||||╰-- a: The ACL information changed
||||||||╰--- u: The u slot is reserved for future use
|||||||╰---- g: Group is different
||||||╰----- o: Owner is different
|||||╰------ p: Permission are different
||||╰------- t: Modification time is different
|||╰-------- s: Size is different
||╰--------- c: Different checksum (for regular files), or
|| changed value (for symlinks, devices, and special files)
|╰---------- the file type:
| f: for a file,
| d: for a directory,
| L: for a symlink,
| D: for a device,
| S: for a special file (e.g. named sockets and fifos)
╰----------- the type of update being done::
<: file is being transferred to the remote host (sent)
>: file is being transferred to the local host (received)
c: local change/creation for the item, such as:
- the creation of a directory
- the changing of a symlink,
- etc.
h: the item is a hard link to another item (requires
--hard-links).
.: the item is not being updated (though it might have
attributes that are being modified)
*: means that the rest of the itemized-output area contains
a message (e.g. "deleting")
rsync 用于各种场景的一些示例输出:
>f+++++++++ some/dir/new-file.txt
.f....og..x some/dir/existing-file-with-changed-owner-and-group.txt
.f........x some/dir/existing-file-with-changed-unnamed-attribute.txt
>f...p....x some/dir/existing-file-with-changed-permissions.txt
>f..t..g..x some/dir/existing-file-with-changed-time-and-group.txt
>f.s......x some/dir/existing-file-with-changed-size.txt
>f.st.....x some/dir/existing-file-with-changed-size-and-time-stamp.txt
cd+++++++++ some/dir/new-directory/
.d....og... some/dir/existing-directory-with-changed-owner-and-group/
.d..t...... some/dir/existing-directory-with-different-time-stamp/
捕获rsync 的输出(专注于位标志):
在我的实验中,--itemize-changes 标志 和 -vvv 标志都需要让rsync 输出所有文件系统更改的条目。如果没有三重详细 (-vvv) 标志,我没有看到列出的目录、链接和设备更改。值得对您的 rsync 版本进行试验,以确保它能够观察并记录您所期望的一切。
此技术的一个方便用途是将--dry-run 标志添加到命令中,并将由 rsync 确定的更改列表收集到一个变量中(不进行任何更改),这样您就可以自己对列表进行一些处理.类似以下的内容会在变量中捕获输出:
file_system_changes=$(rsync --archive --acls --xattrs \
--checksum --dry-run \
--itemize-changes -vvv \
"/some/source-path/" \
"/some/destination-path/" \
| grep -E '^(\.|>|<|c|h|\*).......... .')
在上面的示例中,rsync 的 (stdout) 输出被重定向到 grep(通过标准输入),因此我们可以仅隔离包含位标志的行。
处理捕获的输出:
然后可以记录变量的内容以供以后使用或立即迭代感兴趣的项目。我在研究更多关于rsync 时编写的脚本中使用了这个确切的策略。您可以查看脚本 (https://github.com/jmmitchell/movestough) 以获取对捕获的输出进行后处理以隔离新文件、重复文件(相同名称、相同内容)、文件冲突(相同名称、不同内容)以及更改的示例在子目录结构中。