Skip to content

Instantly share code, notes, and snippets.

@justinkelly
Created December 30, 2011 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinkelly/1537596 to your computer and use it in GitHub Desktop.
Save justinkelly/1537596 to your computer and use it in GitHub Desktop.
Simple MySQL - db refresh script - delete tables and import .sql
#!/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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment