Tuesday, June 12, 2012

Fedora: Restoring Accidentally Deleted Files Using YUM...

While working on a bug, I needed to install some Python libraries from RHEL. Since I didn't want to go through a long process of downloading the source RPM, building it for Fedora, installing it and its dependencies, etc. I thought I'd be slick and just install the library using the setup.py file. Then I can just delete the library when I'm done.

Yeah, that ALWAYS works, right?

So while trying to delete the installed libraries, I managed to accidentally type:

rm -rf /usr/lib

before hitting Enter by accident. I stopped it, but not before deleting directories some might consider important. So, after a brief moment considering how it's going to really be a pain in the ass to reinstall my system, I figured I'd try restoring the deleted files and directories.

Solution

I first got a list of all of the RPMs that have files in the /usr/lib directory. I had to be specific since my laptop uses 64-bit packages, so I had to limit the search to only /usr/lib and ignore /usr/lib64. So I built out the list of packages using the following command line:

for file in $(rpm -qla|grep ^/usr/lib/); do echo $(rpm -qf $file); done | uniq | sort > /root/packages.ls

This part took a VERY long time since it's examining every file in every package, looking for those that have /usr/lib/ in their filename and collecting the package name. But when it's done this file contains the list of all packages I'm interested in re-installing in order to get my laptop back in order. So I then go through and do the re-installation using:

yum reinstall $(cat /root/packages.lst | uniq)

When this finished, all of the packages in question were re-installed on my system. Granted I had a few cases where I had to remove old packages that had some conflicts (not sure how I got into such a state, but that's a different blog post). But when all was said and done, the system was back in working order.