Five Pounds of Flax

Every culture has a burrito.

I *heart* Unix

Wednesday, April 04, 2007posted by Michael Rothwell @ 12:15 AM

Doing a remote backup of a server across the internet over an encrypted channel is one of many things that are easy on Linux and the Mac with built-in tools, but either impossible or non-obvious on Windows with built-in tools. It's one line of shell code on Unix. I don't know how I'd do it on a standard Windows installation (any flavor), or if it's even possible.

It's accomplished using "tar over ssh" to dump a compressed backup of specified files and directories to a remote server:

tar jcvf - /target/directory | ssh user@remotehost "cat >backup.tar.bz2"

It says to tar: create a bzip2-compressed archive file of the specified items on standard output. That data is piped into an ssh command's standard input, which logs into a remote server (set up host keys to avoid the password prompt) and pipes the data into a remote command -- in this case, "cat", which redirects its output into a file named "backup.tar.bz2". Nice!

You can run it out of cron each day, even, and have nice named backup files by doing something like this:

tar jcvf - /target/directory | ssh user@remotehost "cat >backup-`date +%F`.bkp.tar.bz2"

That will produce files named "backup-2007-04-04.tar.bz2" and "backup-2007-04-05.tar.bz2", etc.

The reverse -- restoring a file from a remote backup -- is just as easy.

I <3 Unix!