More often than not, the rsync tool is available for use when
you need to do a remote backup of software. It’s a great tool for this because
you can utilize ssh encryption and authentication as the transport layer for synchronizing
files from one machine to the next. But sometimes rsync isn’t exactly what is
required or may not even be available.

With nothing more than ssh, tar, and bash, you can perform
remote system backups easily, regardless of the operating system. Even on
Windows, using Cygwin, you can automate backups quickly and easily.

The first, simplest way is to create a tar archive on one
machine and send it to the other to uncompress. Normally, you would create the
archive, scp it to the other machine, then unarchive it. However, you can
accomplish the same task in just one easy step with this command:

$ tar cf - files | ssh surtr "(cd /home/joe/tmp; tar xf -)"

This one-line command will create an archive of the directory
files/ and create a tar archive, sending the output to STDOUT instead of to a
file. You then pipe the output of tar to another tar instance that you invoke
after ssh-ing to the remote machine surtr,
changing to the directory /home/joe/tmp
and extracting the contents of the tar archive, which is taken as standard
input, to that directory.

If compressing the data is important, you can do something
similar. The following command illustrates the reverse of the above; instead of
pushing data from the local machine to the remote machine, it compresses data
on the remote machine then copies it to the local machine.

$ ssh surtr "(cd /home/joe/tmp; tar cvjf /home/joe/pkgs.tar.bz2 pkgs)" \
&& scp surtr:~/pkgs.tar.bz2 .

This command first logs into the remote machine surtr and then changes directory and creates
the file ~joe/pkgs.tar.bz2, which
contains the contents of the directory /home/joe/tmp/pkgs.
If this is successful, scp is then invoked to copy the remote pkgs.tar.bz2 file to the local machine.

The first command is obviously the simplest, but at the cost
of some bandwidth, which might be negligible if you use compression within ssh.
The second command, while longer, is a bit more flexible and illustrates many things
you can do with one-line commands (even though we cheated a bit and ran two
commands in a single line by using the &&
construct).

Regardless, in a pinch, knowing how to use
almost-always-available tools like tar, ssh, and bash can be a real life-saver.
These tools can be used to quickly accomplish hundreds of tasks with a little
creativity.

Delivered each Tuesday, TechRepublic’s free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!