Created
June 8, 2012 19:45
-
-
Save kmatt/2897778 to your computer and use it in GitHub Desktop.
Split MySQL dumps
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
file=$1 | |
nextDB="" | |
nextStart=0 | |
nextEnd=0 | |
lastDB="" | |
lastStart=0 | |
for i in `grep -n "^CREATE DATABASE" $file | awk '{print $1":"$7}' | sed "s/CREATE://g"` | |
do | |
i=`echo $i | sed "s/\\\`//g"` | |
nextDB=`echo $i | cut -d : -f 2` | |
nextStart=`echo $i | cut -d : -f 1` | |
nextEnd=$(( $nextStart -1 )) | |
if [ "$lastDB" == "" ] | |
then | |
header=`sed -n 1,${nextEnd}p $file` | |
else | |
echo "$lastDB from $lastStart to $nextEnd" | |
echo "$header" > _$lastDB.sql | |
sed -n ${lastStart},${nextEnd}p $file >> _$lastDB.sql | |
fi | |
lastDB=$nextDB | |
lastStart=$(( $nextStart )) | |
done | |
echo "$lastDB from $lastStart" | |
echo "$header" > _$lastDB.sql | |
sed -n ${lastStart},\$p $file >> _$lastDB.sql |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An alteration of http://lists.mysql.com/mysql/226821