Created
November 10, 2020 03:35
-
-
Save deevis/a2df610eca100f1a07016d099d15cbef to your computer and use it in GitHub Desktop.
Restore a rails DB from ibd files in a backup folder but missing schema definition...
This file contains hidden or 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
# Backstory - I had accidentally deleted /usr/local/mysql/data/mysql.ibd and | |
# as a result lost all schema information for all dbs... | |
# | |
# 1) move the db data folder someplace safe: | |
mv /usr/local/mysql/data/my_rails_db /backups/my_rails_db | |
# 2) recreate the db with the rails app and create the same (empty) schema | |
rake db:create | |
rake db:migrate | |
# 3) generate a drop_tablespaces.sql file | |
echo "SET FOREIGN_KEY_CHECKS=0;" > drop_tablespaces.sql | |
sudo ls /backups/my_rails_db/ | grep ibd | sed 's/\(.*\).ibd/alter table \1 discard tablespace; show warnings;/g' >> drop_tablespaces.sql | |
# 4) generate an import_tablespaces.sql file | |
sudo ls /backups/my_rails_db/ | grep ibd | sed 's/\(.*\).ibd/alter table \1 import tablespace; show warnings;/g' >> import_tablespaces.sql | |
# 5) login to mysql | |
mysql -uroot -p | |
# 6) run all commands in drop_tablespaces.sql file... | |
# (in mysql client) | |
SET FOREIGN_KEY_CHECKS=0; | |
alter table app_settings discard tablespace; show warnings; | |
alter table ar_internal_metadata discard tablespace; show warnings; | |
... | |
# 7) copy backed up tablespaces back into mysql data | |
# (from command line) | |
sudo cp -R /backups/my_rails_db/* /usr/local/mysql/data/my_rails_db | |
# 8) re-chown the copied files | |
# (from command line) | |
sudo chown -R _mysql:_mysql /usr/local/mysql/data/my_rails_db | |
# 9) run all commands in import_tablespaces.sql file... | |
# (in mysql client) | |
alter table app_settings import tablespace; show warnings; | |
alter table ar_internal_metadata import tablespace; show warnings; | |
... | |
# cheers :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment