Import multiple mysql databses in xampp


The script imports multiple mysql databases simultaneously in xampp's mysql . There are only two conditions to run this script.
1. All sql files should be saved in the directory /tmp/db
2. Name of sql file should be name of database.

If you have username and password of the mysql, enter your mysql username in place of text root and your password between the inverted commas. Do not remove the inverted commas. Put your password inside it.

If you have blank root password, when script runs, it prompts for mysql password, do not enter anything, just press return key.


#!/bin/bash
#import all sql files into xampp's mysql
#name of sql file should be name of mysql database.
################### variables ######################
bu_path=/tmp/db

##################### variables defined ############

IFS=$'\n'
lst=$(find "$bu_path" -maxdepth 1 -name '*.sql' -type f -exec basename {} \; | sed -e 's/\.sql$//g')
for i in $lst;
do
if [[ ! -z "`sudo /opt/lampp/bin/mysql -qfsBe "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='$i'"`" ]];
then
echo "..................................$i ALREADY EXISTS"
else
sudo /opt/lampp/bin/mysql -e "CREATE DATABASE \`$i\`;"
sudo /opt/lampp/bin/mysql "$i" < "$bu_path/$i.sql"
echo "Database $i imported successfully."
fi
done

Save the code in the text file and save as import.sh
Now give the executing permission to the script 
# chmod a+x /path/of/import.sh
Now run the script.
#  cd /path/to/the/directory

# ./import.sh

No comments:

Post a Comment