Last active
February 11, 2016 21:56
-
-
Save dmgig/4f1130b346a91379f5aa to your computer and use it in GitHub Desktop.
MySQL: Import tables & data from sql and tab delimited dumps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# Note: This script disables foreign key checks and ignores all errors! | |
DUMPDIR='/tmp/mysql_dumps/' | |
USERNAME='root' | |
PASSWORD='MyP@55' | |
DATABASE='databaseName' | |
cd ${DUMPDIR} | |
# import table structures | |
for i in *.sql | |
do | |
# get table name from filename | |
TABLE=$(echo $i | cut -f 1 -d '.') | |
echo "creating table ${DATABASE}.${TABLE} from ${DUMPDIR}${i} ..." | |
mysql -u ${USERNAME} -p${PASSWORD} --init-command="SET SESSION FOREIGN_KEY_CHECKS=0;" ${DATABASE} < ${i} | |
done | |
# import data | |
for i in *.txt | |
do | |
# get table name from filename | |
TABLE=$(echo $i | cut -f 1 -d '.') | |
echo "importing data to ${DATABASE}.${TABLE} from ${DUMPDIR}${i}..." | |
mysqlimport --ignore --use-threads=24 -u ${USERNAME} -p${PASSWORD} ${DATABASE} ${DUMPDIR}${i} | |
#mysql -u${USERNAME} -p${PASSWORD} -e "SET SESSION FOREIGN_KEY_CHECKS=0; LOAD DATA INFILE '${DUMPDIR}${i}' IGNORE INTO TABLE ${TABLE};" ${DATABASE} < ${i} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment