bash: load db with progress
Posted: 24 Sep 2020, 4:01am - Thursday

Just want to share this and my reminder how to create the script.

#!/bin/bash

NOW=$(date -d '-1 day' '+%Y-%m-%d')

COUNCIL=$1

gzip -dv db_${NOW}/${COUNCIL}_checklists.mysql.dmp.gz
gzip -dv db_${NOW}/${COUNCIL}_obca.mysql.dmp.gz
gzip -dv db_${NOW}/${COUNCIL}_fileman.mysql.dmp.gz
gzip -dv db_${NOW}/${COUNCIL}_logging.mysql.dmp.gz

mysql -h 127.0.0.1 -u root -psecret -e "CREATE DATABASE IF NOT EXISTS ${COUNCIL}_checklists"
mysql -h 127.0.0.1 -u root -psecret -e "CREATE DATABASE IF NOT EXISTS ${COUNCIL}_obca"
mysql -h 127.0.0.1 -u root -psecret -e "CREATE DATABASE IF NOT EXISTS ${COUNCIL}_logging"
mysql -h 127.0.0.1 -u root -psecret -e "CREATE DATABASE IF NOT EXISTS ${COUNCIL}_fileman"

#mysql ${COUNCIL}_checklists -h 127.0.0.1 -u root -psecret --force < db_${NOW}/${COUNCIL}_checklists.mysql.dmp
#mysql ${COUNCIL}_obca -h 127.0.0.1 -u root -psecret < db_${NOW}/${COUNCIL}_obca.mysql.dmp
#mysql ${COUNCIL}_fileman -h 127.0.0.1 -u root -psecret < db_${NOW}/${COUNCIL}_fileman.mysql.dmp
#mysql ${COUNCIL}_logging -h 127.0.0.1 -u root -psecret < db_${NOW}/${COUNCIL}_logging.mysql.dmp

pv db_${NOW}/${COUNCIL}_checklists.mysql.dmp | mysql ${COUNCIL}_checklists -h 127.0.0.1 -u root -psecret --force
pv db_${NOW}/${COUNCIL}_obca.mysql.dmp | mysql ${COUNCIL}_obca -h 127.0.0.1 -u root -psecret --force
pv db_${NOW}/${COUNCIL}_logging.mysql.dmp | mysql ${COUNCIL}_logging -h 127.0.0.1 -u root -psecret --force
pv db_${NOW}/${COUNCIL}_fileman.mysql.dmp | mysql ${COUNCIL}_fileman -h 127.0.0.1 -u root -psecret --force

gzip -9v db_${NOW}/${COUNCIL}_checklists.mysql.dmp
gzip -9v db_${NOW}/${COUNCIL}_obca.mysql.dmp
gzip -9v db_${NOW}/${COUNCIL}_fileman.mysql.dmp
gzip -9v db_${NOW}/${COUNCIL}_logging.mysql.dmp

echo "Import completed!"

Sample output below:

screenshot of the script in action...

That's it! Cheers!