Last active
April 27, 2016 18:08
-
-
Save BackFront/1f9294ae7316cbc1a3524865fbe37b72 to your computer and use it in GitHub Desktop.
Automatizador de comandos para Mysql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
######################################################################### | |
# # | |
# MySQL Automate # | |
# Writen by: Douglas alves # | |
# Version: 1.2.2 # | |
# Released: 2016-04-27 # | |
# Licenced: MIT # | |
# # | |
######################################################################### | |
#ROOT_PASS='******' | |
export black='\033[0m' | |
export boldblack='\033[1;0m' | |
export red='\033[31m' | |
export boldred='\033[1;31m' | |
export green='\033[32m' | |
export boldgreen='\033[1;32m' | |
export yellow='\033[33m' | |
export boldyellow='\033[1;33m' | |
export blue='\033[34m' | |
export boldblue='\033[1;34m' | |
export magenta='\033[35m' | |
export boldmagenta='\033[1;35m' | |
export cyan='\033[36m' | |
export boldcyan='\033[1;36m' | |
export white='\033[37m' | |
export boldwhite='\033[1;37m' | |
# function BoasVindas(){ | |
# echo -e "\n\nSeja Bem Vindo $1 \n\n" | |
# } | |
function colorize (){ | |
## -- Function to easliy print colored text -- ## | |
## -- before, you need exporting the colors outside function: | |
# export black='\033[0m' | |
# export boldblack='\033[1;0m' | |
# export red='\033[31m' | |
# export boldred='\033[1;31m' | |
# export green='\033[32m' | |
# export boldgreen='\033[1;32m' | |
# export yellow='\033[33m' | |
# export boldyellow='\033[1;33m' | |
# export blue='\033[34m' | |
# export boldblue='\033[1;34m' | |
# export magenta='\033[35m' | |
# export boldmagenta='\033[1;35m' | |
# export cyan='\033[36m' | |
# export boldcyan='\033[1;36m' | |
# export white='\033[37m' | |
# export boldwhite='\033[1;37m' | |
# Color-echo. | |
# Argument $1 = message | |
# Argument $2 = color | |
# Argument $3 = before message | |
# Argument $4 = after message | |
local default_msg="No message passed." | |
message=${1:-$default_msg} # Defaults to default message. | |
#change it for fun | |
#We use pure names | |
color=${2:-black} # Defaults to black, if not specified. | |
printf "${3:-}" | |
case $color in | |
black) printf "$black" ;; | |
boldblack) printf "$boldblack" ;; | |
red) printf "$red" ;; | |
boldred) printf "$boldred" ;; | |
green) printf "$green" ;; | |
boldgreen) printf "$boldgreen" ;; | |
yellow) printf "$yellow" ;; | |
boldyellow) printf "$boldyellow" ;; | |
blue) printf "$blue" ;; | |
boldblue) printf "$boldblue" ;; | |
magenta) printf "$magenta" ;; | |
boldmagenta) printf "$boldmagenta" ;; | |
cyan) printf "$cyan" ;; | |
boldcyan) printf "$boldcyan" ;; | |
white) printf "$white" ;; | |
boldwhite) printf "$boldwhite" ;; | |
esac | |
printf "$message" | |
tput sgr0 # Reset to normal. | |
printf "$black" | |
printf "${4:-}\n" | |
return | |
} | |
echo "#########################################################################" | |
colorize " MySql Automate " boldyellow "#" "#" | |
echo "#########################################################################" | |
echo "# #" | |
colorize " Developed by: " boldwhite "#" "Douglas A. <[email protected]> #" | |
colorize " Version: " boldwhite "#" "1.2.2 #" | |
colorize " (C) all rights reserved - 2016" boldwhite "#" " #" | |
echo "# #" | |
echo "#########################################################################" | |
echo | |
echo | |
echo "Say the password 'root' user of MySql:" | |
stty -echo | |
read ROOT_PASS; | |
stty echo | |
echo | |
echo | |
echo "----------------------------------------------------" | |
echo "Select an option to continue:" | |
echo | |
echo " [1] - Create Database and User and assign it user to database created;" | |
echo " [2] - Clear a database;" | |
echo | |
echo "----------------------------------------------------" | |
echo | |
echo | |
read numero; | |
##Start Commands | |
case $numero in | |
1) | |
################################################################ | |
# [1] - Cria um novo banco de dados e atribui um usuario com o # | |
# mesmo nome que o banco de dados para o mesmo # | |
################################################################ | |
echo "Digite o nome para o banco de dados:" | |
read db_name; | |
echo "Digite a senha para o novo usuario" | |
stty -echo | |
read db_pass; | |
stty echo | |
MYSQL=`which mysql` | |
Q1="CREATE DATABASE IF NOT EXISTS $db_name;" | |
Q2="CREATE USER '$db_name'@'%' IDENTIFIED BY '$db_pass';" | |
Q3="GRANT ALL PRIVILEGES ON $db_name.* TO '$db_name'@'%';" | |
Q4="FLUSH PRIVILEGES;" | |
SQL="${Q1}${Q2}${Q3}${Q4}" | |
$MYSQL -u root -p"$ROOT_PASS" -e "$SQL" | |
echo "====================================================" | |
echo "| Banco de dados '$db_name' criado com sucesso. |" | |
echo "| Host (internal): 10.100.222.4 |" | |
echo "| Host (external): snd-db.cloudapp.net |" | |
echo "| Database: $db_name |" | |
echo "| User: $db_name |" | |
echo "| Pass: $db_pass |" | |
echo "====================================================" | |
;; | |
2) | |
########################################################### | |
# [2] - Reseta um banco de dados. Exclui todas as tabelas # | |
########################################################### | |
echo "Digite o nome do banco de dados:" | |
read db_name; | |
if [ ! -z "`mysql -qfsBe "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='$db_name'" 2>&1`" ]; then | |
echo | |
else | |
echo "DATABASE DOES NOT EXIST" | |
exit; | |
fi | |
echo "Voce tem certeza que deseja limpar o banco de dados '$db_name'? [y/n]" | |
read confirma; | |
if [ "$confirma" == "y" ]; then | |
MYSQL=`which mysql` | |
Q1="DROP DATABASE IF EXISTS $db_name;" | |
Q2="CREATE DATABASE IF NOT EXISTS $db_name;" | |
SQL="${Q1} ${Q2}" | |
$MYSQL -u root -p"$ROOT_PASS" -e "$SQL" | |
echo "Database $db_name has been cleaned!" | |
else | |
echo "Not authorized!!" | |
exit; | |
fi | |
;; | |
esac | |
echo "Finished execution" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment