Advanced: Reinstall (Almost) All Packages Without Reinstalling All Of Ubuntu



What Does This Procedure Do?


This procedure allows an advanced user or system administrator to perform a mostly unattended, automatic, batch-mode reinstallation of almost all packages that are already installed on a system.



When Should This Procedure Be Used?


This procedure was designed to assist in the recovery of a damaged or compromised system.


For example, suppose a system has incurred damage to its system libraries because of a slowly failing hard drive. Presuming the filesystems (partitions, volumes, etc.) have been imaged and transferred to a good, identical replacement drive, one may be able to use this procedure (described below) to replace corrupt packages with good ones.


Or, suppose an important system has suffered a security breach, and so the integrity of the installed packages can no longer be guaranteed. One may be able to use this procedure to make sure that most of the installed packages came from a trusted source, such as an official Ubuntu repository.



When Should This Procedure NOT Be Used?


Because the procedure described below is designed to replace almost every installed package on the system with a fresh copy, there is a chance that customized scripts and configuration files will be overwritten. Thus, users and system administrators should consider the effectiveness of other, more traditional system recovery options before using this procedure.


This procedure is dependent upon, at the absolute minimum, a bootable system with the following:


1. A working network connection.

2. Functional package management software (APT and Dpkg).

3. An intact installed packages database.

If any of the above conditions cannot be met, then do not use this procedure.


This procedure presumes a good level of familiarity with all of the following:


1. Basic system backup/restore procedures (in case things do not work as expected).

2. Package management with APT and Dpkg.

3. Command execution using the Bash shell.

4. The location of important system libraries and log files.

If you are not confident in your ability to perform mid-level to advanced debugging of your Linux system, then do not use this procedure.




On What Distributions Has This Procedure Been Tested?


This procedure has been tested on:


1. Ubuntu, Gutsy Gibbon, 7.10.

2. Xubuntu, Gutsy Gibbon, 7.10.

3. Xubuntu with KDE added, Gutsy Gibbon, 7.10.



Background:


I came upon a request for assistance in reinstalling all of the packages on a system that had suffered some filesystem damage that was not corrected by Ext3 and/or ReiserFS journalling.


After searching far-and-wide for info on this topic, I managed to stumble upon a very simple Bash shell script, posted by Tod Detre to the debianHELP.org forums, that tackles a similar issue.


Important: Do not equate simplicity with safety. This procedure could (theoretically) reset important parts of your system configuration, requiring you to redo earlier work.


I took the script I found and modified it slightly so that it is a bit less "draconian" with regard to package reinstallation. The original script completely purged and reinstalled each package; this could result in a loss of carefully-tweaked configuration files. (Which is not necessarily a Bad Thing, if that is what you want.) The modified script, presented below, uses the APT


--reinstall


method instead, which should in most cases maintain each package's current configuration files.


Like the original, the modified script uses Dpkg and APT to iterate through and reinstall all packages that are listed as already installed by the package management database.


Because the script reinstalls packages one at a time, it isn't fast, but it does get the job done. On an Intel 2.4GHz Q6600 with 2GB of RAM, a fast SATA-II drive, and using Xubuntu Gutsy (32-bit) with KDE added, the script will reinstall anywhere from 350 to 800 packages per hour, depending on the size of the package cache and other factors.


Each package is installed individually; this gets around "package X pre-depends on package Y" errors.


Also, the script skips certain packages, namely:


APT- and Dpkg- related packages:
(You don't want the package manager and related software to change mid-stream.)

MySQL- and MythTV- related packages:
(These packages don't seem to take too well to non-interactive configuration.)


Feel free to add your own additional exception patterns to the


egrep


command embedded within the script.



Usage:


To use the script, follow the steps below:


Step 0: Backup all of your data. (This is a prerequisite for any procedure that performs global changes to a system.)


Step 1: Open a Bash command shell, and clear out the downloaded packages cache with a


sudo apt-get clean


command.


Step 2: Using Synaptic (or your preferred package management tool), download, but do not install, fresh copies of all packages currently installed on the system.


Note: Have your original installation CD-ROM handy; the package management utilities may ask for it.

Important: Make sure you update the package repository list(s) before beginning the download.


Step 3: Open a plain text editor, and copy/paste the following code:


Code:
#!/bin/bash

for pkg in `dpkg --get-selections | awk '{print $1}' | egrep -v '(dpkg|apt|mysql|mythtv)'` ; do apt-get -y --force-yes install --reinstall $pkg ; done


Important: The commands "for pkg ... done" should all be on the same line.


Step 4: Save the file with an appropriate name, such as:


reinstall_all.sh


Step 5: Open a Bash shell, change the script's owner to "root", and make it executable:


sudo chown root:root reinstall_all.sh
sudo chmod 755 reinstall_all.sh


Step 6: Execute the script:


sudo ./reinstall_all.sh


Important: Once the script completes:


If you are using proprietary drivers for an nVIDIA or ATi video card, or are running a vendor-provided (as opposed to Ubuntu-provided) copy of VMware, you will probably need to re-run the appropriate driver configuration utility in order to ensure the driver integrates properly with the installed kernel.



Attributions:


The above package reinstallation script is based upon the solution presented by Tod Detre on debianHELP.org at 2007-09-20 17:00: