Skip to content

Instantly share code, notes, and snippets.

@gukandrew
Created June 12, 2023 15:49
Show Gist options
  • Save gukandrew/08c5ce1b8cc2decd074556a444ea5afb to your computer and use it in GitHub Desktop.
Save gukandrew/08c5ce1b8cc2decd074556a444ea5afb to your computer and use it in GitHub Desktop.
#!/bin/sh
# Script to create databases according to dump filenames if not yet imported
# put `dump.sql` files here near script
# Author: Andrew Guk
for file in /db-dumps/*.sql
do
filename=$(basename $file)
dbname="${filename%%.*}"
# if not work add socket arg into mysql. Eg: "mysql --socket=${SOCKET} ...""
tablecount=$(mysql -e "SELECT count(*) AS TOTALNUMBEROFTABLES FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$dbname';" | tail -1)
if [ "$tablecount" -lt 1 ]
then
echo "$dbname -> Creating database"
mysql -e "CREATE DATABASE IF NOT EXISTS \`$dbname\`;"
echo "$dbname <- Importing from $filename"
pv $file | mysql $dbname
tablecount=$(mysql -e "SELECT count(*) AS TOTALNUMBEROFTABLES FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$dbname';" | tail -1)
echo -e "\n\xE2\x9C\x94 $dbname <- Successfully imported $tablecount tables!"
else
echo -e "\xE2\x9C\x94 $dbname -> Database already exists. Skipping."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment