Thursday, July 3, 2008

Backup fun with netcat (nc)

Was asked to help on backing up files in one server and then transfer the image to another server. Sounds simple, but unfortunately the server which files are to be backed up has very limited disk space and prevents me from creating a tar-ball.

Instead of creating a tar-ball in the server, I used netcat (or known as nc) to stream the files over to another server while running the tar command.

On the server, run the following command:
tar czvf - /opt | nc -l -p 8888 -c
This creates a tar gzipped file of the /opt directory and put the output to stdout. Pipe the output of tar to netcat (nc) which is listening (-l) to port (-p) 8888. The additional -c option is to close the connection after EOF on stdin. If this option isn't there, netcat will continue waiting for data from stdin. This is not what I want as I need the backup action to end properly in order to place it in a script.

On the receiving end, just run the following command:
nc 10.112.133.12 8888 > opt.tar.gz
This instructs netcat (nc) to connect to the IP on port 8888 and write the output to opt.tar.gz.

Well, there you have it. A quick and dirty way to transfer a backup image if you're short of space to create the backup file.

2 comments:

Lawrence Teo said...

If netcat is not available, you can do that with ssh too..

tar czvf - /opt | gzip -9 | ssh username@server "cat >opt.tar.gz"

It'll be slower than netcat but it's encrypted.. :)

Mike said...

Thanks for the tip. It's already quite slow with nc. But hey, performance can take a little hit if it's secured :)