3.9. Backing Up and Restoring with the tar Utilitytar is the most popular backup utility discussed in this chapter. Many of the files that you download from the Internet are in tar or compressed tar format. One limitation of tar to consider is that it has always had trouble with exceptionally long pathnames. Although it isn't typically used by itself for daily backup and recovery, GNU tar is often used by other open-source tools, such as Amanda (see Chapter 4).
3.9.1. The Syntax of tar When Backing UpThe basic tar command is as follows: $ tar [cx]vf device pattern Now let's look at some example commands. To create an archive of a directory called pattern, use the command: $ tar cvf device pattern To do the same thing but with a blocking factor of 20, use the command: $ tar cvbf 20 device pattern To do the same thing but have tar verify the data as it writes it (available only in GNU tar),[**] use the command:
$ gtar cvWbf 20 device pattern To create an archive of everything in the current directory starting with an "a", use the command: $ tar cvf device a*
3.9.2. The Options to the tar Commandtar has two great advantages. The first is the level of acceptance that it has received. The second is its short list of options; there really are not very many:
3.9.2.1. Listing files on standard inputMost versions of tar do not support listing the files to be archived on standard input, like cpio does. However, GNU tar added this functionality with a T flag that allows you to specify a file that contains a list of files to be backed up. If you want to specify the names of the files to be backed up via standard input, use GNU tar and specify - as the include file. This usually tells it to look at standard input instead of a named file. For example, suppose you wanted to run a find from /home/curtis and back up all the files that you find there: # cd /home/curtis ; find . -print |tar cvf /dev/rmt/0cbn T - This causes tar to see the result of the find operation as the list of files to be included. Some of the native versions of tar that support this feature are listed in Table 3-2.
3.9.3. Syntax of tar When RestoringA tar backup is very easy to read. Even if you used a blocking factor when you created the tar, you don't need it for the restore. tar automatically figures it out. (Did I hear you say "How beautiful..."?) To read a backup written with tar, enter: $ tar xvf device or: $ tar xvf device pattern The x flag tells it that you are extracting (restoring) from the tar file. The v, f, and device arguments work the same way as they do when making a backup. 3.9.3.1. Restoring selected parts of the archiveWhen restoring, you can specify the filename(s) that you want to restore by listing one or more pathnames after the device name. It is important to note, however, that the pathname must match the name in the tar archive exactly, or it is not restored. Unlike in cpio, wildcards are not supported in tar. However, if you specify a directory name, everything in that directory is restored. Remember, your specification must match the directory name exactly. Consider the following example. There is a subdirectory called home, and we create a tar archive of it, called file.tar. You can enter tar cvf file.tar home or tar cvf file.tar ./home. Watch how that affects what you must do to restore from it: $ tar cvf home.tar ./home a ./home/ 0K a ./home/myfile 0K a ./home/myfile.2 0K If it was backed up with ./home, it must be restored with ./home: $ tar xvf home.tar home tar: blocksize = 5 $ tar xvf home.tar ./home tar: blocksize = 5 x ./home, 0 bytes, 0 tape blocks x ./home/myfile, 0 bytes, 0 tape blocks x ./home/myfile.2, 0 bytes, 0 tape blocks This time it is backed up with home as the pattern: $ tar cvf home.tar home a home/ 0K a home/myfile 0K a home/myfile.2 0K Notice again that if it was backed up with home, it must be restored with home. The pattern of . /home does not work: $ tar xvf home.tar ./home tar: blocksize = 5 $ tar xvf home.tar home tar: blocksize = 5 x home, 0 bytes, 0 tape blocks x home/myfile, 0 bytes, 0 tape blocks x home/myfile.2, 0 bytes, 0 tape blocks If you don't know the name of the file you want to restore and you don't want to restore the entire archive, you can create a table of contents and look for the file there. First, make a table of contents of the archive: tar tf device > somefile If you do that with the archive in the preceding example, you will have a file that looks like this: home/ home/myfile home/myfile.2 If you knew you were looking for myfile, you could grep for that out of this file: # grep myfile somefile home/myfile home/myfile.2 You would then know that you should enter: $ tar xvf device home/myfile 3.9.3.2. Tricking tar into using wildcards during a restoreThere is a trick that works most of the time on tape and should work all of the time for tar files on disk. Issue two tar commands at once: $ tar xvf device \Qtar tf device | grep 'pattern'\Q If you are using this trick with a tape drive, make sure you use the rewind device, or it won't work! You also might want to add the sleep command to give the tape time to rewind: $ tar xvf device \Qtar tf device | grep 'pattern' ; sleep 60\Q 3.9.3.3. Changing ownership, permissions, and attributes during a restoreThe default actions of tar can vary from system to system, but most versions of tar support the following three options during a restore:
3.9.4. Some Other Neat Things About tartar has many options, and you should read the manpages to find them all. They can come in very handy. 3.9.4.1. Finding everything that's under the directorySometimes things underneath a directory are not what they seem. If you are creating "one last archive" of a directory before deleting it, you might want to follow any symbolic links that you come across. This is what the -h option is for. Make sure you've got lots of tape! 3.9.4.2. Using tar to move a directoryAs discussed earlier, cpio has a built-in command to move directories. The problem is that many people do not remember its syntax when the time comes. However, you also can use tar to move a directory. You do this by first cd'ing to one level above the directory you are going to move: $ cd old-dir ; cd .. You then use tar and a set of parentheses to create a subshell that "untars" the directory into its new location. (Note the use of the p flag to ensure that tar creates the new directory with the same permissions as the old one.) $ tar cf - old-dir | (cd new-dir ; cd .. ; tar xvpf - ) The - option for tar cf tells it to send its data to stdout. (We omit the v option to prevent writing the filenames to the display twice.) The - option on the tar xvf tells it to look at stdin for its data. Surrounding the cd old-dir ; tar xvf - with parentheses creates a subshell so that the directory old-dir is extracted into new-dir.
The syntax may seem a bit difficult, but it is very portable. It could be made a little shorter by saying: $ cd parent ; tar cf - old-dir | (cd new-parent ; tar xvpf - )
3.9.4.3. Restoring to an alternate locationIf you make your tar archives with relative pathnames, restoring to an alternate location is very easy. Simply change directories to something other than the original mount point (e.g., /home1), and start the restore from there. tar creates directories as needed.
Read the cpio section about relative pathnames and why they are important. |