Backup files with tar

[NOTE]
I don’t remember where I got this article from but it is full of some good information
[/NOTE]

TAR is the Unix Tape ARchive utility. It can be used to either store data on a streaming tape device like a DAT drive, or store files in what is commonly called a tarball file- somewhat like a pkzip file, only compression is optional.

[The basics]

In these examples, I will use the following file structure: a top level directory called DIR1 containing the files picture.jpg, document.doc and database.db.

DIR1/
DIR1/picture.jpg
DIR1/document.doc
DIR1/database.db

[Creating a tarball]

If we were in the directory DIR1 and wanted to backup all the files to a tarball called backup.tar, we could issue this command:

$ tar cvf backup.tar .
./
picture.jpg
doucment.doc
database.db
tar: backup.tar is the archive; not dumped

Note:
c=create (an archive)
v=verbose (just because)
f=filename (the name of our tarball)
.=current directory (what’s going to be backed up)

Also worth mentioning is that by default tar is recursive- meaning it will back up all files and subdirectories recursively unless you otherwise specify with the n flag (non-recursive)

[Displaying the Contents of a Tarball]

The current directory will now contain a file called backup.tar. To display the contents of the tarball file, we could issue this command:

$ tar tvf backup.tar
drwxr-xr-x root/gci 0 Jun 29 10:10 ./
-rw-r–r– root/gci 1 Jun 29 10:10 picture.jpg
-rw-r–r– root/gci 1 Jun 29 10:10 document.doc
-rw-r–r– root/gci 1 Jun 29 10:10 databse.db

Note:
t=table of contents (list)
v=verbose (display all info)
f=filename (backup.tar)

[Extracting Data from a Tarball]
To extract the entire contents of the tarball to the current directory, we can type:
$ tar xvf backup.tar
./
picture.jpg
doucment.doc
database.db

Note:
x=extract
v=verbose
f=filename (backup.tar)

To extract only the picture.jpg file from the archive, type the following command:

$ tar xvf backup.tar picture.jpg

Alternatively, you can use wild cards in either the creation or extraction of a tarball. To extract all jpg files from our archive, we can use a command like this:

$ tar xvf backup.tar *.jpg

[Using Compression]

If you would also like to add compression to your tarballs, you can combine the gzip utility with tar on the command line by adding the z switch to the command. Usually when this is done, we change the suffix of our tarball filename from .tar to either .tgz or .tar.gz. This will let whoever sees the file know that it is a gzipped tarball.

$ tar zcvf tarball.tgz .

Note:
z=gzip compression
c=create
v=verbose
f=filename (backup.tgz)
.=current directory (what to backup)

[Permissions with tar]

If you would like to preserve the permissions of the files you backup, use the p option with the tar command. This will save the uid, gid as well as the specific permission attributes of the files (read, write, execute etc.)

$ tar pzcvf tarball.tgz .

You should also use the p option with the tar extraction command:

$ tar pxvf tarball.tgz .

Advertisements
Tagged , , ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: