UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 20.3 How to Make Backups with a Local Tape Drive Chapter 20
Backing Up Files
Next: 20.5 Using tar to a Remote Tape Drive
 

20.4 Restoring Files from Tape with tar

When you create an archive, there are several ways to specify the directory. If the directory is under the current directory, you could type:

% tar c project

A similar way to specify the same directory is:

% tar c ./project

If you are currently in the directory you want archived, you can type:

% tar c .

Another way to archive the current directory is to type:

% tar c *

Here, the shell expands the * (asterisk) to the files in the current directory. However, it does not match files starting with a . (dot), which is why the previous technique is preferred.

This causes a problem when restoring a directory from a tar archive. You may not know if an archive was created using . or the directory name.

I always check the names of the files before restoring an archive:

% tar t

If the archive loads the files into the current directory, I create a new directory, change to it, and extract the files.

If the archive restores the directory by name, then I restore the files into the current directory.

20.4.1 Restoring a Few Files

If you want to restore a single file, get the pathname of the file as tar knows it, using the t flag. You must specify the exact filename, because filename and ./filename are not the same. You can combine these two steps into one command by using [this may run very slowly-JP ]:

% tar xvf /dev/rst0 `tar tf /dev/rst0 | grep filename`

Whenever you use tar to restore a directory, you must always specify some filename. If none is specified, no files are restored.

There is still the problem of restoring a directory whose pathname starts with / (slash). Because tar restores a file to the pathname specified in the archive, you cannot change where the file will be restored. The danger is that either you may overwrite some existing files or you will not be able to restore the files because you don't have permission.

You can ask the system administrator to rename a directory and temporarily create a symbolic link pointing to a directory where you can restore the files. Other solutions exist, including editing the tar archive and creating a new directory structure with a C program executing the chroot(2) system call. Another solution is to use the version from the Free Software Foundation (52.9) that allows you to remap pathnames starting with / (slash). It also allows you to create archives that are too large for a single tape, incremental archives, and a dozen other advantages. This freely available version of tar is also called GNU tar (19.6). (It's on the disc.)

But the best solution is to never create an archive of a directory that starts with / (slash) or ~ (tilde) (14.11).

20.4.2 Remote Restoring

To restore a directory from a remote host, use the following command:

rsh 
% rsh -n host dd if=/dev/rst0 bs=20b | tar xvBfb - 20 files

Because of its nature, it is difficult to read fixed-size blocks over a network. This is why tar uses the B flag to force it to read from the pipe until a block is completely filled. [Some versions of tar, like the one from GNU (52.9) on the CD-ROM, handle remote drives automatically. -JIK ]

- BB


Previous: 20.3 How to Make Backups with a Local Tape Drive UNIX Power ToolsNext: 20.5 Using tar to a Remote Tape Drive
20.3 How to Make Backups with a Local Tape Drive Book Index20.5 Using tar to a Remote Tape Drive

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System