How-to : Using Tar (Taring)

Ok well here is a short listing on how to use the command tar to backup you data..
Tar is solely an archiving app. Tar by its self wont compress files.

But you say “then what is a .tar.gz”

It’s a tar file that has been compressed with a different compression utility. The .gz=gzip is the compression app use to compress it.

Here is tar in its simplest form

tar -cvf filename.tar /path/to/files

-c means create
-f means filename (-f should always be last when you using syntax)
-v Verbose will display all the files its puts in the tar and error you might have incurred
You should see the filename.tar file in what ever directory you ran tar from.

You say “But I want to make the tarball compressed”

Well then -z is the option you want to include in your syntax

tar -zvcf filename.tar.gz /path/to/files

#notice I had to add the .gz extension.
-Z( no not -z) will run it thru the old compress app.

Now when I make a tarball I like to keep all the path’s from which the file is in.
For this use the -P (absolute path)

tar -zPvcf filename.tar.gz /path/to/file

When I extract it I will see a new directory called /path
and under that I will see the “to” directory, and the “file” is under “to”

Now you say “I want to backup ALL my files in my home directory EXCEPT the temp directory I use”. No problem.

tar -zPvcf myhomebackup.tar.gz –exclude /home/erik/temp /home/erik

The –exclude will give you this option, just slip it in between the tar filename and the path your going to backup. This will exclude the whole temp directory.

You say “Ok this tar thing is pretty cool but I want to backup only single files from all around the drive.

No problem, this requires a bit more work, but hey this is UNIX, get used to it.

Make a file called locations (call it anything you like). In locations place the full path to each file you want to backup on a new line. Please be aware that you have to have read rights to the files you are going to backup.


Now with the -T option I can tell it to use the locations file.

tar -zPvcf backup.tar.gz -T locations

Now if you want to backup the whole drive. Then you will have to exclude lots of files like /var/log/* and /usr/local/named/*

Using the -X option you can create an exclude file just like the locations file.

tar -zPvcf fullbackup.tar.gz -X /path/to/excludefile -T /path/to/locationsfile

Now a month has gone by and you need to update your myhomebackup.tar.gz with new or changed files.

This requires a extra step (quit your bitching I already told you why)
You have to uncompress it first but not untar it.

gunzip /path/to/myhomebackup.tar.gz

This will leave your myhomebackup.tar.gz mising the .gz.
Now we can update your tarball with -u and then we are going to compress it again.

tar -Puvf myhomebackup.tar /home/erik | gzip mybackup.tar

It will add the .gz for you.

Tar is a pretty old app and has lots of Fetchers.
I suggest reading the man pages to get a lits of all the options.

I have included a little perl script that I made so I can run it as cron job evernight and get a full backup each time.
It wouldn’t be that hard to update the tarball but I just like full backups.
Feel free to use it.

If you want to extract the tarball that is compressed

tar -zxvf filename.tar.gz

-x extract

If it is not compressed then

tar -xvf filename.tar

#Created by Erik Mathis 7/02

#Change These paths to fix your needs.
my $filename=”/home/sysbkup/backup”;
my $exclude=”/home/erik/exclude”;
my $data=”/home/erik/locations”;
my $tar=”\.tar”;
my $gz=”\.gz”;


system (“tar -Pzcvf $file -X $exclude -T $data”);



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s