Wednesday, January 16, 2013

Detect Orphaned MySQL Databases On Linux Running cPanel

This script relies on databases being in the following format:
username_database-name

...and located in:
/var/lib/mysql
cd /var/lib/mysql
for I in `ls -d */ | grep '_' | cut -d '_' -f 1 | uniq`
do
if [ ! -d "/home/$I" ]; then
    echo User $I doesnt exist - databases to be cleaned up!
fi
done
This should be used with caution and user existence manually verified but will give you a starting point.