pulling and pushing with ssh

this post is from my old, first website, dated 2011-08-10. It’s the one I most refer back too.

The usual way is to pull data. This is often more secure. Say you have a web server and a file server for backup. The backup server is likely more trustworthy than the web server. You don’t want to be typing your backup server password on a possibly compromised web server. So from the backup server, you type:

ssh someuser@mywebserver.com tar cvf - some-folder >some-folder.tar

The dash tells tar to use standard output for the -f option. The redirection to some-folder.tar is interpreted by the local shell.

But sometimes, you are working on a server, already logged in, and you consider the server secure. Say a virtual host system. You want to run a program and send the output to another machine, a backup server or another virtual host, without leaving an intermediate file laying around. This is slightly more obscure:

virsh dumpxml some-guest |ssh someuser@some-other-host 'cat - >some-guest.xml-`date +%F`'

Here the dash tells cat to read standard input. The single quotes keep the redirection of the output of cat on the remote host.

-`date +%F` adds a datestamp to the filename.

Variation, just copy a file. Of course, you could use rsync, etc..

cat somefile |ssh me@remotehost 'cat - >somefile'

Leave a Reply

Your email address will not be published. Required fields are marked *