Skip to content

Instantly share code, notes, and snippets.

@henno
Last active January 12, 2020 09:57
Show Gist options
  • Save henno/7139c7c9ff132d541411441632969e6e to your computer and use it in GitHub Desktop.
Save henno/7139c7c9ff132d541411441632969e6e to your computer and use it in GitHub Desktop.
Bash (for example git-bash.exe) version
#!/usr/bin/env bash
# This file can do one of two things:
# - reset a MySQL database back to original state from a dump file at given location and
# - create a new dump file from the current contents of given MySQL database
#
# Usage for resetting a database from dump:
#
# refresh_database.sh yourdatabasename
#
#
# Usage for creating a new dump file (or overwriting an existing one):
#
# refresh_database.sh yourdatabasename --dump
# Variables
DUMP_FILE_LOCATION=doc/database.sql
MYSQL_PASSWORD=""
MYSQL=/Applications/MAMP/Library/bin/mysql
MYSQLDUMP=/Applications/MAMP/Library/bin/mysqldump
if [[ $2 == **--dump** ]]
then
echo "Dumping database $1"
MYSQL_PWD="$MYSQL_PASSWORD" $MYSQLDUMP -u root $1 > $DUMP_FILE_LOCATION
else
echo "Dropping database $1"
MYSQL_PWD="$MYSQL_PASSWORD" $MYSQL -u root -e "drop database $1"
echo "Creating database $1"
MYSQL_PWD="$MYSQL_PASSWORD" $MYSQL -u root -e "create database $1"
echo "Importing doc/database.sql"
MYSQL_PWD="$MYSQL_PASSWORD" $MYSQL -u root $1 < $DUMP_FILE_LOCATION
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment