Olivia to Petra Bash: Restoring MySQL Databases

So far we covered backing up Olivia, restoring our data and workday environment, and setting up our LAMP stack with Apache, MySQL and PHP. Today in our Olivia to Petra Bash series we’ll look at the bash script used to restore our MySQL databases.

We installed MariaDB 10 in our LAMP Stack Installation script, so all that remains is restoring our databases and perhaps creating some MySQL user accounts. Our databases sitting on a Cloud drive displayed below. The MySQL databases have a .sql filename extension.

Here’s our script logic.

  1. Establish our MySQL database connection properties
  2. The restore_db() function used to determine if the database exists and if not, create the database and import the database contents from the Cloud directory file
  3. This is where we loop through the files in our archives directory, extracting the file root name and full file name path to pass to our restore_db() function. There’s an echo statement of the file contents which we’ll remove when executing the script. More on that below.
  4. We have a “phpdude” MySQL user account that we create at the end of the script. We would obviously want to change the logic here if we needed to create multiple accounts.

I mentioned an echo statement in #3 where we can display the archive file elements while testing the script. Below is the output of the script with the echo statement enabled and bypassing the restore_db() function.

Here’s a link to the script tar.gz. You know by this point in the party that I am in no way responsible for any [bad] consequences from using the script. Good luck!

We’ll be winding up our Olivia to Petra Bash in the next post with a script that re-creates our Apache Web Sites!