MySQL script: drop all tables in a db and import a .sql file - Justin Kelly
#!/bin/bash
MUSER="-- Insert Db username here --"
MPASS="-- Insert Db password here --"
MDB="-- Insert Db name here --"
FILE="/home/user/path/to/your/import_file.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"
PS:
fixed version
which mysql may return ''
suggest drop database instead of delete all tables
This is the place where I store the words when I surfing. Share with u.
这里放着我在网上看到的文章,和你一起分享。
Friday, September 04, 2015
Thursday, September 03, 2015
replace - Linux - Replacing spaces in the file names - Stack Overflow
replace - Linux - Replacing spaces in the file names - Stack Overflow
for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done
Subscribe to:
Posts (Atom)