Created
March 2, 2016 14:53
-
-
Save byrnedo/486949819b84ed8ca571 to your computer and use it in GitHub Desktop.
Splits a phpmyadmin sql export into one file per database. Requires a 'footer.sql' and 'header.sql' file for custom content.
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 | |
set -euo pipefail | |
mkdir -p ./out | |
rm -rf ./out/prod_split_* | |
csplit -n2 -s -f "out/" -b "prod_split_%d" $1 "/-- Database: /-1" "{*}" | |
rm -f ./out/prod_split_0 | |
cd ./out | |
for file in `ls -1 prod_split_*`; do | |
echo $file | |
db=$(grep -nr "\-\- Database:" $file|sed 's/\([0-9]*\):-- Database: `\([a-zA-Z_]*\)`/\2/') | |
echo $db | |
rm -f tmpuse.sql | |
echo "use $db" > tmpuse.sql | |
cat ../header.sql tmpuse.sql $file ../footer.sql > $db.sql | |
rm -f $file | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment