Created
May 5, 2023 21:22
-
-
Save PaulGoldschmidt/3daf28543e92348bd216a38b715d7b1c to your computer and use it in GitHub Desktop.
A small script that is able to split one big MariaDB-SQL-Dump into seperate DBs.
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 | |
if [ $# -eq 0 ] | |
then | |
echo "No dump file specified. Usage: ./extract_dbs.sh dumpfile.sql" | |
exit 1 | |
fi | |
# Extract database names from the dump file | |
databases=$(grep -oE '^CREATE DATABASE( IF NOT EXISTS)?( .*)? `[^`]+`(.*)?;' "$1" | sed -r 's/^CREATE DATABASE( IF NOT EXISTS)?( .*)? `(.*)`(.*)?;/\3/') | |
# Loop through each database name and extract it into a separate file | |
for db in $databases | |
do | |
echo "Extracting $db..." | |
awk -v db="$db" '/^CREATE DATABASE/ {p=0} /^USE `'"$db"'`;/ {p=1} {if(p) print}' "$1" > "$db".sql | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment