Skip to content

Instantly share code, notes, and snippets.

@levi730
Forked from m-faraz/mysql_splitdump_v2.sh
Created July 19, 2024 16:52
Show Gist options
  • Save levi730/360ed1f102a765d13a45ef6297f9aa00 to your computer and use it in GitHub Desktop.
Save levi730/360ed1f102a765d13a45ef6297f9aa00 to your computer and use it in GitHub Desktop.
Split MySQL dump SQL file into one file per table
#!/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