-
-
Save msawired/4fbc18d8ea7ce9c3324dffcb06ea71d7 to your computer and use it in GitHub Desktop.
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 http://blog.tty.nl/2011/12/28/splitting-a-database-dump | |
#### | |
if [ $# -lt 1 ] ; then | |
echo "USAGE: bash mysql_splitdump.sh DUMP_FILE [TABLE]" | |
exit | |
fi | |
if [ $# -ge 2 ] ; then | |
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 | |
csplit -s -ftable $1 "/-- Table structure for table/" {*} | |
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
Forked it to change output behavior (WIP). If a single table name is provided, current behavior creates multiple files: table0 (all tables before), table01 (the expected table), table02 (all tables after). (My) Expected behavior is to only get table02. This is particularly useful in case trying to extract a table from a large SQL document, so no time (and resources) is wasted for the unwanted tables.