Jef*_*ter 22 permissions rsync error-handling
我正在使用rsync -rlptD
从另一个用户复制目录。有一些文件(我无法提前知道)我无权复制。有没有办法让 rsync 忽略这些。问题是,如果 rsync 返回非零值,我的 bash -x 脚本将退出。
Gil*_*il' 10
Rsync 没有这个选项。我看到了两种解决方案。一是解析rsync错误信息;这不是很健壮。另一种是生成不可读文件列表进行过滤。
cd /source/directory
exclude_file=$(mktemp)
find . ! -readable -o -type d ! -executable |
sed -e 's:^\./:/:' -e 's:[?*\\[]:\\1:g' >>"$exclude_file"
rsync -rlptD --exclude-from="$exclude_file" . /target/directory
rm "$exclude_file"
Run Code Online (Sandbox Code Playgroud)
如果您find
没有-readable
and -executable
,请用适当的-perm
指令替换它们。
这假设没有名称包含换行符的不可读文件。如果你需要处理这些,你需要像这样生成一个空分隔的文件列表,并将-0
选项传递给rsync
:
find . \( ! -readable -o -type d ! -executable \) -print0 |
perl -0000 -pe 's:\A\./:/:' -e 's:[?*\\[]:$1:g' >>"$exclude_file"
Run Code Online (Sandbox Code Playgroud)
我针对这种特定情况做了一个简单的解决方法:
rsync --args || $(case "$?" in 0|23) exit 0 ;; *) exit $?; esac)
Run Code Online (Sandbox Code Playgroud)
这将返回0
如果返回码为0或23,并返回在其他情况下,退出代码。
然而,重要的是要注意,这将忽略所有Partial transfer due to error
错误,而不仅仅是权限错误,因为它将捕获退出代码的所有内容23
。有关 rsync 状态代码的更多信息,请参阅此链接。