Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement