MySQL Repair / Recovery
über mysqldump einen Export erzeugen, dann die DB-Files löschen und den Dump wieder importieren.
#!/bin/bash
DUMPFILE="/tmp/mysql.sql"
if [ "$(id -u)" != "0" ]; then
echo "ERROR: Only root can do that!"
exit 1
else
echo -en "INFO: This will export all MySQL-Databases, erase the DB-Files and re-import the dump.\nYou will be asked for the root-password of the database.\n\nDo you want to continue? [Y|n] "
read CONTINUE
if [ -z "$CONTINUE" -o "$CONTINUE" == "Y" -o "$CONTINUE" == "y" ]; then
echo -en "\n"
else
exit 1
fi
fi
# Daten exportieren
mysqldump -A -f -v -u root -p > $DUMPFILE
# War der Exsport erfolgreich?
if [ $? -eq 0 -a -s $DUMPFILE ]; then
service mysqld stop
sleep 3
cd /var/lib/
if [ "`pidof mysqld`" = "" ]; then
if [ -d mysql ]; then
if [ -f mysql.tar.gz ]; then
rm -vf mysql.tar.gz
fi
tar -cvzf mysql.tar.gz mysql/*
if [ -f mysql.tar.gz ]; then
cd mysql
# Loesche alle Verzeichnisse ausser "mysql"
ls -d */ | grep -v mysql | sed s@/@@g | xargs -I {} rm -rfv {}
# Loesche alle InnoDB-Files, diese werden beim naechsten Service-Start neu erzeugt
rm -fv ib*
service mysqld start
# Daten importieren
echo -e "\nINFO: Importing dump..."
mysql -f -u root -p < $DUMPFILE
service mysqld restart
echo -e "\nINFO: Done."
exit 0
else
echo -e "\nERROR: TAR (/var/lib/mysql.tar.gz) failed?!"
exit 1
fi
else
echo -e "\nWARNING: MySQL-Directory (/var/lib/mysql) not found."
exit 2
fi
else
echo -e "\nERROR: Could not stop mysqld?!"
exit 1
fi
else
echo -e "\n\nERROR: DUMP ($DUMPFILE) failed?!"
exit 1
fi