Now that we know how to make backups, let's see how we can restore data from a backup. Remember from last week the difference between a relative and an absolute pathname, as this has an impact when you are restoring data. Relative pathnames are considered a good thing in a backup. Fortunately, the
tar utility that comes with your FreeBSD system strips the leading slash, so it will always use a relative pathname -- unless you specifically overrride this default by using the
It's always a good idea to do a listing of the data in an archive before you try to restore it, especially if you receive a
tar archive from someone else. You want to make sure that the listed files do not begin with "
/", as that indicates an absolute pathname. I'll check the first few lines in my backup:
tar tf backup.tar | head www/ www/mod_trigger/ www/mod_trigger/Makefile www/mod_trigger/distinfo www/mod_trigger/pkg-comment www/mod_trigger/pkg-descr www/mod_trigger/pkg-plist www/Mosaic/ www/Mosaic/files/ www/Mosaic/files/patch-ai
None of these files begin with a "
/", so I'll be able to restore this backup anywhere I would like. I'll practice a restore by making a directory I'll call
testing, and then I'll restore the entire backup to that directory:
mkdir testing cd testing tar xvf ~test/backup.tar
You'll note that I
cd'ed into the directory to contain the restored files, then told
tar to restore or extract the entire
backup.tar file using the
x switch. Once the restore was complete, I did a listing of the testing directory:
I then did a listing of that new
www directory and saw that I had successfully restored the entire
www directory structure, including all of its subdirectories and files.
It's also possible to just restore a specific file from the archive. Let's say I only need to restore one file from the
www/chimera directory. First, I'll need to know the name of the file, so I'll get a listing from
tar and use
grep to search for the files in the
tar tf backup.tar | grep chimera www/chimera/ www/chimera/files/ www/chimera/files/patch-aa www/chimera/scripts/ www/chimera/scripts/configure www/chimera/pkg-comment www/chimera/Makefile <snip>
I'd like to just restore the file
www/chimera/Makefile, and I'd like to restore it to the home directory of the user named
genisis. First, I'll
cd to the directory to which I want that file restored, and then I'll tell
tar just to restore that one file:
cd ~genisis tar xvf ~test/backup.tar www/chimera/Makefile
You'll note some interesting things if you try this at home. When I did a listing of
genisis' home directory, I didn't see a file called
Makefile, but I did see a directory called
www. This directory contained a subdirectory called
chimera, which contained a file called
Makefile. Remember, when you make an archive, you are including a directory structure, and when you restore from an archive, you recreate that directory structure.
You'll also note that the original ownership, permissions, and file creation time were also restored with that file:
ls -l ~genisis/www/chimera/Makefile -rw-r--r-- 1 test wheel 406 May 11 09:52 www/chimera/Makefile
That should get you started with using the
tar utility. In next week's article, I'll continue with some of the interesting options that can be used with
tar, and then I'll introduce the
Dru Lavigne is a network and systems administrator, IT instructor, author and international speaker. She has over a decade of experience administering and teaching Netware, Microsoft, Cisco, Checkpoint, SCO, Solaris, Linux, and BSD systems. A prolific author, she pens the popular FreeBSD Basics column for O'Reilly and is author of BSD Hacks and The Best of FreeBSD Basics.
Read more FreeBSD Basics columns.
Return to the BSD DevCenter.