% Linux Installation and Getting Started    -*- TeX -*-
% backups.tex
% Copyright (c) 1993 by Matt Welsh and Lars Wirzenius
%
% This file is freely redistributable, but you must preserve this copyright 
% notice on all copies, and it must be distributed only as part of "Linux 
% Installation and Getting Started". This file's use is covered by
% the copyright for the entire document, in the file "copyright.tex".

\section{Archiving and Compressing Files}

\index{files!archiving|(}
\index{archiving files|(}
\index{files!compressing|(}
\index{compressing files|(}
Before we can talk about backups, we need to introduce the tools
used to archive files and software on UNIX systems. 

\subsection{Using {\tt tar}}
\index{tar@{\tt tar}}
The {\tt tar} command is most often used to archive files.

The format of the {\tt tar} command is
\begin{tscreen}
tar \cparam{options} \cparam{file1} \cparam{file2} \ldots \cparam{fileN}
\end{tscreen}
where \cparam{options} is the list of commands and options for {\tt tar},
and \cparam{file1} through \cparam{fileN} is the list of files to 
add or extract from the archive.

For example, the command
\begin{tscreen}
\# {\em tar cvf backup.tar /etc}
\end{tscreen}
would pack all of the files in {\tt /etc} into the tar archive
{\tt backup.tar}. The first argument to {\tt tar}---``{\tt cvf}''---is
the {\tt tar} ``command''. ``{\tt c}'' tells {\tt tar} to
create a new archive file. The ``{\tt v}'' option forces {\tt tar}
into verbose mode---printing each filename as it is archived. The
``{\tt f}'' option tells {\tt tar} that the next 
argument---{\tt backup.tar}---is the name of the archive to create. The 
rest of the arguments to {\tt tar} are the file and directory names to
add to the archive.

The command
\begin{tscreen}
\# {\em tar xvf backup.tar}
\end{tscreen}
will extract the tar file {\tt backup.tar} in the current directory.
This can sometimes be dangerous---when extracting files from a tar file,
old files are overwritten. 

Furthermore, before extracting tar files
it is important to know where the files should be unpacked. For example,
let's say you archived the following files: {\tt /etc/hosts}, 
{\tt /etc/group}, and {\tt /etc/passwd}. If you use the command
\begin{tscreen}
\# {\em tar cvf backup.tar /etc/hosts /etc/group /etc/passwd}
\end{tscreen}
the directory name {\tt /etc/} is added to the beginning of 
each filename. In order to extract the files to the correct
location, you would need to use the following commands:
\begin{tscreen}
\# {\em cd /} \\
\# {\em tar xvf backup.tar}
\end{tscreen}
because files are extracted with the pathname saved in the archive
file. 

If, however, you archived the files with the command
\begin{tscreen}
\# {\em cd /etc} \\
\# {\em tar cvf hosts group passwd}
\end{tscreen}
the directory name is not saved in the archive file. Therefore, 
you would need to ``{\tt cd /etc}'' before extracting the files.
As you can see, how the tar file is created makes a large difference
in where you extract it. The command
\begin{tscreen}
\# {\em tar tvf backup.tar}
\end{tscreen}
may be used to display an ``index'' of the tar file before unpacking 
it. In this way you can see what directory the filenames in the archive
are stored relative to, and can extract the archive from the correct
location.

\subsection{{\tt gzip} and {\tt compress}}
\index{gzip@{\tt gzip}}
\index{compress@{\tt compress}}
Unlike archiving programs for MS-DOS, {\tt tar} does not automatically
compress files as it archives them. Therefore, if you are archiving
two 1-megabyte files, the resulting tar file will be two megabytes in
size. The {\tt gzip} command may be used to compress a file (the file
to compress need not be a tar file). The command
\begin{tscreen}
\# {\em gzip -9 backup.tar}
\end{tscreen}
will compress {\tt backup.tar} and leave you with {\tt backup.tar.gz},
the compressed version of the file. The {\tt -9} switch tells 
{\tt gzip} to use the highest compression factor.

The {\tt gunzip} command may be used to uncompress a gzipped file.
Equivalently, you may use ``{\tt gzip -d}''. 

{\tt gzip} is a relatively new tool in the UNIX community. For many 
years, the {\tt compress} command was used instead. However, because
of several factors\footnote{These factors include a software patent
dispute against the {\tt compress} algorithm and the fact that {\tt gzip}
is much more efficient than {\tt compress}.}, {\tt compress}
is being phased out. 

{\tt compress}ed files end in the extension {\tt .Z}. For example,
{\tt backup.tar.Z} is the compressed version of {\tt backup.tar}, while
{\tt backup.tar.gz} is the gzipped version\footnote{To add further
confusion, for some time the extension {\tt .z} (lowercase ``{\tt z}'') 
was used for gzipped files. The official {\tt gzip} extension is
now {\tt .gz}.}. The {\tt uncompress} command is used to expand a
{\tt compress}ed file; {\tt gunzip} knows how to handle {\tt compress}ed
files as well.

\subsection{Putting them together}
Therefore, to archive a group of files and compress the result,
you can use the commands:
\begin{tscreen}
\# {\em tar cvf backup.tar /etc} \\
\# {\em gzip -9 backup.tar}
\end{tscreen}
The result will be {\tt backup.tar.gz}. To unpack this file, use
the reverse set of commands:
\begin{tscreen}
\# {\em gunzip backup.tar.gz} \\
\# {\em tar xvf backup.tar}
\end{tscreen}
Of course always make sure that you are in the correct directory
before unpacking a tar file.

You can use some UNIX cleverness to do all of this on one command
line, as in the following:
\begin{tscreen}
\# {\em tar cvf - /etc $\mid$ gzip -9c $>$ backup.tar.gz}
\end{tscreen}
Here, we are sending the tar file to ``{\tt -}'', which stands for
{\tt tar}'s standard output. This is piped to {\tt gzip}, which 
compresses the incoming tar file, and the result is saved in 
{\tt backup.tar.gz}.
The {\tt -c} option to {\tt gzip} tells {\tt gzip} to send its output to
stdout, which is redirected to {\tt backup.tar.gz}.

A single command used to unpack this archive would be:
\begin{tscreen}
\# {\em gunzip -c backup.tar.gz $\mid$ tar xvf -}
\end{tscreen}
Again, {\tt gunzip} uncompresses the contents of {\tt backup.tar.gz}
and sends the resulting tar file to stdout. This is piped to 
{\tt tar}, which reads ``{\tt -}'', this time referring to {\tt tar}'s
standard input.

Happily, the {\tt tar} command also includes the {\tt z} option to 
automatically compress/uncompress files on the fly, using the
{\tt gzip} compression algorithm.

For example, the command
\begin{tscreen}
\# {\em tar cvfz backup.tar.gz /etc}
\end{tscreen}
is equivalent to
\begin{tscreen}
\# {\em tar cvf backup.tar /etc} \\
\# {\em gzip backup.tar}
\end{tscreen}
Just as the command
\begin{tscreen}
\# {\em tar xvfz backup.tar.Z} 
\end{tscreen}
may be used instead of
\begin{tscreen}
\# {\em uncompress backup.tar.Z}  \\
\# {\em tar xvf backup.tar}
\end{tscreen}

Refer to the man pages for {\tt tar} and {\tt gzip} for more
information.

\index{files!archiving|)}
\index{archiving files|)}
\index{files!compressing|)}
\index{compressing files|)}
