Backup a system to a remote location using ‘netcat’ and ‘tar’

I use to say “backup is only for wimps”. But to be honest, I do backups. And I even store the backup media in a save place.

To get the data onto a backup device and put that one to a save place, sometimes you have to write the backup over the network.

netcat or nc, the swiss army knife of networking is a big help for that.

On the remote system, where you want to write the backup start netcat:

nc -l -p 12345 > /var/backup/name-of -the-backup-2010-08-18.tgz
  • -l means listen
  • -p <number> is the port, where nc listens.

On the system you want to backup  you can exclude some directories, like /proc and /sys from being backed up. So run:

echo "./proc
./sys
./tmp" > /tmp/X

Now it is time to start the backup:

cd /
tar -X /tmp/X -czpf - . | nc 11.12.13.14 12345

So you cd into the root directory, exclude the files listed in /tmp/X, write the backup to STDOUT  and backup everything under the current directory. The backup is done relative.

Of course, you could use a backup command like tar -czpf – /,  but then the backup is done absolute. You realize the advantage of doing relative backups, when you want to restore the backup into a directory. With an absolute backup everything is written back to the original location.