
リモート サーバーで毎月生成されるレポートを取得するスクリプトを実行しています。リモート サーバーから最新のファイルのみを取得する方法を見つけようとしていました。スクリプトで機能するものを見つけられるでしょうか、それともそれは悪い習慣でしょうか?
for host in "${hosts[@]}"; do
scp "$host":"$remote_path" "$local_target_dir"/filename."$host"
done
ファイル形式 = servername_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