Need to split big SQL dump into separate databases?

I do. So I’ve written small bash script to do so. I think it’s self explanatory and does it’s job well :)

#!/bin/bash

if [[ $# -lt 1 ]]; then
  echo "Usage: $0 filename"
  exit 1
fi

FILE=$1
echo "Spliting "$FILE""

# default filename for sql headers
dbname=header

cat $FILE | while read line; do
 # echo $line
  if [[ $line =~ ^USE\ \`([^\`]*)\`\; ]]; then
    dbname=${BASH_REMATCH[1]}
    echo "Found db '$dbname'"
  fi
  echo $line >> $dbname.sql
done

I do NOT have CREATE DATABASE in sql file, thus set to USE..

Leave a Reply

Your email address will not be published. Required fields are marked *

9 × 1 =