13.5. Backup Capabilities in Linux

13.5. Backup Capabilities in Linux

I will consider only those backup capabilities available in standard Linux distributions. These are simple copying and archiving commands that can be automated by scheduling them for execution in the task scheduler. If several commands are required for a particular backup task, these commands can be recorded into a script file, which then can be run as a scheduled task

13.5.1. Copying Files

The simplest way to make a backup copy is to use the cp command, which is used to copy files. However, file permissions must be preserved in the process. The following command saves the /home directory to the /mnt/bkupdisk device used especially for backup purposes:

 cp -a /home /mnt/bkupdisk 

In this case, the -a option is used, which is equivalent to specifying options -dpR . The functions of these options are as follows :

  • -d Never follow symbolic links. The directory is copied as is.

  • -p Preserve the specified attributes (mode, ownership, and time stamps).

  • -R Copy directories recursively to back up all subdirectories.

Thus, the previous command is identical to the following command:

 cp -dpR /home /mnt/bkupdisk 

This command backs up all files and subdirectories in the /home directory to the /mnt/bkupdisk device. Files from this directory that were modified after the backup was performed can be copied with the help of the same command, but specifying the -u option, as follows:

 cp -au /home /mnt/bkupdisk 

13.5.2. The tar Utility

Backing up one file at a time is inconvenient. It is much better to back up the entire directory as one file. Linux has a utility, called tar , which allows several files to be gathered into one. This process is called archiving; but you should not confuse this with compressing, which tar does not do. If several files totaling 2 MB are archived, the size of the resulting file will be slightly larger than 2 MB (the sum of all files plus the tar header).

The sense of collecting several files into one is that a single file is easier than several small files to control and compress using specialized programs.

Archiving file using the tar utility is performed by executing the following command:

 tar cf archive.tar directory 

The functions of the command's two parameters are the following:

  • c Specifies that an archive is to be created.

  • f Specifies the archive file and the device; by default, /dev/rmt0 is used.

Thus, the /home directory can be archived by executing the following command:

 tar cf backup.tar /home 

When the cf options are used at archiving, the paths of the archived files are preserved. Extracting the archived /home directory reconstructs the /home directory hierarchy in the current directory. For example, if extraction is performed into the /home directory, the path to the extracted /home directory will be /home/home. And if extraction is performed into the /etc directory, the path to the extracted /home directory will be /etc/home.

Thus, to restore files properly, extraction should be performed into the root directory. This is done by executing the following two commands:

 cd / tar xf /home/backup.tar 

Here, the first command changes the current directory to the root directory, and the second command extracts the archived backup files from the /home/backup.tar file.

The tar utility also uses the following options:

  • v Lists verbosely files being processed .

  • z Detects and properly processes gzip archives during extraction.

  • p Specifies to extract all protection information.

  • d Specifies to find differences between the archive and the file system.

  • t Lists the contents of the archive.

  • u Specifies to append only files newer than the archive copies.

  • N date Specifies to archive only files newer than the specified date.

  • P Specifies not to strip the leading / character from file names . In this case, regardless of the directory, from which the extraction command is executed, the files will be extracted into their initial directories.

The tar utility can be used to archive more than one directory at once. The following command archives the /home and /etc directories into one file:

 tar cf backup.tar /home /etc 

The contents of the archive can be viewed by executing the following command:

 tar tvf backup.tar 

This will list all directories and files contained in the archive, along with their ownership and permissions. An example of such a list is shown in Listing 13.1.

Listing 13.1: An example of listing archive contents
image from book
 drwx------ 504/504   0 2004-11-27 20:24:05 home/adr/ drwxr-xr-x 504/504   0 2004-11-27 20:24:05 home/adr/.kde/ drwxr-xr-x 504/504   0 2004-11-27 20:24:05 home/adr/.kde/share/ -rw-r--r-- 504/504   118 2004-11-27 20:24:05 home/adr/.gtkrc -rw-r--r-- 504/504   24 2004-11-27 20:24:05 home/adr/.bash_logout -rw-r--r-- 504/504   191 2004-11-27 20:24:05 home/adr/.bash_proflie -rw-r--r-- 504/504   124 2004-11-27 20:24:05 home/adr/.bashrc -rw-r--r-- 504/504   5 2004-11-27 20:24:05 home/adr/text -rw-r--r-- 504/504   2247 2004-11-27 20:24:05 home/adr/.emacs 
image from book
 

Note that there is no leading / character in the paths of the archive files in the last column. To properly restore files from this archive, the command to extract it has to be executed from the root directory; otherwise , the files will be extracted into the current directory.

13.5.3. The gzip Utility

Unlike the tar utility, the gzip utility compresses archived files. The resulting archives are of a much smaller size than the sum of the uncompressed files, meaning that they can be stored on a smaller medium.

Most often, data that have to be backed up are documents, whose size can be reduced by 90% by compressing them. Unlike programs, text files yield to compression extremely well.

Compressing, however, places a great workload on the processor, and it may take a long time to fully back up a large directory.

Because the size of a compressed archive is much smaller than that of a noncompressed archive, it takes less time to copy the archive over the network or to write it to removable media.

Before compressing files, you should place them into a tar archive. Then compress the obtained tar file as follows:

 gzip -degree file.tar 

The degree parameter specifies the degree, to which the file is to be compressed. The maximum compression degree is 9. The file.tar parameter specifies the tar archive file to be compressed. For practice, compress the tar archive of the /home directory, applying the maximum degree of compression. Execute the following command:

 gzip -9 backup.tar 

Now list the contents of the directory (using the ls command). Note that there is no more backup.tar file. It was replaced by the backup.tar.gz file, which is much smaller.

A compressed file is decompressed using the same tar command, but with the xfz option specified:

 cd / tar xfz /home/backup.tar.gz 

The first command changes the current directory to root. The second command first decompresses the gzip file and then extracts files from the tar archive. To simply decompress a gzip file without extracting files from the tar archive, execute the following command:

 gzip -d /home/backup.tar.gz 

This will replace the backup.tar.gz compressed gzip file with the backup.tar tar archive file.

Now you are ready to write a script to archive directories to be backed up in a tar archive and then compress this file into a gzip file. You can redirect the results of the tar command into the gzip command as follows:

 tar cvf - /home  gzip -9c > backup.tar.gz 

Here, the command part before the character creates a tar archive of the /home directory. The character pipes the results to the second command part, which then compresses the tar file and stores it as a gzip file.

Another Linux compressing utility is compress . It, however, does not compress as well as gzip ; moreover, it has been a subject of scandals and litigations concerning the license. Most administrators have switched to gzip , and I recommend that you start using this utility from the get-go.

13.5.4. The dump Utility

The utilities considered so far in this chapter are not specialized backup utilities. Their main function is to simply copy, archive, and compress files. The initial purpose of the dump utility was to back up the Ext2 file system.

To create a backup copy, at least the following parameters have to be specified:

  • - n This parameter takes values from 0 to 9 and specifies the backup level. The value of 0 means that a full backup is to be performed. Levels higher than 0 specify that only files newer than the last backup of a lower level are to be backed up.

  • - u This option specifies that the /etc/dumpdates file, which records backup dates, is to be updated after a successful backup.

  • -f file This option specifies the file or device, to which the backup is to be stored.

The simplest command to perform a full backup looks like the following:

 dump -0u -f /home/backup.bak 

To back up only files newer than the full backup, a level greater than 0 is specified, for example, as follows:

 dump -lu -f /home/backup.bak 

Files are restored by executing the restore command. Before executing it, however, make sure that you execute it from the directory that has to be restored.

The only required parameter for the restore command is -f file , which specifies the file to be restored. The -i option runs the command in the interactive mode, in which you can also specify the files to restore. The interactive mode is similar to the command line, in which you can browse the archive and execute the following commands:

  • help Displays brief help options for the available commands.

  • ls Displays the contents of the current directory.

  • pwd Displays the name of the current directory.

  • add directory Adds the directory specified in the directory parameter to the list of files to be extracted.

  • cd directory Changes the current directory to the one specified in the directory argument.

  • delete directory Deletes the directory specified in the directory argument from the list of files to be extracted.

  • extract Extracts all files on the extraction list.

  • quit Quits restore .



Hacker Linux Uncovered
Hacker Linux Uncovered
ISBN: 1931769508
EAN: 2147483647
Year: 2004
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net