Skip to content

Instantly share code, notes, and snippets.

@gmhabchi
Last active May 1, 2020 00:53
Show Gist options
  • Save gmhabchi/74986f14d2217c6e1363ca378e95ae99 to your computer and use it in GitHub Desktop.
Save gmhabchi/74986f14d2217c6e1363ca378e95ae99 to your computer and use it in GitHub Desktop.
SQL Migration
#!/bin/bash
sql_dump() {
echo "Performing mysqldump on database $DBNAME @ $HOST"
MYSQL_PWD=$PASSWORD mysqldump -h $HOST -P 3306 -u root $DBNAME > $DBNAME.sql
}
restore() {
echo "Performing restore on database $DBNAME @ $HOST"
MYSQL_PWD=$PASSWORD mysql -h $HOST -P 3306 -u root $DBNAME < $DBNAME.sql
}
# Get input
read -p 'Function: sql_dump or restore: ' FUNCTION
read -p 'DBName: ' DBNAME
read -p 'Host: ' HOST
read -sp 'Password: ' PASSWORD
if [[ -z $FUNCTION || -z $DBNAME || -z $HOST || -z $PASSWORD ]]
then
echo "Not all params supplied"
else
if [ "$FUNCTION" == "sql_dump" ]
then
sql_dump
elif [ "$FUNCTION" == "restore" ]
then
restore
else
echo "Please use either 'sql_dump' or 'restore' as function"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment