Mon 21 Jan 2008
Repair RPM Database
Posted by jprentice under bash, centos, linux, redhat, rpm, scripting
No Comments
The RPM database has two functions,. First, it helps you manage software versions. Second, it eats itself. The “eat itself” functionality is second to none! I’ve never seen a function work as spectacularly as this one! As a result, I have to repair a corrupt RPM database quite often. So I wrote a quick and dirty shell script to do this. It backs up your current RPM database so that you can attempt further recovery if this scripts fails:
#!/bin/bash
DATE=`date +%Y%m%d%M%S`
echo "Before rebuilding the DB there are `rpm -qa|wc -l` rpms"
tar -cf /tmp/rpmdb.backup.$DATE.tar /var/lib/rpm
if [ $? = "0" ]
then
echo "Current RPM db has been backed up to /tmp/rpmdb.$DATE.tar"
else
echo "*ERROR* Problem backing up the DB, cannot continue"
exit 1
fi
rm -f /var/lib/rpm/__db*
if [ $? = "0" ]
then
echo "The db lock files were successfully removed"
else
echo "*ERROR* Problem removing the db lock files, cannot continue"
exit 1
fi
echo "Rebuilding the rpm db, this could take a few minutes..."
rpm --rebuilddb
if [ $? = "0" ]
then
echo "Rebuild appears to have been successful!"
echo "After rebuilding the DB there are `rpm -qa|wc -l` rpms"
else
echo "*ERROR* Problem doing the rpm rebuild"
exit 1
fi
exit 0



