The Simplest Way to Make a Backup with TAR in your Linux Box

November 3, 2007

1. How To Make A Backup

To make a Good Backup in a Linux System such as Ubuntu or Debian, is possible to use TAR utility. Why TAR and not other backup system? TAR is usually used to create archive of entires directory and after the tar process we usually compress it in a GZIP file to distribute our archives to a friend by mail. Often, many Linux Users found in Norton Ghost a good utility to make backup of entire HardDisk. I think: “Why use a commercial application when there is just a better solution integrated in my linux system?”. Often we don’t use it because is a command line application and seems to be difficult to use it. No panic! I have a simple driven solution for you. Every step is explained clearly.

The first think to known is: I will be root to make this job!

sudo su

We have to make a directory where store backup, for example “/backup”.

mkdir /backup

We will change directory in root “/”.

cd /

Now I can type the command that I use usually to make a backup of my system.

tar cvpzf /bakup/backup.tgz –exclude=/proc –exclude=/lost+found –exclude=/mnt –exclude=/sys / –exclude=/dev –exclude=”/backup” –exclude=”/media”

As you can see, I’ve used “tar” command with options “cvpfz”. Why “cvfpz”?

You can see option using command “man tar”, but i will explain it simply:
“c” stands for create; “v” verbose option; “p” preserve file and permission structure; “z” make tar archive in a GZIP compressed file.

We have excluded some directories in our backup. For example “/backup” (because TAR can’t make its job if you try to make an archive of the actual processed archive), “/sys”, “/media”, “/mnt”, “/dev” (every of this directory is not necessary to backup, because are virtual system direcotry or for “/mnt” if you have a second HardDisk mounted on it TAR could be backup also it!)

Afterwards you will have a file called backup.tgz in your /backup directory. Now you can burn it in a CD/DVD or copy in a second HardDisk.

At the end of the process you might get a message along the lines of ‘tar: Error exit delayed from previous errors’ or something, but in most cases you can just ignore that.

2. How To Restore A Backup TAR file

A Good think of Linux is the possibilty of overwriting entire filesystem without any Live Boot CD. But, if you have a system unbootable because damaged, you have to boot with a LiveCD as Rescue System.

Now, you have to extract your file located in /backup directory, so:

cd /backup

tar xvpfz backup.tgz -C /

As you can see, I have added an option in TAR command. “x” option stands for extract.
When it is done, you have a fully restored Ubuntu system! Just make sure that, before you do anything else, you re-create the directories you excluded:

mkdir proc
mkdir lost+found
mkdir mnt
mkdir sys
mkdir dev

And when you reboot, everything should be the way it was when you made the backup! Have fun with your Linux Box!

Related Post