OMG I’ve deleted a system directory! How do I recover it?


Posted: febbraio 20th, 2013 | Author: | Filed under: English, Guide, Howto | Tags: , , , , , , , , , , | No Comments »


Ok, a little introduction first. I was working on an uninstall target of a Makefile for a system library. For a very unlucky case (or maybe for my stupidity) I’ve deleted (fortunately not all) my /usr directory :)

And now?

As I said before not every files and binaries were deleted because I realized quickly what I was doing and I pressed ctrl-c to stop it before it was late.

Let’s start with the trick.

What I need?

This method consist of two different part. The aim of the first one is to fix all packages needed by “APT” to do its work. The aim of the second is to force the reinstall of all the packages which have files into the /usr directory.

For the first part you need:

  • a program (or a method) to find in which package is a particular binary (I used apt-file)
  • a program to download deb packages (if it’s still available you can use apt-get download, I used this command)
  • a program to extract the deb packages you have downloaded (I used dpkg)
  • cp to copy extracted files

For the second part you need (but it’s easy to have these programs with the first part):

  • a script that I have created which needs apt-get, cat, uniq, sed, mv, apt-file, (e)grep, echo and a working bash shell interpreter

First part: prepare the system to the script execution

As I told before, with this first part we will install all the necessary binaries needed to restore the system status. I’ll explain how I did but some part can be done using other methods (for example you can get packages downloading them from the debian website instead of using apt-get download).

  1. you have to start with an installation using:
    apt-get –reinstall install something
    if you see any problem during the install you’ll have to understand which is the missing executable file reading the output of the apt-get execution (it’s very easy to understand which is the missing program). For example we’ll reinstall the abiword program
  2. now you have to search where this executable file is contained:
    apt-file search abiword

    if you get a lot of output you’ll be able to reduce it searching for the exact executable path ( /usr/bin/abiword in this case) in the apt-file command above

  3. now you know which package you have to reinstall; download it with:
    apt-get download abiword

    it saves the deb package in the current directory

  4. now we’ll extract the package, first of all we have to make a directory (you have to do this only once):
    mkdir pkg
  5. and after extract the package:
    dpkg -x abiword_2.9.2 svn20120603-8_amd64.deb pkg/
  6. copy all files extracted which are in our corrupted directory:
    cp -R pkg/usr/* /usr/
  7. remove the content of the pkg folder:
    rm -r pkg/*
  8. now fix the installation of the package:
    apt-get -f install

This procedure can be done also when the following script will be executed, every time that you see an error during the installation of a package you must stop the script (if it isn’t stopped autonomously) and do the above procedure starting from the point 2, and after you have to restart the script (it has a “resume” capability so it will start from the next package after the last reinstalled).

Second part: reinstall all packages with files into your deleted directory

This part is almost done automagically by my script, but sometime you need to re-do the first part (starting from point 2 and omitting the point 4 if you already have the directory).

  1. you need a list of all packages installed in the system, you can use:
    dpkg --get-selections | awk '{print $1}' > /tmp/to_be_installed.txt
  2. download the script (see the end of this post for the code)
  3. make it executable:
    chmod +x reinstall_all.sh
  4. and start it (as superuser):
    ./reinstall_all.sh

It will take a lot of time, it will check if each package in your system has some file into /usr directory and if this condition is true then it’ll reinstall the package.

This is the script code:

#!/bin/bash

DAMAGED_DIR="/usr"

apt-get clean

cat /tmp/to_be_installed.txt > /tmp/tbi_act.txt

for pkg in `cat /tmp/already_installed.txt | sort | uniq`; do
sed -e "s/^$pkg$//" -e 's/$pkg //' /tmp/tbi_act.txt > /tmp/temp.txt; mv /tmp/temp.txt /tmp/tbi_act.txt
done

for pkg in `cat /tmp/tbi_act.txt`; do

RES=`apt-file search $pkg | egrep "^$pkg" | grep $DAMAGED_DIR`
if [[ $RES != "" ]]; then
echo $pkg >> /tmp/already_installed.txt
apt-get install -y --reinstall $pkg
fi

done

That’s all folks, let me know if it’ll be useful for someone :)



Leave a Reply

  • Articoli Recenti:

  • Tag

  • Categorie

  • Meta

  • Licenza Creative Commons

    Copyright © 2018, | ldlabs.org – Blog is proudly powered by WordPress All rights Reserved | Theme by Ryan McNair modified By Francesco Apollonio

    Smilla Magazine Il Bloggatore iwinuxfeed.altervista.org Feedelissimo - tecnologia WebShake - tecnologia