Using tar for a backup

Let’s get down to some basics. One of the most common things that you will want to do, no matter what kind of business you run or what you are using your computer for, is backup your data. There are many ways to do this, however, this article addresses manual backups.

One of the easiest ways to create a backup is to use “tar” which is short for Tape Archive. The tar program is native to linux systems and was initially designed to archive systems to a tape backup. The command is not limited to tape backups, however, as it can be used to archive any group of files into one “archive” file, which can be stored either separately on a computer or on a backup device such as a tape, thumbdrive, dvd, or external harddrive.

There are two benefits to creating a tar archive as opposed to performing a basic copy of the data. First, you preserve path, ownership, and creating date information in all of the files. Second, you can compress the file to save on space by using tar in conjunction with either gzip or bzip. The resulting archive files typically have a .tar.gz extension to indicate that they have been both tarred and zipped.

A manual backup can be very useful. In my case, I backup data once a week to move it to an off site location by way of an external harddrive.

I would like to do the following:
Back up my working files, stored in /vol1
Ignore temporary files stored in directory /vol1/temp
Compress the data
Create an easy “batch” script so that I don’t have to type in the whole command each time I run the backup

The general layout of the tar command is:

tar [options] [directory to backup]

There are many options and details that you can discover by typing “man tar,” but here’s a run down of the options we’ll use:

z - compress the file using gzip (you can use bzip by using the option 'j')
c - create a new file (as opposed to appending the new files to the end of another archive)
v - verbose - display each file on the screen as it is added to the archive
R - Recursive - include subdirectories
f - specify the output file (otherwise it will output to stdout) 

Here is my command:

tar -zcvRf vol1.tar.gz --exclude "/vol1/temp" /vol1

As you can see, I am creating a new archive called vol1.tar.gz which will zip all files and directories contained within /vol1, excluding /vol1/temp. I can later extract and restore the files by typing:

tar -zxvf vol1.tar.gz

The options are almost identical, however we do not need to create an archive, and we will extract the files (option x).

To list the files in the archive, you can type

tar -tf vol1.tar.gz

If you do not see anything happening after typing tar, you probably did not include the -f option and tar is waiting for input from stdin.

The final step in creating a batch script to allow you to simply run the backup with a simple command. In my case, I would like to save a couple recursive backups so that, if I want, I can go back to data from a few times ago in case of an error.

Use your favorite editor to create a file called I use nano by doing the following:


Type the following:

rm vol1.tar.gz.backup # remove the old backup
mv vol1.tar.gz vol1.tar.gz.backup # move the current tar file to a backup
tar -zcvRf vol1.tar.gz --exclude "vol1/temp" /vol1

Save and close the file by typing
ctl-o then ctl-x
Now you can run the file by simply typing


Keep in mind that the tar file will be created in the directory you are currently in. I keep this program on my backup drive and run the command from there.

Have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *