Created
June 17, 2021 11:40
-
-
Save maaami98/f3f5d9fecd3809932ccc6ef01e47d213 to your computer and use it in GitHub Desktop.
Mysql Database recovery with .ibd and .frm files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dbname="mydb_restore" | |
mysqlpass="" | |
mysqldata="/home/databases/my_db" | |
mysqlpath="/var/lib/mysql" | |
curl -s get.dbsake.net > /usr/local/bin/dbsake | |
chmod +x /usr/local/bin/dbsake | |
# Make a temp directory for data | |
mkdir -p /tmp/${dbname}/tmp | |
# Make a folder for frmTxt converts | |
mkdir -p /tmp/${dbname}/frmtxt/ | |
mysql -uroot ${mysqlpass} -e "CREATE DATABASE IF NOT EXISTS ${dbname};" | |
# Loop through files and do stuff | |
for file in ${mysqldata}/*.frm; | |
do | |
for ((i = 0; i < 1; i++)) | |
do | |
name=${file##*/} | |
base=${name%.txt} | |
drop=${base%.*}.txt | |
table=${base%.*} | |
sql=${base%.*}.sql | |
# Read the frm files and get the sql etc | |
dbsake frmdump $file > /tmp/${dbname}/frmtxt/$sql | |
echo /tmp/${dbname}/frmtxt/$sql | |
# Import the SQL - you need an empty db called museums in mysql for this | |
mysql -uroot ${mysqlpass} ${dbname} < /tmp/${dbname}/frmtxt/$sql | |
# Copy some files around so you don't break stuff | |
cp ${mysqldata}/$table.frm /tmp/${dbname}/tmp/ | |
cp ${mysqldata}/$table.ibd /tmp/${dbname}/tmp/ | |
# Discard the table space | |
mysql -uroot ${mysqlpass} ${dbname} -e "ALTER TABLE $table DISCARD TABLESPACE" | |
# Copy in the ibd file to relink | |
cp /tmp/${dbname}/tmp/$table.ibd ${mysqlpath}/${dbname}/ | |
# Check ownership | |
sudo chown mysql:mysql ${mysqlpath}/${dbname}/$table.ibd | |
# This will probably fail with big tables on low powered machines | |
mysql -uroot ${mysqlpass} ${dbname} -e "ALTER TABLE "$table" IMPORT TABLESPACE;" | |
# Throw in a sleep to stop it bugging out | |
sleep 0.1s | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment