[RM note: Script in this e-mail has been updated based on subsequent correspondence. Please note: Although, thanks to help from Remi Moyen, this script has finally been tested, you might want to be cautious and back up your HD contents, before running it.]


From: "Karsten M. Self" (kmself@ix.netcom.com)
To: LUGOD vox-tech mailing list (vox-tech@lists.lugod.org)
Cc: nicholas.petreley@linuxworld.com
Subject: Re: [vox-tech] Recovering Debian without /var/lib/dpkg...
Date: Wed, 22 Jan 2003 03:46:51 +0000
User-agent: Mutt/1.4i

On Tue, Jan 21, 2003 at 11:30:28AM -0800, R. Douglas Barbieri (doug@dooglio.net) wrote:
> Here is a good article about recovering a Debian system without having
> a backup of /var/lib/dpkg...
>
> http://www.linuxworld.com/2003/0113.petreley.html

Funny, I walked someone through that on irc.debian.org:#debian last week. This can be somewhat simplified from Nick's procedure. Rather than manually re-selecting packages in dselect, you would take advantage of the fact that Debian policy requires all packages to create an entry in /usr/share/doc, with the name of the package.

Procedure (in handy shell-script format). Note that I haven't tried this (suggestion: someone with a faster box than I should try installing Debian under UML and self-inflicting damage, then recovering, and telling me what I got wrong).

(Aside to Nick: this expands somewhat on my prior email.)

#!/bin/sh
# Recovering a Debian system without any backup of /var/lib/dpkg.
# ref: http://www.linuxworld.com/2003/0113.petreley.html
# (C) 2003, Karsten M. Self kmself@ix.netcom.com
# (C) 2003, Osamu Aoki osamu@debian.org, (fixup)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA

export PATH=/usr/sbin:/usr/bin:/sbin:/bin

# Let's be verbose....
set -v

# Test existence of any subdirs in /var. If they exist, exit, otherwise
# risk destroying an active system. If they don't exist, the remainder
# of this script is safe.

function create_new_dir () {
if [ ! -f $1 && ! -d $1 ]
then mkdir $1
else
echo "$1 exists, aborting" 1>&2
exit 1
fi
}

for d in /var/log /var/cache/{debconf,apt/archives} \
/var/lib/dpkg/{info,parts,alternatives,methods,updates}
do
create_new_dir $d
done

# Create a rudimentary status file
cat <<-EOF>/var/lib/dpkg/status
Package: libc6
Status: install ok installed
Version: 2.3.1-9
EOF

# Update package cache
apt-get update
apt-get dist-upgrade

# Register as installed crucial packages.
dpkg --clear-avail
apt-get update
apt-get dist-upgrade
cd /var/cache/apt/archives
dpkg -i libncurses*
dpkg -i perl-base*
dpkg -i libstdc++*
dpkg -i dselect*
dpkg -i dpkg*

# Reinstall libc to register it.
apt-get install --reinstall libc6

# Reinstall base packages.
apt-get dist-upgrade

# Generate list of installed packages to Re-register previously
# installed packages as installed, using /usr/share/doc as a
# fallback package registry.
# ref: dpkg -l $( ls /usr/share/doc | grep -v [A-Z] ) >/dev/null

dpkg --set-selections < $(
ls /usr/share/doc | \
grep -v [A-Z] | \
grep -v '^texmf$' | \
grep -v '^debian$' | \
awk '{print $1 " install"}'
)

# Re-register everything.
apt-get -u dselect-upgrade

## End of script ##


Peace.

--
Karsten M. Self kmself@ix.netcom.com
http://kmself.home.netcom.com/
What Part of "Gestalt" don't you understand?
Geek for hire: http://kmself.home.netcom.com/resume.html




Quoting from
http://qref.sourceforge.net/Debian/reference/ch-package.en.html#s-recover-status
:

6.3.4 Recover package selection data

If /var/lib/dpkg/status became broken for any reason, Debian system loses package selection data and suffers severely. Look for the old /var/lib/dpkg/status file at /var/lib/dpkg/status-old or /var/backups/dpkg.status.*.

Keeping /var/backups/ in a separate partition may be a good idea since this directory contains lots of important system data.

If old /var/lib/dpkg/status file is not available, you can still recover information from directories in /usr/share/doc/.

# ls /usr/share/doc | \
grep -v [A-Z] | \
grep -v '^texmf$' | \
grep -v '^debian$' | \
awk '{print $1 " install"}' | \
dpkg --set-selections
# dselect --expert # reinstall system, de-select as needed