최신 파일 복사

최신 파일 복사

원격 서버에서 매월 생성된 보고서를 선택하는 스크립트가 실행 중입니다. 원격 서버에서만 최신 파일을 가져오는 방법을 찾으려고 했습니다. 스크립트에서 작업을 찾을 수 있나요? 아니면 나쁜 습관인가요?

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

관련 정보