The best couples complement each other, and each member of the couple contributes unique and irreplaceable parts to the whole. But some couples are very odd. Such is the case with our best couple this year: the tar and ssh commands.
Yup, that's right, the tar and ssh commands work together in interesting ways, especially when used with full consideration of the capabilities of Standard I/O (STDIO), which is also known as standard streams.
The ssh command is a secure and sophisticated form of terminal emulator that allows one to log in to a remote computer to access a shell session and run commands. So I could log in to a remote computer and run the ls command on the remote computer. The results are displayed in the ssh terminal emulator window on my local host. The Standard Output (STDOUT) of the command is displayed on my terminal window, but it remains on the remote host and cannot be used by the local host.
That is trivial and everyone does that. But the next step is a bit more interesting. Rather than maintain a terminal session on the remote computer and issuing multiple commands, I can simply use a command like the following to run a single command on the remote computer with the results being displayed on the local host. This assumes that SSH public/private keypairs (PPKP) are in use and I do not have to enter a password each time I issue a command to the remote host:
ssh remotehost ls
So now I can use the results of that command on my local host because the standard output data stream is sent through the SSH tunnel to the local host. OK, that is good, but what does it mean?
Let's look at the tar command before answering that question.
The tar command is used to make backups. The name tar stands for Tape ARchive, but the command can be used with any type of recording media such as tape, hard drives, thumb drives and more. A command like the following can be used to create a backup of a home directory on the local host:
tar -cvf /tmp/home.tar /home
This command created a tar file—also called a tarball—named home.tar in the /tmp directory. That file is a backup of everything in the home directory. Well, that's nice, but also not very interesting because it is very common.
But what can be interesting is, although many people do not realize it, if the target output file is not specified using the -f option, the output of the tar command is sent directly to STDOUT:
tar -cv /home
That means that the complete output of the tar command—the files being backed up —is sent to the terminal, which opens up some interesting possibilities, such as redirecting the STDOUT data stream to a backup file. That looks like the following command:
tar -cv /home > /tmp/home.tar
This command performs the same function as the first tar command in this section, but in a somewhat different and more interesting manner.
The Odd Couple
We can use a command similar to the following to back up the home directory of the remote host to the /tmp directory of that remote host:
ssh remotehost "tar -cvf /tmp/home.tar /home"
Note that the command to be executed on the remote host is enclosed in quotes to ensure that the correct command is executed remotely; this is a bit of clarification for both the shell and for us humans. A slight change to this command gives us one in which we simply redirect the output of the tar command to the /tmp directory on the remote host:
ssh remotehost "tar -cv /home > /tmp/home.tar"
This command produces exactly the same result as the previous one. In this case, the STDOUT data stream of the tar command is maintained entirely on the remote host and is redirected to the backup file. The next command, however, is the one that opens up many new possibilities. Can you see what it does?
ssh remotehost "tar -cv /home" > /tmp/home.tar
In this case, the STDOUT data stream from the tar command is sent through the SSH connection to the local host. This stream of data is then redirected to the backup file /tmp/home.tar on the local host. By simply moving the trailing quote to the left, the command is changed so that we now have a command that can do backups of remote hosts to a local host.
I use our couple of the year every day to perform backups. I have a script that uses ssh and tar, along with SSH public key encryption, to perform backups of several remote hosts to an external USB hard drive on a local host. These two commands simplify a necessary task, and the best part is that they are free and open source software—free as in beer, as well as free as in speech.
So let's hear it for this year's Opensource.com Best Couple: tar and ssh.