Team LiB
Previous Section Next Section

Hack 84. Recover from Debian Disaster

Restore your Debian system from even the most seemingly unrecoverable disaster.

We all make mistakes, but the worst accident on a Debian system (or any Debian-based system, such as Knoppix) is one that causes you to lose the contents of your /var/lib/dpkg directory. Once that directory is gone, you can no longer update or install software. These things happen even to the best of us, so here's a procedure to get your Debian system back where it was (or close), without losing any of your data or having to reinstall all of Debian from scratch.

OK, so you lost the /var/lib/dpkg directory. Heck, if you had /var on its own partition, you might have lost everything under the /var directory. If that's the case, you'll have a lot more work to do than restoring your Debian package database. Unfortunately, because every system differs, it's up to you to figure out what you need to restore directories other than those required by Debian. At minimum, you'll need to create the following directories to start your recovery process. Log in as root, and run these commands:

# mkdir -p /var/cache/apt/archives
# mkdir -p /var/cache/debconf
# mkdir -p /var/log
# mkdir -p /var/lib/dpkg/info
# mkdir -p /var/lib/dpkg/parts
# mkdir -p /var/lib/dpkg/alternatives
# mkdir -p /var/lib/dpkg/methods
# mkdir -p /var/lib/dpkg/updates

The next thing you need to do is find out what version of libc6 you have installed. Check the documentation for this information. For example, the first line of the README file in the documentation directory for libc6 often tells you the version:

# less /usr/share/doc/libc6/README
This directory contains the version 2.3.2 release of the GNU C Library.

Now fire up your favorite editor to create the /var/lib/dpkg/status file. Enter the following text in this file (substitute the version of your particular libc6 for the version in our example):

Package: libc6
Status: install ok installed
Version: 2.3.2

Save this file and exit the editor.

Be sure to press Enter after you enter the version number in this file. A new line must appear after the version number for this recovery method to work.


Now you need to run the following commands. You will get error messages and warnings, and the apt-get dist-upgrade command will not actually install any files, as it should. It will simply download packages. Don't worry. Just plow through these commands, ignoring the warnings and errors:

# dpkg --clear-avail
# apt-get update
# apt-get dist-upgrade

These commands cause your Debian system to download some critical packages. Change to the directory where these packages are installed and install them manually with the dpkg command. (Ironically, Debian might complain that dpkg isn't installed, but it works anyway.) Note the * at the end of the package names in the following commands. This is just a wildcard that saves you the trouble of specifying the full package filenames. Here are the commands to manually install the minimum required packages you'll need for installing packages in a more automated fashion:

# cd /var/cache/apt/archives
# dpkg -i libncurses*
# dpkg -i perl-base*
# dpkg -i libstdc++*
# dpkg -i dselect*
# dpkg -i dpkg*

Read the error messages you encounter and be prepared to be flexible if one or more steps give you trouble. I can almost guarantee there's a solution to every glitch, so keep at it. Remember that not all the error messages you see are important. Ignore the complaints about insufficient information regarding libc6. These error messages go away by the end of the procedure.


Now you're ready to get rid of that annoying message about insufficient information regarding libc6. Type in the following command:

# apt-get install -reinstall libc6

Finally, you can run the dist-upgrade command once again. This causes Debian to install all the base packages required for a bare-bones Debian distribution, because Debian doesn't know they already exist:

# apt-get dist-upgrade

At this point, you have only rebuilt the Debian database such that it "knows" about only the most fundamental packages you have installed. It doesn't know you have things such as KDE installed, or various productivity applications. These programs are on your system and they will work, but Debian doesn't know to upgrade them as new versions appear. The only way to get Debian to know about the packages you have installed is to install them again.

Don't worry. You won't have to reconfigure the packages you reinstall. But if you want your Debian package database to know they exist, you have to install them again. You can install your favorite programs any way you want, but this suggestion should make the process easier. Install the synaptic program:

# apt-get install synaptic

Then start up a graphical desktop, and run synaptic from a run dialog or terminal window. synaptic is a graphical, friendly package installer for Debian that sorts the available packages by category. You can browse through the various categories and checkmark all the programs you want on your system. Then have synaptic (re)install all of them in one big batch. Voilà. You have just recovered from one of the most disastrous types of damage one can inflict upon a Debian system.

    Team LiB
    Previous Section Next Section