MySQL automatic restore script

We had a .sql export from a MySQL databaseserver from all the databases. In our case the filenames were made up like:

mysqldump-hostname-user_database-D5-22H.sql

So I wrote a little bash script to recreate all the databases and import it’s data:

#!/bin/bash

folder=/var/sqlbackup/

for file in `ls $folder/mysqldump-*.sql`
do
echo $file
IFS='-' read -r -a array <<< "$file"
echo ${array[2]}
echo "create database ${array[2]}" | mysql -u da_admin -ppassword
mysql -u da_admin -pnAcQKhIK ${array[2]} < $file

done

After a little while all databases were full with content again 🙂

Author: Maarten Keizer

Hosting guru. WordPress addict. n00b programmer.

Leave a Reply

Your email address will not be published. Required fields are marked *