Simple Script to do so?

hi guys, i am a noob to shell scripting, and i would like to run a simple script, that could simply do the following: 1. SFTP to a remote server/path...and download the newest *.gz backup file on that server. (there are many *.gz files in that folder, i simply need the latest one) 2. locally unpack the received file.which contains an .sql backup 3. rename it locally to "backup.sql" and import it into mysql ( this is not the problematic part, i can do that). how can i do it best, and with least lines? Thanks in advance.

sftp and scp are ssh under the covers. Go over there, find the most recently modified file, and send it down the socket to gunzip, make the flat file copy you desire and send the data on to mysql for import real time:

ssh remote_user@remote_host '
ls -t /whatever/*.gz 2>/dev/null | while read f
do
 cat $f
 break
done
 ' | gunzip | tee backup.sql | mysql_import . . . - . . . .

If mysql still does not allow import from stdin pipe, you might be able to use a named pipe, aka FIFO, named for the target table. You might want to use a staging table, in case you need to rerun. Pipes kill latency.