Bash 脚本以 scp 远程服务器上目录中的最新文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4111967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:52:03  来源:igfitidea点击:

Bash script to scp newest file in a directory on a remote server

bashcronbackupscpdatabase-backups

提问by beatbreaker

Ok so I kinda know how to do this locally with a find then cp command, but don't know how to do the same remotely with scp.

好的,所以我有点知道如何使用 find then cp 命令在本地执行此操作,但不知道如何使用 scp 远程执行相同操作。

So know this:

所以知道这个:

scp -vp me@server:/target/location/ /destination/dir/.

That target directory is going to be full of database backups, how can I tell it to find the latest backup, and scp that locally?

该目标目录将充满数据库备份,我如何告诉它在本地找到最新的备份和 scp ?

回答by DigitalRoss

remote_dir=/what/ever
dst=remote-system.host.name.com
scp $dst:`ssh $dst ls -1td $remote_dir/\* | head -1` /tmp/lastmod

回答by Ignacio Vazquez-Abrams

Write a script on the remote side that uses findto find it and then catto send it to stdout, then run:

在远程端编写一个脚本,用于find查找它,然后cat将其发送到标准输出,然后运行:

ssh me@server runscript.sh > localcopy