Last active
August 24, 2024 22:31
-
-
Save jasny/1608062 to your computer and use it in GitHub Desktop.
Split MySQL dump SQL file into one file per table or extract a single table
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 $0 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* |
there is a simple fix to those saying issue with more than a 100 files: -
- add
-n5
in bothcsplit
commands to allow upto 5 digits zero-spaced in the name. - change
mv table00 head
tomv table00000 head
.
Now this will handle upto 100000
tables starting from 00000
to 99999
.
Upgrade as required!
Bonus: add rm $FILE
after cat head $FILE foot
to remove the old file as well so as to save some space if there are too many files. My scenario had over 2500 tables with the base sql file at 130GB!
Bonus 2 hours later: for the really lazy ones, heres a link to my version of this script including output and timing
#https://gist.github.com/m-faraz/4e32cbae283e3a9454118cd1d281f8cf
Worked perfect for me on a 2.5gb file. Took less than 10 seconds.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
which csplit command? there are several :(