daily pastebin goal
75%
SHARE
TWEET

Untitled

a guest Apr 27th, 2016 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #########################################################################
  3. #                                                                       #
  4. #       MySQL Automate                                                  #
  5. #       Writen by: Douglas alves                                        #
  6. #       Version: 1.2.2                                                  #
  7. #       Released: 2016-04-27                                            #
  8. #       Licenced: MIT                                                   #
  9. #                                                                       #
  10. #########################################################################
  11.  
  12. #ROOT_PASS='******'
  13.  
  14. export black='\033[0m'
  15. export boldblack='\033[1;0m'
  16. export red='\033[31m'
  17. export boldred='\033[1;31m'
  18. export green='\033[32m'
  19. export boldgreen='\033[1;32m'
  20. export yellow='\033[33m'
  21. export boldyellow='\033[1;33m'
  22. export blue='\033[34m'
  23. export boldblue='\033[1;34m'
  24. export magenta='\033[35m'
  25. export boldmagenta='\033[1;35m'
  26. export cyan='\033[36m'
  27. export boldcyan='\033[1;36m'
  28. export white='\033[37m'
  29. export boldwhite='\033[1;37m'
  30.  
  31. # function BoasVindas(){
  32. #      echo -e "\n\nSeja Bem Vindo $1 \n\n"
  33. # }
  34.  
  35. function colorize (){
  36.   ## -- Function to easliy print colored text -- ##
  37.  
  38.   ## -- before, you need exporting the colors outside function:
  39.           # export black='\033[0m'
  40.           # export boldblack='\033[1;0m'
  41.           # export red='\033[31m'
  42.           # export boldred='\033[1;31m'
  43.           # export green='\033[32m'
  44.           # export boldgreen='\033[1;32m'
  45.           # export yellow='\033[33m'
  46.           # export boldyellow='\033[1;33m'
  47.           # export blue='\033[34m'
  48.           # export boldblue='\033[1;34m'
  49.           # export magenta='\033[35m'
  50.           # export boldmagenta='\033[1;35m'
  51.           # export cyan='\033[36m'
  52.           # export boldcyan='\033[1;36m'
  53.           # export white='\033[37m'
  54.           # export boldwhite='\033[1;37m'
  55.  
  56.           # Color-echo.
  57.           # Argument $1 = message
  58.           # Argument $2 = color
  59.           # Argument $3 = before message
  60.           # Argument $4 = after message
  61.  
  62.  
  63.   local default_msg="No message passed."
  64.  
  65.   message=${1:-$default_msg}      # Defaults to default message.
  66.  
  67.   #change it for fun
  68.   #We use pure names
  69.   color=${2:-black}               # Defaults to black, if not specified.
  70.  
  71.   printf "${3:-}"
  72.   case $color in
  73.     black) printf "$black" ;;
  74.     boldblack) printf "$boldblack" ;;
  75.     red) printf "$red" ;;
  76.     boldred) printf "$boldred" ;;
  77.     green) printf "$green" ;;
  78.     boldgreen) printf "$boldgreen" ;;
  79.     yellow) printf "$yellow" ;;
  80.     boldyellow) printf "$boldyellow" ;;
  81.     blue) printf "$blue" ;;
  82.     boldblue) printf "$boldblue" ;;
  83.     magenta) printf "$magenta" ;;
  84.     boldmagenta) printf "$boldmagenta" ;;
  85.     cyan) printf "$cyan" ;;
  86.     boldcyan) printf "$boldcyan" ;;
  87.     white) printf "$white" ;;
  88.     boldwhite) printf "$boldwhite" ;;
  89.   esac
  90.  
  91.   printf "$message"
  92.   tput sgr0   # Reset to normal.
  93.   printf "$black"
  94.   printf "${4:-}\n"
  95.   return
  96. }
  97.  
  98.  
  99. echo "#########################################################################"
  100. colorize "       MySql Automate                                                  " boldyellow "#" "#"
  101. echo "#########################################################################"
  102. echo "#                                                                       #"
  103. colorize "       Developed by: " boldwhite "#" "Douglas A. <alves.douglaz@gmail.com>              #"
  104. colorize "       Version: " boldwhite "#" "1.2.2                                                  #"
  105. colorize "       (C) all rights reserved - 2016" boldwhite "#" "                                  #"
  106. echo "#                                                                       #"
  107. echo "#########################################################################"
  108. echo
  109. echo
  110.  
  111. echo "Say the password 'root' user of MySql:"
  112.  
  113. stty -echo
  114. read ROOT_PASS;
  115. stty echo
  116.  
  117. echo
  118. echo
  119. echo "----------------------------------------------------"
  120. echo "Select an option to continue:"
  121. echo
  122. echo "        [1] - Create Database and User and assign it user to database created;"
  123. echo "        [2] - Clear a database;"
  124. echo
  125. echo "----------------------------------------------------"
  126. echo
  127. echo
  128.  
  129. read numero;
  130.  
  131. ##Start Commands
  132. case $numero in
  133.   1)
  134.     ################################################################
  135.     # [1] - Cria um novo banco de dados e atribui um usuario com o #
  136.     # mesmo nome que o banco de dados para o mesmo                 #
  137.     ################################################################
  138.     echo "Digite o nome para o banco de dados:"
  139.     read db_name;
  140.     echo "Digite a senha para o novo usuario"
  141.     stty -echo
  142.     read db_pass;
  143.     stty echo
  144.     MYSQL=`which mysql`
  145.     Q1="CREATE DATABASE IF NOT EXISTS $db_name;"
  146.     Q2="CREATE USER '$db_name'@'%' IDENTIFIED BY '$db_pass';"
  147.     Q3="GRANT ALL PRIVILEGES ON $db_name.* TO '$db_name'@'%';"
  148.     Q4="FLUSH PRIVILEGES;"
  149.     SQL="${Q1}${Q2}${Q3}${Q4}"
  150.     $MYSQL -u root -p"$ROOT_PASS" -e "$SQL"
  151.     echo "===================================================="
  152.     echo "| Banco de dados '$db_name' criado com sucesso.    |"
  153.     echo "| Host (internal): 10.100.222.4                    |"
  154.     echo "| Host (external): snd-db.cloudapp.net             |"
  155.     echo "| Database: $db_name                               |"
  156.     echo "| User: $db_name                                   |"
  157.     echo "| Pass: $db_pass                                   |"
  158.     echo "===================================================="
  159.   ;;
  160.  
  161.   2)
  162.     ###########################################################
  163.     # [2] - Reseta um banco de dados. Exclui todas as tabelas #
  164.     ###########################################################
  165.     echo "Digite o nome do banco de dados:"
  166.     read db_name;
  167.     if [ ! -z "`mysql -qfsBe "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='$db_name'" 2>&1`" ]; then
  168.       echo
  169.     else
  170.       echo "DATABASE DOES NOT EXIST"
  171.       exit;
  172.     fi
  173.  
  174.     echo "Voce tem certeza que deseja limpar o banco de dados '$db_name'? [y/n]"
  175.     read confirma;
  176.     if [ "$confirma" == "y" ]; then
  177.       MYSQL=`which mysql`
  178.       Q1="DROP DATABASE IF EXISTS $db_name;"
  179.       Q2="CREATE DATABASE IF NOT EXISTS $db_name;"
  180.       SQL="${Q1} ${Q2}"
  181.       $MYSQL -u root -p"$ROOT_PASS" -e "$SQL"
  182.  
  183.       echo "Database $db_name has been cleaned!"
  184.     else
  185.       echo "Not authorized!!"
  186.       exit;
  187.     fi
  188.   ;;
  189. esac
  190.  
  191. echo "Finished execution"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top