O'Reilly logo

Linux Server Hacks by Rob Flickenger

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #37. Backing Up with tar over ssh

Copy arbitrary bits of the filesystem between servers using ssh and tar

Shuffling files between servers is simple with scp :

root@inky:~# scp some-archive.tgz blinky:/

Or even copying many files at once:

root@pinky:~/tmp# scp clyde:/usr/local/etc/* .

But scp isn't designed to traverse subdirectories and preserve ownership and permissions. Fortunately, tar is one of the very early (and IMHO, most brilliant) design decisions in ssh to make it behave exactly as any other standard Unix command. When it is used to execute commands without an interactive login session, ssh simply accepts data on STDIN and prints the results to STDOUT. Think of any pipeline involving ssh as an easy portal to the machine you're connecting to. For example, suppose you want to backup all of the home directories on one server to an archive on another:

root@inky~# tar zcvf - /home | ssh pinky "cat > inky-homes.tgz"

Or even write a compressed archive directly to a tape drive on the remote machine:

root@blinky~# tar zcvf - /var/named/data | ssh clyde "cat > /dev/tape"

Suppose you wanted to just make a copy of a directory structure from one machine directly into the filesystem of another. In this example, we have a working Apache on the local machine but a broken copy on the remote side. Let's get the two in sync:

root@clyde:~# cd /usr/local
root@clyde:/usr/local# tar zcf - apache/ \  
| ssh pacman "cd /usr/local; mv apache apache.bak; tar zpxvf -"

This moves /usr/local/apache/ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required