MySQL script: drop all tables in a db and import a .sql file

Used to refresh each hour


MUSER="-- Insert Db username here --"
MPASS="-- Insert Db password here --"
MDB="-- Insert Db name here --"
# 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
    echo "Deleting $t table from $MDB database..."
    $MYSQL -u $MUSER -p $MPASS $MDB -e "drop table $t"

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

Justin Kelly

Justin Kelly

Web Developer, Business Analytics, Data Engineer specialising in PHP and Tableau

Based in Melbourne, Australia

Feel free to contact me or _justin_kelly

Leave a comment

Notify me of replies by email.