Skip to content

Instantly share code, notes, and snippets.

@PaulGoldschmidt
Created May 5, 2023 21:22
Show Gist options
  • Save PaulGoldschmidt/3daf28543e92348bd216a38b715d7b1c to your computer and use it in GitHub Desktop.
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.
#!/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