简体   繁体   English

确定SCP传输是否失败的最佳方法

[英]Best way to determine if an SCP transfer fails

At the end of a script if successful it creates a file (files) and stores it into a destination directory; 如果成功,在脚本末尾,它将创建一个文件(多个文件)并将其存储到目标目录中。 I'm then retrieving the file (files) via scp from a server back to my workstation. 然后,我通过scp从服务器检索文件(文件)回到我的工作站。 The user of the script has the ability to determine an output location for the file (or files) they are retrieving. 脚本的用户可以确定要检索的文件的输出位置。

What would be the best way to determine if the file (files) were successfully transferred? 确定文件是否成功传输的最佳方法是什么? My main issue being if the user retrieves the file to a location like /home/$USERNAME/Desktop and there are files on the desktop. 我的主要问题是用户是否将文件检索到/ home / $ USERNAME / Desktop之类的位置,并且桌面上有文件。 What's the best way to determine if there is a new file on the desktop that was retrieved by means of the script? 确定桌面上是否有通过脚本检索到的新文件的最佳方法是什么?

In the end, I'm really just trying to determine if the transfer failed or not. 最后,我实际上只是在尝试确定传输是否失败。 When the script ends, I'd like to inform the user that the file was successfully transferred to the output directory. 脚本结束后,我想通知用户文件已成功传输到输出目录。

Here are my thoughts... 这是我的想法...

if (ssh user@server "[ find /myFiles -maxdepth 0 -empty -exec return 0 \; ]")
then
    scp user@server:/myFiles localhost:/DirToSave 
fi

But this isn't working, however, I think using this method will simplify my other issues by just letting me know that the files I'm looking for aren't there anyway. 但这是行不通的,但是,我认为使用此方法将使我知道我要找的文件根本不存在,从而简化了其他问题。 Is there a better way to do this? 有一个更好的方法吗?

Try rsync. 尝试rsync。

rsync -arvz -e ssh user@server:/myFiles localhost:/DirToSave 

This will tell you in the output if files have changed. 这将在输出中告诉您文件是否已更改。 You can run it multiple times cheaply 您可以廉价地多次运行它

An error code will be returned if it fails. 如果失败,将返回错误代码。 Can be tested with: if [ $? -ne 0 ] 可以测试: if [ $? -ne 0 ] if [ $? -ne 0 ] This may be sensitive if files change while the code is writing or permissions can't be set, such as on timestamps. if [ $? -ne 0 ]如果在编写代码时更改文件或无法设置权限(例如时间戳),这可能很敏感。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM