複製最新文件

複製最新文件

我們正在運行一個腳本,它可以獲取遠端伺服器上每月產生的報告。我試圖找到一種僅從遠端伺服器獲取最新文件的方法。會在劇本中找到工作嗎?

for host in "${hosts[@]}"; do
    scp "$host":"$remote_path" "$local_target_dir"/filename."$host"
done

檔案格式=伺服器名稱_BBC-3.0_2014-06-04_164510_.txt

答案1

您可以ls -rt透過 SSH 在伺服器上的目錄中執行來尋找上次修改的檔案(基於其上次修改日期而不是檔案名稱)

fileToCopy=$(ssh "$host" "cd $remote_path && ls -rt | tail -1")
scp "$host":"$remote_path"/"$fileToCopy" "$local_target_dir"/filename."$host"

答案2

我建議找出它來驗證您的日期並考慮上次備份,例如:

#!/bin/bash
day=${date +%d}
last_month=${date -d "-1 month" date +%Y-%m-%d}
if [ $day -eq 15]
then
    echo "Is 15th, time to make get last backup!"
    scp -P port user@server:/dir/servername_BBC-3.0_$last_month* destination
fi

相關內容