Simple MySQL db refresh script

If you need to drop the contents of a MySQL database and reimport base data alot (such as a webapp demo or development site) below is a very simple script to make your life easier

Just edit the configs, create the import.sql add it to your cron jobsĀ 

#!/bin/bash

MUSER="MYSQL_USER"
MPASS="MYSQL_PASSWORD"
MDB="MYSQL_DB"
FILE="/path/to/sql/to/import.sql"

# Detect paths
MYSQL=$(which mysql)
AWK=$(which awk)
GREP=$(which grep)

TABLES=$($MYSQL -u $MUSER -p$MPASS $MDB -e 'show tables' | $AWK '{ print $1}' | $GREP -v '^Tables' )

for t in $TABLES
do
        echo "Deleting $t table from $MDB database..."
        $MYSQL -u $MUSER -p$MPASS $MDB -e "drop table $t"
done

cat $FILE | mysql -u $MUSER -p$MPASS $MDB
echo "Sql imported"

Refer gist for comments https://gist.github.com/justinkelly/1537596

Justin Kelly

Justin Kelly

Web Developer, Business Analytics, Data Engineer specialising in PHP, ReactJS, Tableau, AWS RedShift and Matillion ETL

Based in Melbourne, Australia

Feel free to contact me justin@kelly.org.au or _justin_kelly

Leave a comment

Name Notify me of replies by email.