fredyounan
8/30/2015 - 1:28 PM

Simple bash script to drop a mysql database, create the database, and then import it using pv to display progress of the large import

Simple bash script to drop a mysql database, create the database, and then import it using pv to display progress of the large import

#!/bin/sh
#
# import_mysql_db - Drops the database and then imports it from sql file
#
# 2015-02-26, Kevin Elliott, kevin@catalyzed.io
#

if [ $# -ne 3 ];
then
  echo "syntax: import_mysql_db <database_user> <database_name> <sql_file>";
  exit 1;
fi;

database_user=$1
database_name=$2
sql_file=$3

if [ ! -f $sql_file ];
then
  echo "File $sql_file does not exist!"
  exit 1;
fi;

# Drop the database (it will ask to confirm)
mysqladmin -u$database_user drop $database_name
mysqladmin -u$database_user create $database_name

# Import the database (and use pv)
pv $sql_file | mysql -u$database_user $database_name