Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. source=${1}
  4. destination=${2}
  5. user=${3:-root}
  6. pass=${4}
  7.  
  8. # Help menu
  9. print_help() {
  10. echo -e "
  11. \e[01;34mInstructions:\e[0m
  12. This script is used to replicate database.
  13. Following arguments are available in the script :
  14.  
  15. 1) \e[01;32m --source \e[0m - Source database. (mandatory)
  16. 2) \e[01;32m --destination \e[0m - Destination database.(mandatory)
  17. 3) \e[01;32m --user \e[0m - Database user who have access to both source and destination database.(mandatory)
  18. 4) \e[01;32m --pass \e[0m - Password for the database user.(mandatory)
  19.  
  20.  
  21. \e[01;36mUsage:\e[0m ./${0##*/} --source=DB --destination=DB --user=USERNAME --pass=PASSWORD
  22.  
  23.  
  24. "
  25. exit 0
  26. }
  27.  
  28.  
  29. while [ $# -gt 0 ]; do
  30. case "$1" in
  31. --source=*)
  32. source="${1#*=}"
  33. ;;
  34. --destination=*)
  35. destination="${1#*=}"
  36. ;;
  37. --user=*)
  38. user="${1#*=}"
  39. ;;
  40. --pass=*)
  41. pass="${1#*=}"
  42. ;;
  43. --help) print_help
  44. ;;
  45. *)
  46. printf "Invalid argument passed to the script.\n";
  47. exit 1
  48. esac
  49. shift
  50. done
  51.  
  52. echo "Database replication started"
  53.  
  54. mysql -u$user -p$pass -e "DROP SCHEMA IF EXISTS $destination;"
  55. mysql -u$user -p$pass -e "CREATE DATABASE IF NOT EXISTS $destination DEFAULT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci';"
  56. mysqldump -u$user -p$pass $source | mysql -u$user -p$pass $destination
  57.  
  58. echo "$source database has been replicated on $destination database."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement