2012-09-18 122 views
1

我正在使用lftp將文件從本地傳輸到僅允許SFTP訪問的遠程服務器。 我使用下面的語法來傳輸文件:驗證LFTP傳輸成功

lftp -c "open -u $UN,$Pass sftp://$Server ; mirror -R $Directory" 

,這是工作的所有罰款,當談到transffering文件。 因爲我使用這個作爲嵌入.sh文件中的cron,並且有時lftp失敗(原因不明)。

如何使用一些退出代碼或可用於確保轉移的正確性的方式驗證轉移?

+0

有人嗎?任何想法?這種轉移仍然間歇性地失敗,沒有任何日誌。 –

回答

4

我想,你必須把它包裝在一個shell腳本中。

的shell腳本必須生成一個腳本lftp的樣,比如:

set xfer:log true 
set xfer:log-file "/path/to/somewhere/$DATESTAMP.log" 
lcd "/path/of/source" 
open -u $UN,$Pass sftp://$Server 
mirror -R $Directory 
quit 

通過lftp -f $ScriptName執行。

然後,解析生成的日誌文件; AFAIK它只包含成功的轉賬。

awk '{print $3,$6}' $DATESTAMP.log | sed -r -e 's/ [0-9]+-/ /' 

以上將生成爲每個文件傳輸的文件和字節的列表。您可以通過循環的結果與實際的文件大小比較,例如

# WARNING: Script assumes that filename contains no space. 
while read fname fsize; do 
    # The following line used if the lftp runs from e.g. the source folder 
    actualname=$Prefix/$fname 
    actualsize=$(stat -c "%s" "$actualname") 
    if ((actualsize != fsize)); then 
    # Some error handling code here 
    fi 
done <<< "$(awk '{print $3,$6}' $DATESTAMP.log | sed -r -e 's/ [0-9]+-/ /')" 

注:我用的是「herestring」存在,而不是管道,使的while循環中的變量操縱。