Backup System Using TAR and Netcat

The author wrote this article based on Ubuntu Feisty. However, almost all commands also can be used on other Linux distributions. The command has also been used on CentOS.

Tar tool allows us to make backups or even restore them, while our system is running.

It is a good idea becoming root to be able to access every file on the system, or at least you should use sudo command:

$sudo tar cvzpf /home/Backup.tgz --same-owner --exclude=/home/Backup.tgz --exclude=/home/error.log --exclude=/proc/* --exclude=/media/* --exclude=/dev/* --exclude=/mnt/* --exclude=/sys/* --exclude=/tmp/* / 2>/home/error.log

Tar options and arguments

c creates a tar backup

v activates verbose mode

z will compress data using gzip format

p preserves file permissions

f sends output to a file instead of standard output.

After f option it is necessary to write the file name where backup will be stored.

i.e: tar cvzpf /home/Backup.tgz is correct, but
tar cvzfp /home/Backup.tgz is wrong.

/home/Backup.tgz is our backup file name.

--same-owner preserve file ownership, it is not necessary because, being root, is a default option.

Excluding files from Backup

--exclude option gives us the possibility of excluding files and directories from backup.

--exclude=/home/Backup.tgz --exclude=/home/error.log Prevent Backup file and error log file to be stored.

--exclude=/proc/* --exclude=/dev/* --exclude=/sys/*

These are virtual directories so will not be stored.

NOTE: Difference between --exclude=/proc/* and --exclude=/proc is that first option would store a void proc directory, and second one would store nothing.

--exclude=/tmp/*

/tmp is a temporary directory, and is deleted on every boot, so will not be backed up either.

--exclude=/mnt/* --exclude=/media/*

If you want to save your main system only, and not what you have mounted on it, you should exclude these directories.

Logging errors

2>/home/error.log redirects standard error output to error.log file so we will be able to check if there have been important errors when creating backup file.

Normally some warnings appear about not storing some sockets and not including / at beginning of files. You can simply ignore them.

RESTORING YOUR SYSTEM

exec:
$sudo tar xvzf /home/Backup.tgz -C /

TESTING BACKUP FILE

To test if your backup file has no errors try this command:
$tar tvzf /home/Backup.tgz

BACKING UP VIA NETWORK

We are going to send our backup copy over the network using netcat tool.

We have two computers:

- Local computer with IP_local address. Contents of this computer will be backed up.
- Remote computer, which ip addess is IP_remote. Backup file will be stored in this computer.

First at remote computer launch netcat in listening mode:
$nc -l -p 6000 >/home/Backup.tgz

That command starts listening at port 6000 and sends what receives to file /home/Backup.tgz.

Second at local computer:
$sudo tar cvzp --same-owner --exclude=/home/error.log --exclude=/proc/* --exclude=/media/* --exclude=/dev/* --exclude=/mnt/* --exclude=/sys/* --exclude=/tmp/* / 2>/home/error.log | nc -w 3 IP_remote 6000

NOTE: We have deleted f and --exclude=/home/Backup.tgz options because tar data is sent to standard output instead of to a file.

Backup starts, is sent via network and stored at remote computer.

NOTE: You need to start first netcat in listening mode or it will give you a connection error.