How to recreate /var/lib/dpkg/status?
If you look at the purpose of /var as given in the Filesystem Hierarchy Standard, it says:
/var
contains variable data files. This includes spool directories and files, administrative and logging data, and transient and temporary files.
Note that "transient and temporary" files are just one of the things it contains. It also contains "spool directories and files" and "administrative and logging data". You deleted critical "administrative data".
It goes on to explain why /var
exists:
/var
is specified here in order to make it possible to mount/usr
read-only. Everything that once went into/usr
that is written to during system operation (as opposed to installation and software maintenance) must be in/var
.
That's the key thing about /var
: the data in it changes, unlike /usr
(which only changes when you add/remove/update software).
Further sections explain the various subdirectories of /var
; for example, /var/lib
(where the files you deleted used to live) holds "state information pertaining to an application or the system", defined as "data that programs modify while they run, and that pertains to one specific host."
You really shouldn't delete files without knowing what the specific file is for. With the files you deleted, unless you have a backup of these files, I think the only thing left to do is take a backup of /home
, /etc
etc. and reinstall. Until you do so, you'll be unable to use dpkg
(and APT, etc.). Other than that, the system should continue to function.
You can't "recreate" /var/lib/dpkg/status
in the sense of just running a command and the file magically appears. No. You need to use a backup of the file, and learn never going around deleting things of the /var/lib
directory:
sudo cp /var/lib/dpkg/status-old /var/lib/dpkg/status
This would give you the package status of the day before. Start praying it didn't broke something else.
Files located in /var
are very much system-critical. For example, /var/mail
or /var/spool/mail
contains the users' email; you would no more delete that than you would light a fire in your neighbor's mailbox. It's only files in certain subdirectories of /var
that contain files that are more or less transitory: log files in /var/log
, caches that can usually be recreated in /var/cache
, temporary files (which you should not delete while they're in use!) in /var/tmp
.
Data in /var/lib
can be quite critical. For example, MySQL is usually configured to store its databases in /var/lib/mysql
by default: if you erase that, you wipe your databases. Dpkg puts its own databases under /var/lib
as well; /var/lib/dpkg/status
is one.
/var/lib/dpkg/status
contains information about installed packages. If you've erased that, you should restore it from a backup. If your backup isn't fully up-to-date, check the logs of recent package manipulations under /var/log/apt
and in /var/log/dpkg.log
. You'll need to create that file before dpkg
will work.
/var/lib/dpkg/available
is built from data downloaded from the Internet. apt-get update
should rebuild it.
/var/lib/dpkg/info
contains files that ship with Debian packages. You can restore these files simply by reinstalling the packages. Of course, you will need a list of installed packages for that. If you've restored /var/lib/dpkg/status
, then you can extract the list of packages from there.
apt-get install --reinstall $(</var/lib/dpkg/status sed -n 's/^Package://p')
If you've lost /var/lib/dpkg/status
, then you may be able to recreate it by creating an empty file, then running apt-get install --reinstall
on the list of packages. One place where the list of packages is also saved is /var/lib/apt/extended_states
, at least if you've only ever used APT to install packages (as opposed to dpkg
directly) — use that file instead of /var/lib/dpkg/status
int the command above. If you've deleted that too, you can rebuild an approximate list of packages with $(cd /usr/share/doc && ls)
, because most packages create an entry in /usr/share/doc
. There are probably a few exceptions.
Do not ask for any assistance about package management on this system. Recovering from the deletion of system-critical files is not an exact science. If you can't restore from backups, you should install a new, clean system as soon as possible.