Repairing the MythTV Database

I've been pretty happy with MythDora over KnoppMyth overall (it's actively maintained, for one thing), but like most MythTV setups, the MySQL database tends to crash with regular use, despite running the DB Optimize Script regularly.

After having to type (and re-type) a series of terminal commands, I finally just wrote a shell script to do everything automatically:


service mythbackend stop
service mysqld stop
cd /var/lib/mysql/mythconverg
myisamchk *.MYI > crash.log 2>&1

grep corrupt crash.log | grep table | \
 sed -e 's/ is corrupted//' | \
 sed -e 's/MyISAM-table //' | \
 sed -e "s/'//g" | \
 sed -e 's/^/myisamchk -r /' >

grep fixed crash.log | \
 sed -e 's/ is usable but should be fixed//' | \
 sed -e 's/MyISAM-table //' | \
 sed -e "s/'//g" | \
 sed -e 's/^/myisamchk -r /' >>

sh > repair.log 2>&1
myisamchk -c *.MYI > after.log 2>&1

echo "**** After repair ****"
grep -i fix after.log
grep -i corrupt after.log
mv *.log /tmp
mv *.sh /tmp

echo "**** Repair done ****"
echo "Check /tmp/after.log and if ok, run:"
echo "  service mysqld start"
echo "  service mythbackend start"

Ok, not quite everything, since the repair results should be checked (a simple grep for "fix" and "corrupt" suffices) before restarting the mythv services, but recovery is much easier now.