Backing Up, Archiving, and Compressing Files
Several methods are available for backing up, archiving and
compressing files on the Linux system. Which method is used
depends on which file or files are being backed up and if they
should be compressed.
In this section, backing up specific data on the system will be
shown. These methods only create an additional copy of your files
on the system but if there is a system failure, you would still
lose them. For long-term backup, these files should be copied to
another system or removable media and preferably stored in a
different physical location. How you back up your entire system
depends on your backup hardware and software and is beyond the
scope of this book.
Here is a brief list of pros and cons of archiving and
compression methods. This should give a good start to deciding
what method to use.
Method |
Pros |
Cons |
tar |
Universally available, maintains permissions
and other file information, handles multiple files very well |
Does not compress files |
cpio |
Good compatibility between systems |
No compression, clumsy syntax |
zip |
Format readable on nearly all platforms, good
space saving on compression |
More space can be saved with other
compression methods |
gzip |
Very good space saving on compression,
broadly adopted |
gzip utilities may not be available for some
platforms |
Table 13.5: Pros and Cons of Common Archiving and
Compression Methods
Using tar, the Tape Archiver
Originally written to manipulate magnetic tape devices, the tar
command, short for tape
archiver, can be used to create or read tar archives. Tar
archives, sometimes called tarballs, typically can be identified
by the .tar file extension, though this is not a requirement.
The -cvf optionsare
typically used to create tar files. The -c indicates that you are
creating an archive, -v tells tar that you would like verbose
output, e.g. each file name and location will be printed as it is
added to the archive, and the -f option tells tar that you are
working with a file rather than a magnetic tape drive.
When creating archives, the tar command requires at least two
arguments. The first argument is the archive file with which to
work. The second argument is the file or directory to add to the
archive. Additional arguments can also be given to indicate more
files to add to the archive.
$ tar -cvf notes.tar notes
notes/
notes/chapter3.txt
notes/chapter5.txt
notes/chapter2.txt
notes/chapter4.txt
notes/required_packages.txt
notes/chapter13.txt
notes/chapter1.txt
notes/chapter10.txt
To create a tar archive from a file or directory, it is best to
cd to the directory the source resides in. It is possible to
specify the source directory with an absolute path, but that
causes the files to be extracted to that path regardless of where
the file is extracted from.
NOTE: It may
be noticed in some places that the dash (-) is omitted
from the tar command options. Options for tar can be
specified with or without the dash. Specifying options
without the dash is an older method which is maintained in
tar for compatibility.
|
To extract files from a tar archive, the -x optionis used instead of -c. Files are extracted based on
the current working directory as long as an absolute path was not
used during archive creation. Only one argument is needed to
indicate where the tar archive can be found.
$ cd /tmp
$ tar -xvf /home/jemmons/notes.tar
notes/
notes/chapter3.txt
notes/chapter5.txt
notes/chapter2.txt
notes/chapter4.txt
notes/required_packages.txt
notes/chapter13.txt
notes/chapter1.txt
notes/chapter10.txt
Tar archives can be compressed using zip or
gzip to save space. If the archive is already created, it can be
compressed using the method described in the part of this section
on gzip, but if an archive needs to be created in a compressed
format, add the -z option to the tar command.
$ tar -cvzf
notes.tar.gz notes
notes/
notes/chapter3.txt
...
When using tar, the file names are arbitrary. It is best to
follow the common convention of .tar for tar archives and .tar.gz
or .tgz for compressed tar archives. If, for some reason, you are
unsure of the contents of a file, use the file command to check
it.
$ file
notes.tar.gz
notes.tar.gz: gzip compressed data, from Unix,
last modified: Sun Jan 17 13:07:47 2015
$ file notes.tar
notes.tar: POSIX tar
archive