Last active
April 1, 2025 12:54
-
-
Save m-faraz/4e32cbae283e3a9454118cd1d281f8cf to your computer and use it in GitHub Desktop.
Split MySQL dump SQL file into one file per table
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 | |
#### | |
# Split MySQL dump SQL file into one file per table | |
# based on http://blog.tty.nl/2011/12/28/splitting-a-database-dump | |
#### | |
#### | |
# This version modified from https://gist.github.com/jasny/1608062 | |
#### | |
if [ $# -lt 1 ] ; then | |
echo "USAGE $0 DUMP_FILE [TABLE]" | |
exit | |
fi | |
echo -n "Starting to split the dump @ " | |
date | |
if [ $# -ge 2 ] ; then | |
csplit -n5 -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 -n5 -s -ftable $1 "/-- Table structure for table/" {*} | |
fi | |
[ $? -eq 0 ] || exit | |
echo -n "Done with splitting files @ " | |
date | |
mv table00000 head | |
FILE=`ls -1 table* | tail -n 1` | |
if [ $# -ge 2 ] ; then | |
mv $FILE foot | |
else | |
csplit --suppress-matched -b '%d' -s -f$FILE $FILE "/UNLOCK TABLES/" {*} | |
rm ${FILE} | |
mv ${FILE}0 $FILE | |
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" | |
rm $FILE | |
done | |
echo -n "Done with renaming tables @ " | |
date | |
rm head foot table* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also important to include SET_FOREIGN_KEY_CHECKS=0 on top
Another bonus would be a parallel table loading/import script