我有一个 SFTP 服务器,客户端不断上传大文件。我想定期将所有完整(完全上传)的文件复制到另一台机器上进行处理。我不想复制正在积极写入的文件。有没有办法做到这一点?我目前正在使用 rsync,但我愿意切换到其他东西。
要检查文件当前是否打开(如果当前写入的文件肯定被某个进程打开),标准方法是使用lsof:
if lsof /your/file > /dev/null; then echo "file currently open"; fi
Run Code Online (Sandbox Code Playgroud)
您可以使用此代码段来过滤未打开文件的查找结果,并使用它们来提供 rsync:
find . -type f -exec sh -c 'if ! lsof `readlink -f {}` > /dev/null; then echo `basename {}`; fi' \; | tr '\n' '\0' | rsync -avz --from0 --files-from=- ./ user@host:destination/
Run Code Online (Sandbox Code Playgroud)
一些注意事项:
readlink -f 需要文件的完整路径,lsof 只接受完整路径tr '\n' '\0' 模拟查找 -print0