Sometimes IT is life and death… Perhaps you’re being chased by an army of zombies, while quickly trying to update a file on a remote server. Or maybe you’re hacking together a quick little backup script, before running off to a Halloween party in July (after one more slice of peach pie first of course). This is a quick little trick that I use quite often to move files around when all you have is 4 acorns, half a pack of gum, $0.13 in pennies and SSH access on two servers. Yes there are other ways to do this (e.g.: rsync), but this is nice and simple, and quick and easy for those times when you just need to get it done!
First, let me show you an example, and then we’ll break down how it works:
ssh [email protected] "cd /usr/folder/stuff; tar czf - ." | tar xvzf -
What we’re doing here is copying a file (or files) from a remote host back to the local host, and putting it in our current working directory on the local host.ย The command starts from the current machine, connects to a remote host (that’s the SSH part at the beginning). Once on the remote host, we go into the /usr/folder/stuff directory and tar up everything there. That’s the middle portion inside the Quotes. The last little part, after the | is the commands that are issued on the local host, to un-tar the file again.
“tar xzf – .” tells tar to archive (c), use gzip compression (z), and put the output into a file (f). The – tells tar to pipe the output to the ssh connection instead of an actual file, and the . tells it to pickup everything in the current directory. That second tar command is run using the xvzf flags for: extract (x), verbose (v), de-compress (z), and file (f), and define the file as -, so that it reads the incoming data stream.
To go the opposite direction, and push files to a remote system, instead of pulling them to you. You simply reverse the syntax:
cd /usr/folder/stuff tar czf - . | "ssh [email protected]; tar xvzf -"
This will take the files in the /use/folder/stuff directory on the local machine, and place it in the users home directory on the remote machine. To do the same thing, but place the files in another directory, simply add a cd command to the remote side of the mix:
cd /usr/folder/stuff tar czf - . | "ssh [email protected]; cd /remote/dir; tar xvzf -"
And off you go!
The gzip flag for tar (z) is most useful on slow network connections (eg: the internet). if you’re using this on a fast local network (ie: gigabit ethernet), you should probably skip that flag, as the CPU overhead to do the compression will actually slow you down compared to just transferring the files uncompressed.