9

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.

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?

2 Answers 2

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

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

ssh me@server runscript.sh > localcopy
6
  • So I make something like this remotely: ls -t | head -1 | cat stdout then i execute it locally with ssh me@server runscript.sh > localcopy ... then cat localcopy into a variable that gets used for the file name? Commented Nov 6, 2010 at 5:14
  • No. The script looks like: cat "$(ls -t | head -n 1)", and then localcopy will have the file's contents. Commented Nov 6, 2010 at 5:20
  • Humm seems no need for the cat, the >> does the output. so ls -t /var/backup| head -n 1 Commented Nov 6, 2010 at 5:41
  • That will print just the filename. Why not just get the file in the first place? Commented Nov 6, 2010 at 5:58
  • the remote server is always making new backups so the file names change constantly. I need the latest one. I ended up setting a variable for the file name like this: remote_backup=ssh $dst ls -1td $remote_dir/\* | head -1| cut -d/ -f6 I then just called in in the following:scp $dst:$remote_dir/$remote_backup $target Commented Nov 6, 2010 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.