| Getting Started with Linux - Lesson 15 |
|---|
Backing up your files
Let's face it, computers aren't perfect. Linux is an "almost perfect"
operating system, but things do happen and data is sometimes lost. The best way to
avoid problems is to backup your files. Linux provides two key programs
to do this: 'tar' and 'gzip'
First we'll start with 'tar'. This program assembles various files
into one package, commonly called a "tarball". Let's say you have some
files - notes that you've taken during this course. You have:
- notes_1.txt
- notes_2.txt
- notes_3.txt
- notes_4.txt
- notes_5.txt
and you've placed them in a directory called /linux_course. You want
to back them up and keep them on a floppy, let's say. You would type the
following command to package them in a tarball.
tar -cvf linux_notes.tar notes*.txt
First, you have tar, the name of the program. Then you have the
options, c (--create) v (--verbose-show what files they are) (f--file -make a file - should always be the last option) Then you have the name of the
file you want to create ( linux_notes.tar) and the files you want
to backup (notes*.txt).
This presupposes that you may have other files in the directory that you
don't want to include. If you want to include ALL files in a directory, just
substitute notes*.txt for *.*.
If you've got good data storage capabilities (Jaz or Zip drives, a CD writer
or a tape backup drive), you might want to back up whole directories along with
their corresponding subdirectories. Then you would enter in the directory, let's
say /home/bob/ and issue the command:
tar -cvf bob_backup.tar *
With one asterisk, you will include directories and files without
extensions (my_file as opposed to my_file.txt). Be prepared to get a fairly
voluminous tarball.
This is the first step in the backup process. Now let's look at the second
step; the compression of these files.
Using 'gzip'
As we mentioned, 'tar' just assembles the files together into only one file.
There is no reduction in the size of these files (the tarball might even be
bigger!) Now we would have to do one more thing in order to reduce this file
into a more manageable size: use 'gzip'.
gzip is the preferred compression tool for Linux. To reduce the size
of your tar file, you would issue the following command:
gzip your_tar_file.tar
and the tar file would be compressed. You can also compress a regular file
using the same command, but gzip is used primarily with tarballs.
The result would be a file like this: your_tar_file.tar.gz
The two file extensions show us that the file is a tarball and it is
compressed with the 'gzip' format. You can now proceed to store this
as you see fit.
Putting it all together
'tar' has an option built into it to use 'gzip' to zip the file at the same
time you make the tarball. If you add z to the options,
and change the name of the file to create to a .gz extension, you
have the whole shebang in one step. Our previous example would be modified
to this:
tar -czvf bob_backup.tar.gz *
Remember f should always be the last option.
[Next]
|