Created
June 18, 2017 13:58
-
-
Save thanuja919/cfdddec8892b8a17b85ca36a57ea085c to your computer and use it in GitHub Desktop.
split-mysql-dump
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
#!/usr/bin/env bash | |
#When executing the script keep the main db dump in another folder. | |
AWK=awk | |
REGEX_NAME="Current Database: \`(.*)\`" | |
# Checks argument and prints usage if needed | |
if [ "$#" -lt "1" ] | |
then | |
echo "Usage: $0 <dump.sql>" | |
exit 1 | |
fi | |
# Splits dump into temporary files | |
$AWK '/Current Database\: .*/{g++} { print $0 > g".tmpsql" }' $1 | |
# Renames files or appends to existing one (to handle views) | |
for f in *.tmpsql | |
do | |
DATABASE_LINE=`head -n1 $f` | |
[[ $DATABASE_LINE =~ $REGEX_NAME ]] | |
TARGET_FILE="${BASH_REMATCH[1]}.sql" | |
if [ -f $TARGET_FILE ]; then | |
cat $f >> $TARGET_FILE | |
rm $f | |
else | |
mv $f ${BASH_REMATCH[1]}.sql | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment