|  About Me  |  Blogs  |  Photos  |  Publications  |  Resume  | 

Recovering accidentally deleted files under /usr

It’s been a very unfortunate week… Made a really REALLY stupid mistake today,
did a rm -rf /usr when I meant rm -rf usr
and didn’t realised for 5 seconds. Deleted /usr/bin, /usr/sbin, and a
big chunk of /usr/share directories.

Steps taken to recover

copy files without scp

copy bin and sbin from officemate. Don’t
have scp anymore so use nc instead:

my laptop:

$ nc -l -p 6666 | tar xv 

his laptop:

$ tar zv /usr/bin | nc 9.4.21.36 6666

See which files are missing

$ locate locate '/usr/*' | sort
to see files before deletion (locate reads from cache)
$ find /usr/ | sort
to see files after deletion.

wc -l and diff the outputs. *sniff*

Locate packages to reinstall

$ dpkg -S filename
find package that provides this file
$ dpkg -l | grep "^ii" | awk '{print $2;}'
list just the names of packages installed
$ apt-get --reinstall install package_name
force reinstall packages

To reinstall all the installed packages:

#!/usr/bin/perl

@packages = `dpkg -l | grep "^ii"`;

$install = "";for $i (@packages){    my ($a, $b, $c, $d) = split /\s/, $i;    $install = "$install $c";}

`apt-get --reinstall install $install`;

To reinstall the packages that provides the missing files:

#!/usr/bin/perl## Find and reinstall packages that provided for accidentally deleted system files#

@orig_files = `cat files/orig_files`; # generated by locate

%packages = ();

for $i (@orig_files){    if(not `ls $i`){        $package = `dpkg -S $i`;        if($package){            ($package, $x) = split /:/, $package;            $packages{"$package"} = 1;        }    }}

@uniq_packages = keys %packages;

print "@uniq_packages"

Run the script:

$ ./replace_missing.pl > unique;$ apt-get --reinstall install `cat uniq`;

Some package names in output file contains ‘,’ and some packages can’t
be downloaded. So removed them manually and tried again.

Will not work as root. Will use sudo instead.

Leave a Reply

You must be logged in to post a comment.