Linux Babble 123-reg - Cheap domain names, free configuration

Backing up files with tar and netcat

June 19, 2004 at 14:59, by Geoff Richards

What is netcat?

Netcat (usually the command is actually called nc) is one of those simple Unix commands that you can plug together to do all sorts of handy things. It's a relatively recent addition to the Unix cannon, so it isn't available on every Unix box, but most Linux machines will have it.

Netcat simply establishes a TCP/IP network connection to a particular port (of your choosing) and sends data to and from it. It can also listen for connections on a port and talk to whatever connects. You can use it to test server programs (like webservers) by talking to them directly, or test client programs by having them talk to netcat rather than a real server, so that you can see what they're saying.

Transfering files over a network with netcat

If your stuck in a corner trying to transfer large amounts of data over a network (maybe you don't want to set up an FTP server, or very large files are causing problems with the file transfer programs you'd usually use) then netcat save the day. Here's how to do it.

On the machine that should receive the data, run netcat in ‘listen’ mode, writing data it receives on some arbitrary high-numbered port into a file:

nc -lp 1234 >backup.tar.gz

Whatever data gets sent to port 1234 will get fed into the file backup.tar.gz.

Then tar up the files you want to back up on the sending machine, and send them over the network to the same port:

tar czvf - stuff... | nc -q 0 backup-hostname 1234

The -q 0 option makes netcat break the connection as soon as it has sent all the data, rather than waiting for a response.

For some reason tar sends a load of extra null characters at the end when it's writing to a pipe, but that doesn't seem to cause any problems when extracting it. It'll be something weird to do with magnetic tapes.

You can also use nc with dd to send CD or hard disk images over the network:

dd if=/dev/cdrom bs=2k | nc -q 0 backup-hostname 1234