[Penguin Logo]

Linux for Newbies, pt. 23:
Disaster Recovery Tips

by Gene Wilburn

(The Computer Paper, June 2001. Copyright © Wilburn Communications Ltd. All rights reserved)

No matter which operating system you're using, at some point you'll be faced with an emergency recovery operation. Your MBR gets overwritten, your hard disk fails, an important file gets accidentally deleted, your video card gives one last flicker and dies. No matter the cause, you need some recovery procedures. Here are a few tips and techniques for some common mishaps.

The first item you need in your disaster recover kit is a floppy boot diskette. You probably created one when you first installed Linux, but do you know where it is? Take it for a test drive to make sure it still lets you boot into your system. Need a new one? Let's look at two options. (For any newcomers, this series is based on Red Hat 6.X Linux and some of the following procedures are Red Hat specific.)

Emergency Boot Diskettes

What makes the Red Hat boot disk you created for your system particularly valuable is that it contains any special drivers, such as SCSI drivers, that may be required to boot your system. If you need a new boot disk or would like to keep a second one on hand just in case, you can create a new boot disk by logging in as root and executing the command mkbootdisk. You need to know your kernel version and your floppy device (usually /dev/fd0). Use the uname command to display your kernel version as in this example session:

Insert the disk, wait a bit for the boot disk to be built, and now you have a working boot disk. Test it, then store it in a safe place.

A second alternative (it doesn't hurt to use both) is to create a Tom's Root Boot diskette. The tomsrtbt distribution may be one of the coolest items on the Internet: a complete Linux system on a single diskette. To create a tomsrtbt diskette, set your browser to www.toms.net/rb/ and download the latest version. To prepare an emergency boot disk, place the zipped tar file into a directory such as /usr/local/src and follow the install procedures:

Just follow the onscreen instructions. The package uses high compression to squeeze an amazing number of Linux utilities onto a single 1.7Mb floppy. You may go through a blank floppy or two before you find one that can stand up to the compression. The program does a careful check to make sure that the sectors are packed correctly and that they can be read.

After you've created the diskette, read the tomsrtbt.FAQ file in the install directory to get a full appreciation of the product and learn some ways to customize it. The default disk contains the following programs, drivers and kernel:

Impressive, and even better, tomsrtbt is not distribution specific. In fact you can even use it to perform certain rescue operations on Windows 9X.

To The Rescue

Let's look at a common mishap: in this scenario you have a dual-boot Linux/Windows computer and you happily use LILO in your master boot record partition (MBR) to boot to one OS or the other. You decide to upgrade or re-install your Windows setup. Zap! There goes the MBR (Windows ALWAYS zaps the MBR when it installs even though it's unnecessary and in decidedly poor form).

Now you're in a pickle. Your Linux partition is still fine, but it's inaccessible. How do you get back into Linux in order to re-run lilo so that it can rewrite the multi-boot loader into the MBR? Why with your boot disk, of course.

With the Red Hat boot disk, it's a piece of cake. Just insert the floppy, boot, log in as root and type lilo at the command line. Everything's back to normal.

You can also re-run lilo using tomsrtbt, but the procedure is slightly different. Because it's not a boot disk for your setup, you must mount the Linux partition(s) and chroot to your root filesystem before running LILO. To perform this procedure with tomsrtbt, pop in the emergency boot disk, reboot, log in as root, then remove the floppy disk. Here is a sample LILO recovery session in which the Linux root system is /dev/hda4:

Prior to release 6.2 Red Hat offered a rescue.img that could be used to create a separate "rescue disk" used in conjunction with the boot disk. When the LILO: prompt appeared, you would type in linux rescue and follow the instructions. From 6.2 onwards Red Hat has moved the rescue function over to its installation CD. You boot with the CD then type "rescue" at the first prompt.

This method doesn't work well with old BIOSes that cannot boot from CD-ROM. You first have to boot with the installation diskette, then type "rescue" then insert the CD-ROM. It's often easier to perform these rescue operations with tomsrtbt instead.

To The Rescue, Continued

Here's another situation: you've accidentally made a typo in /etc/fstab when you reconfigured your system. You meant to write /dev/hda4 as your boot drive but accidentally entered /dev/hda44. Your system is now unable to boot.

Pop in a tomsrtbt floppy, reboot, log in as root, remove the floppy, and proceed as follows:

Another common mishap that affects computers is being shutdown incorrectly, say when the lights go out during a power outage and your hard disk suffers some corruption as a consequence.

You can use tomsrtbt to try to fix a corrupted partition (e.g. /dev/hda2). Boot with the tomsrtbt floppy and work on the affected partition using e2fsk:

For a badly damaged partition, this command may need to be repeated several times.

If a partition can't be fully repaired, you can sometimes grab off an important file or two anyway, writing them to floppy. When tomsrtbt boots it loads itself completely into memory, freeing up the floppy drive for other uses. Here's an example session showing how you can use a tomsrtbt instance to format a floppy in Linux EXT2 format, mount the newly formatted floppy, mount a HD partition, then copy some important files to the floppy:

The preceding session used the fdformat, mke2fs, mount, cd and cp commands, all of which are included on tomsrtbt. In this session we rescued our configuration files from /etc, our DNS files from /var/named and an important user-created file.

Odd as it may seem, tomsrtbt can be used a rescue/salvage disk for Windows. Here's a sample session to rescue some files from a Windows system that no longer boots (assuming you've got some spare DOS-formatted diskettes on hand). You can also edit the autoexec.bat file while you're there:

By now you should be getting the message about tomsrtbt. Tuck one into your briefcase, knapsack or shirt pocket and carry it around as faithfully as your cell phone. You never know when you might need it. And imagine your friends' astonishment when you help them recover a Windows file using "pocket Linux".

Backup is your Friend

While it's good to be prepared for rescue operations, there are times when the damage is beyond repair. If you accidentally delete a file in Linux, for instance, kiss it goodbye. There are some convoluted methods of trying to recover deleted files, but they're rarely successful.

Worse, hard disks tend to fail all at once, with no warning. No matter your operating system of choice, there's no substitute for good backup.

Good backup is your best friend. It's not very glamorous or exciting and it takes a little effort, but you only need to lose your work once before you become one of the converted.

At the very least, you can back up certain files as you create them by copying them to floppy. This works for small files such as configuration files and other text files. Most Linux distributions include a set of tools called mtools. The mtools can be used to work with Windows floppies, letting you easily move small amounts of data between Linux and Windows. Mtools can create DOS/Windows floppy disks and to manage their contents.

If you want to make your text files Windows friendly, so that you can continue to work on them in Windows if you suffer any downtime, you can even invoke a line-ending flag that will convert the Linux LF endings to the CR/LF endings of the DOS world, and vice-versa.

Here's a sample session in which I format a DOS floppy, copy one set of text files with Linux line endings to floppy, then copy one text file with translated line endings to floppy. The session starts with inserting a floppy disk (blank or otherwise) that you can reformat.

To reverse the process, copying a file from DOS floppy to Linux and translating the line endings, insert the floppy disk and follow a pattern such as this:

Notice the dollar-sign prompts ($), indicating that I was able to do these diskette formatting and copying exercises while logged in on a non-root account. To allow any users to access floppies via the mtools kit, you must first assign them to group floppy and give the group read-write-execute permission on /dev/fd0.

Serious Backup

More robust, regular backup is usually done via a tape drive or by installing a CD-RW device and burning backups to CD-R or CD-RW. If you can afford it, CD-RW makes an inexpensive and sturdy backup solution. Tape has proven its reliability over time and usually holds more data than CD-RW.

With a tape device you ordinarily use a program like tar, cpio, or dump to back up data. With CD-RW you can use the copy command (cp). In both cases it's best to automate backup so you don't forget to do it. In the event of total disk failure, the main filesystems you want to recover to a new HD are /etc, /home, and /usr/local (if you've been installing software from source). You will also want /var/named if you're running a local DNS server. The rest you can rebuild from scratch via your distribution CD and Internet updates.

Disasters are not common and Linux is one of the most stable operating systems you could place on a home system, but all systems eventually fail and knowing some emergency repair procedures can lessen the impact. Being proactive about disaster possibilities is the first step towards being a professional system administrator.

Gene Wilburn (gene@wilburn.ca) is a Toronto-based IT specialist, musician and writer who operates a small farm of Linux servers.