Skip to content

Instantly share code, notes, and snippets.

@labbots
Created December 23, 2016 13:25
Show Gist options
  • Save labbots/7f2edfbc2b9c70877db07efb4ac006ae to your computer and use it in GitHub Desktop.
Save labbots/7f2edfbc2b9c70877db07efb4ac006ae to your computer and use it in GitHub Desktop.
Script to replicate Mysql databases for local development
#!/bin/bash
source=${1}
destination=${2}
user=${3:-root}
pass=${4}
# Help menu
print_help() {
echo -e "
\e[01;34mInstructions:\e[0m
This script is used to replicate database.
Following arguments are available in the script :
1) \e[01;32m --source \e[0m - Source database. (mandatory)
2) \e[01;32m --destination \e[0m - Destination database.(mandatory)
3) \e[01;32m --user \e[0m - Database user who have access to both source and destination database.(mandatory)
4) \e[01;32m --pass \e[0m - Password for the database user.(mandatory)
\e[01;36mUsage:\e[0m ./${0##*/} --source=DB --destination=DB --user=USERNAME --pass=PASSWORD
"
exit 0
}
while [ $# -gt 0 ]; do
case "$1" in
--source=*)
source="${1#*=}"
;;
--destination=*)
destination="${1#*=}"
;;
--user=*)
user="${1#*=}"
;;
--pass=*)
pass="${1#*=}"
;;
--help) print_help
;;
*)
printf "Invalid argument passed to the script.\n";
exit 1
esac
shift
done
echo "Database replication started"
mysql -u$user -p$pass -e "DROP SCHEMA IF EXISTS $destination;"
mysql -u$user -p$pass -e "CREATE DATABASE IF NOT EXISTS $destination DEFAULT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci';"
mysqldump -u$user -p$pass $source | mysql -u$user -p$pass $destination
echo "$source database has been replicated on $destination database."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment