Created
May 22, 2014 05:49
-
-
Save elw00d/29eeabf67dc6f74ba3c6 to your computer and use it in GitHub Desktop.
2 scripts: to split mysql dump file to multiple files (one per table) and to import all sql files in current directory to local mysql instance.
This file contains 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 | |
for FILE in ./*.sql; do | |
echo "Importing $FILE" | |
/cygdrive/c/Program\ Files/MySQL/MySQL\ Server\ 5.6/bin/mysql.exe -u root -padmin --database mydb < $FILE | |
done |
This file contains 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 | |
#### | |
# Split MySQL dump SQL file into one file per table | |
# based on https://gist.github.com/jasny/1608062 | |
#### | |
#adjust this to your case: | |
START="/-- Table structure for table/" | |
# or | |
#START="/DROP TABLE IF EXISTS/" | |
if [ $# -lt 1 ] || [[ $1 == "--help" ]] || [[ $1 == "-h" ]] ; then | |
echo "USAGE: extract all tables:" | |
echo " $0 DUMP_FILE" | |
echo "extract one table:" | |
echo " $0 DUMP_FILE [TABLE]" | |
exit | |
fi | |
if [ $# -ge 2 ] ; then | |
#extract one table $2 | |
csplit -s -ftable $1 "/-- Table structure for table/" "%-- Table structure for table \`$2\`%" "/-- Table structure for table/" "%40103 SET TIME_ZONE=@OLD_TIME_ZONE%1" | |
else | |
#extract all tables | |
csplit -s -ftable $1 "$START" {*} | |
fi | |
[ $? -eq 0 ] || exit | |
mv table00 head | |
FILE=`ls -1 table* | tail -n 1` | |
if [ $# -ge 2 ] ; then | |
mv $FILE foot | |
else | |
csplit -b '%d' -s -f$FILE $FILE "/40103 SET TIME_ZONE=@OLD_TIME_ZONE/" {*} | |
mv ${FILE}1 foot | |
fi | |
for FILE in `ls -1 table*`; do | |
NAME=`head -n1 $FILE | cut -d$'\x60' -f2` | |
cat head $FILE foot > "$NAME.sql" | |
done | |
rm head foot table* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on http://stackoverflow.com/questions/14640753/how-extract-single-table-data-from-fullbackup