9.1.1.1. Archiving with tar
In most cases, we will first collect all the data to back up in
a single archive file, which we will compress later on. The process
of archiving involves concatenating all listed files and taking out
unnecessary blanks. In Linux, this is commonly done with the
tar command. tar was
originally designed to archive data on tapes, but it can also make
archives, known as tarballs.
tar has many options, the most important
ones are cited below:
-
-v: verbose
-
-t: test, shows content of a tarball
-
-x: extract archive
-
-c: create archive
-
-f archivedevice: use archivedevice as source/destination for the
tarball, the device defaults to the first tape device (usually
/dev/st0 or something similar)
-
-j: filter through bzip2, see
Section 9.1.1.2
It is common to leave out the dash-prefix with tar options, as you can see from the examples
below.
|
Use GNU tar for
compatibility |
|
The archives made with a proprietary tar
version on one system, may be incompatible with tar on another proprietary system. This may cause
much headaches, such as if the archive needs to be recovered on a
system that doesn't exist anymore. Use the GNU tar version on all systems to prevent your system
admin from bursting into tears. Linux always uses GNU tar. When
working on other UNIX machines, enter tar
--help to find out which version you
are using. Contact your system admin if you don't see the word GNU
somewhere.
|
In the example below, an archive is created and unpacked.
gaby:~> ls images/
me+tux.jpg nimf.jpg
gaby:~> tar cvf images-in-a-dir.tar images/
images/
images/nimf.jpg
images/me+tux.jpg
gaby:~> cd images
gaby:~/images> tar cvf images-without-a-dir.tar *.jpg
me+tux.jpg
nimf.jpg
gaby:~/images> cd
gaby:~> ls */*.tar
images/images-without-a-dir.tar
gaby:~> ls *.tar
images-in-a-dir.tar
gaby:~> tar xvf images-in-a-dir.tar
images/
images/nimf.jpg
images/me+tux.jpg
gaby:~> tar tvf images/images-without-dir.tar
-rw-r--r-- gaby/gaby 42888 1999-06-30 20:52:25 me+tux.jpg
-rw-r--r-- gaby/gaby 7578 2000-01-26 12:58:46 nimf.jpg
gaby:~> tar xvf images/images-without-a-dir.tar
me+tux.jpg
nimf.jpg
gaby:~> ls *.jpg
me+tux.jpg nimf.jpg
|
This example also illustrates the difference between a tarred
directory and a bunch of tarred files. It is advisable to only
compress directories, so files don't get spread all over when
unpacking the tarball (which may be on another system, where you
may not know which files were already there and which are the ones
from the archive).
When a tape drive is connected to your machine and configured by
your system administrator, the file names ending in .tar are replaced with the tape device name, for
example:
tar cvf /dev/tape mail/
The directory mail and all the files
it contains are compressed into a file that is written on the tape
immediately. A content listing is displayed because we used the
verbose option.