Posts Tagged ‘netcat’

Network backup with netcat (nc) and tar

// October 3rd, 2009 // 1 Comment » // linux

Sometimes I need to recover some data from a server via network and either don’t have enough space on the source server I want to backup or just want to get things copied to an other server really quickly.

In this cases I usually just use the abilities of tar and netcat to create a backup via the network.

Let’s assume we have two computers:

- Computer A) which will be backed up.
- Computer B), which ip addess is our REMOTE_IP and the backup will be stored in this computer.

To create a backup we login to Computer B and launch netcat in listening mode using the following command.

nc -l -p 6969 >/root/backup.tgz

This command tells netcat to start listening on port 6969 (you can use other free ports) and sends whatever it receives to /root/backup.tgz.

Once done the following command, executed on computer A will start the backup:

sudo tar cvzp --same-owner --exclude=/root/backup.log --exclude=/proc/* --exclude=/media/* --exclude=/dev/* --exclude=/mnt/* --exclude=/sys/* --exclude=/tmp/* / 2>/root/backup.log | nc -w 3 REMOTE_IP 6969

This will create a full backup of your computer and excludes various system files which usually should not be copied. It also creates a logfile in /root/backup.log.

NOTES

  • make sure to replace REMOTE_IP with the ip address of computer B and that the port number 6969 matches the one on computer B.
  • sometimes you might not have netcat available as “nc” and you need to type “netcat” instead.