The tar backup program

Securing and Optimizing Linux: RedHat Edition -A Hands on Guide
Prev | Chapter 33. Why's and When's of Backup and Restore | Next

# 33.2. The tar backup program

The tar backup program is an archiving program designed to store and extract files from an archive file known as a tarfile. A tarfile may be made on a tape drive; however, it is also common to write a tarfile to a normal file.

A simple backup is when you decide to make a backup of files on your system you must choose a backup scheme before the beginning of your backup procedure. A lot of strategic backup schemes exist, and depend on the backup policies you want to use. In the following, We have shown you one backup scheme that you may use which takes advantage of the tar program's capabilities. This scheme is to first back up everything once, then back up everything that has been modified since the previous backup.

1. The first backup is called a full backup

2. The subsequent ones are incremental backups.

With six tapes you can make backups every day; The procedure is to use tape 1 for the first full backup Friday 1, and tapes 2 to 5 for the incremental backups Monday through Thursday. Then, you make a new full backup on tape 6 second Friday, and start doing incremental ones with tapes 2 to 5 again. It's important to keep tape 1 at its state until you've got a new full backup with tape 6.

In the following example below, we assume that we write the backup to a SCSI tape drive named `/dev/st0,` and we backup the home directory `/home` of our system. First of all, we must to move to the file system `/` partition. When creating an archive file, tar will strip leading `/` slash characters from file path names. This means that restored files may not end up in the same locations they were backed up from. Therefore, to solve the problem, the solution is to change to the `/` root directory before making all backups and restorations.

To move to the `/` root directory, use the command:

    [root@deep]# cd /

It is important to always start with a full backup; say on a Friday, for example:

Friday 1. use tape 1 for the first full backup.

    [root@deep] /# cd /
    [root@deep] /# tar cpf /dev/st0 --label=" full-backup created on `date '+%d-%B-%Y'`." \
         	 --directory / home

Monday. use tapes 2 for the incremental backups.

    [root@deep] /# cd /
    [root@deep] /# tar cpNf /dev/st0 --label=" full-backup created on `date '+%d-%B-%Y'`." \
         	 --directory / home

Tuesday. use tapes 3 for the incremental backups.

    [root@deep] /# cd /
    [root@deep] /# tar cpNf /dev/st0 --label=" full-backup created on `date '+%d-%B-%Y'`." \
         	 --directory / home

Wednesday. use tapes 4 for the incremental backups.

    [root@deep] /# cd /
    [root@deep] /# tar cpNf /dev/st0 --label=" full-backup created on `date '+%d-%B-%Y'`." \
         	 --directory / home

Thursday. use tapes 5 for the incremental backups.

    [root@deep] /# cd /
    [root@deep] /# tar cpNf /dev/st0 --label=" full-backup created on `date '+%d-%B-%Y'`." \
         	 --directory / home

Friday 2. use tape 6 for the new full backups.

    [root@deep] /# cd /
    [root@deep] /# tar  cpf /dev/st0 --label=" full-backup created on `date '+%d-%B-%Y'`." \
         	 --directory / home

Now, start doing incremental ones with tapes 2 to 5 again and so on.

- The `c` option specifies that an archive file is begin created.

- The `p` option preserves permissions; file protection information will be remembered.

- The `N` option does an incremental backup and only stores files newer than DATE.

- The `f` option states that the very next argument will be the name of the archive file or device being written.

Notice how a filename, which contains the current date, is derived, simply by enclosing the `date` command between two back-quote characters. A common naming convention is to add a `tar` suffix for non-compressed archives, and a `tar.gz` suffix for compressed ones. Since we aren't able to specify a filename for the backup set, the `--label` option can be used to write some information about the backup set into the archive file itself. Finally, only the files contained in the `/home` are written to the tape.

Because the tape drive is a character device, it is not possible to specify an actual file name. Therefore, the file name used as an argument to `tar` is simply the name of the device, `/dev/st0`, the first tape device. The `/dev/st0` device does not rewind after the backup set is written. Therefore it is possible to write multiple sets on one tape. You may also refer to the device as `/dev/st0`, in which case the tape is automatically rewound after the backup set is written. When working with tapes you can use the following commands to rewind and eject your tape:

    [root@deep] /# mt -f  /dev/st0 rewind
    [root@deep] /# mt -f  /dev/st0 offline

To reduce the space needed on a `tar` archive, the backups can be compressed with the `z` option of tar program. Unfortunately, using this option to compress backups can cause trouble. Due to the nature of how compression works, if a single bit in the compressed backup is wrong, all the rest of the compressed data will be lost. It's recommended to NOT using compression, the `z` option to make backups with the tar command.

If your backup doesn't fit on one tape, you'll need to use the `--multi-volume` `-M` option:

    [root@deep] /# cd /
    [root@deep] /# tar cMpf /dev/st0 /home

Prepare volume #2 for `/dev/st0` and hit return:

After you have made a backup, you should check that it is OK, using the `--compare` `-d` option as shown below:

    [root@deep] /# cd /
    [root@deep] /# tar dvf /dev/st0

To perform a backup of your entire system, use the following command:

    [root@deep] /# cd /
    [root@deep] /# tar  cpf /archive/full-backup-`date '+%d-%B-%Y'`.tar \
     --directory / --exclude=proc --exclude=mnt --exclude=archive \
     --exclude=cache --exclude=*/lost+found .

- The `--directory` option tells tar to first switch to the following directory path, the `/` directory in this example, prior to starting the backup.

- The `--exclude` options tells tar not to bother backing up the specified directories or files.

- The `.` character at the end of the command tells tar that it should back up everything in the current directory.

When backing up your file systems, do not include the `/proc` pseudo-file-system! The files in `/proc` are not actually files but are simply file-like links which describe and point to kernel data structures. Also, do not include the `/mnt`, `/archive`, and all `lost+found` directories.

Prev | Home | Next
What to backup | Up | Automating backups with tar