![scp를 사용하여 폴더의 최신 파일을 다운로드하는 방법은 무엇입니까?](https://linux55.com/image/44990/scp%EB%A5%BC%20%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC%20%ED%8F%B4%EB%8D%94%EC%9D%98%20%EC%B5%9C%EC%8B%A0%20%ED%8C%8C%EC%9D%BC%EC%9D%84%20%EB%8B%A4%EC%9A%B4%EB%A1%9C%EB%93%9C%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F.png)
scp transfer를 실행하여 특정 디렉터리에서 내 로컬 디렉터리로 최신(최신) 파일을 다운로드하고 싶습니다.
이 같은:
- 원천:
[email protected]:/home/rimmer/backups/
- 목적지:
/home/rimmer/backups/
.backups
답변1
변수가 있고 정의되었다고 가정하면 server
다음 dir
을 수행할 수 있습니다.
$ dir="~"
$ server="[email protected]"
$ scp $server:$dir/$(ssh $server 'ls -t $dir | head -1') .
먼저 그곳에서 최신 파일을 찾아 복사하세요.
참고: 완벽한지 확인하지 않았습니다(예: 최신 항목이 폴더임).
답변2
scp
소스에서 대상으로 파일을 맹목적으로 복사한다는 점에서는 어리석은 일입니다. 보다 지능적으로 파일을 복사하려면 rsync
.
$ rsync -avz [email protected]:'$(find /home/rimmer/backups/ -ctime -1)' /home/rimmer/backups/
이렇게 하면 rimmer.sk 백업 디렉터리에서 지난 하루(-ctime -1)에 손실되거나 변경된 파일만 로컬 백업 디렉터리로 복사됩니다.
-ctime n
File's status was last changed n*24 hours ago. See the comments for
-atime to understand how rounding affects the interpretation of file
status change times.
인용하다
답변3
조금 늦었지만 ssh 및 rsync에 대한 솔루션이 누군가에게 효과적일 수 있습니다.
source_host="yourhost.com"
source_dir="/a/dir/on/yourhost.com/"
target_dir="/the/dir/where/last_backup/will/be/placed"
last_backup=$(ssh user@${source_host} "ls -t ${source_dir} | head -1")
if [ "${last_backup}" == "" ]; then
echo "ERROR: didn't find a backup, cannot continue!"
else
echo "the last backup is: ${last_backup}"
rsync -avzh user@${source_host}:${source_dir}/${last_backup} ${target_dir}
fi