0xCor3

userdb_creator.sh

Aug 4th, 2018
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.72 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # Script to create MySQL db + user
  5. #
  6. # @author   Raj KB <[email protected]>
  7. # @website  http://www.magepsycho.com
  8. # @version  0.1.0
  9.  
  10. ################################################################################
  11. # CORE FUNCTIONS - Do not edit
  12. ################################################################################
  13. #
  14. # VARIABLES
  15. #
  16. _bold=$(tput bold)
  17. _underline=$(tput sgr 0 1)
  18. _reset=$(tput sgr0)
  19.  
  20. _purple=$(tput setaf 171)
  21. _red=$(tput setaf 1)
  22. _green=$(tput setaf 76)
  23. _tan=$(tput setaf 3)
  24. _blue=$(tput setaf 38)
  25.  
  26. #
  27. # HEADERS & LOGGING
  28. #
  29. function _debug()
  30. {
  31.     [ "$DEBUG" -eq 1 ] && $@
  32. }
  33.  
  34. function _header()
  35. {
  36.     printf "\n${_bold}${_purple}==========  %s  ==========${_reset}\n" "$@"
  37. }
  38.  
  39. function _arrow()
  40. {
  41.     printf "➜ $@\n"
  42. }
  43.  
  44. function _success()
  45. {
  46.     printf "${_green}✔ %s${_reset}\n" "$@"
  47. }
  48.  
  49. function _error() {
  50.     printf "${_red}✖ %s${_reset}\n" "$@"
  51. }
  52.  
  53. function _warning()
  54. {
  55.     printf "${_tan}➜ %s${_reset}\n" "$@"
  56. }
  57.  
  58. function _underline()
  59. {
  60.     printf "${_underline}${_bold}%s${_reset}\n" "$@"
  61. }
  62.  
  63. function _bold()
  64. {
  65.     printf "${_bold}%s${_reset}\n" "$@"
  66. }
  67.  
  68. function _note()
  69. {
  70.     printf "${_underline}${_bold}${_blue}Note:${_reset}  ${_blue}%s${_reset}\n" "$@"
  71. }
  72.  
  73. function _die()
  74. {
  75.     _error "$@"
  76.     exit 1
  77. }
  78.  
  79. function _safeExit()
  80. {
  81.     exit 0
  82. }
  83.  
  84. #
  85. # UTILITY HELPER
  86. #
  87. function _seekConfirmation()
  88. {
  89.   printf "\n${_bold}$@${_reset}"
  90.   read -p " (y/n) " -n 1
  91.   printf "\n"
  92. }
  93.  
  94. # Test whether the result of an 'ask' is a confirmation
  95. function _isConfirmed()
  96. {
  97.     if [[ "$REPLY" =~ ^[Yy]$ ]]; then
  98.         return 0
  99.     fi
  100.     return 1
  101. }
  102.  
  103.  
  104. function _typeExists()
  105. {
  106.     if [ $(type -P $1) ]; then
  107.         return 0
  108.     fi
  109.     return 1
  110. }
  111.  
  112. function _isOs()
  113. {
  114.     if [[ "${OSTYPE}" == $1* ]]; then
  115.       return 0
  116.     fi
  117.     return 1
  118. }
  119.  
  120. function _checkRootUser()
  121. {
  122.     #if [ "$(id -u)" != "0" ]; then
  123.     if [ "$(whoami)" != 'root' ]; then
  124.         echo "You have no permission to run $0 as non-root user. Use sudo"
  125.         exit 1;
  126.     fi
  127.  
  128. }
  129.  
  130. function _printPoweredBy()
  131. {
  132.     cat <<"EOF"
  133.  
  134. Powered By:
  135.    __  ___              ___               __
  136.   /  |/  /__ ____ ____ / _ \___ __ ______/ /  ___
  137.  / /|_/ / _ `/ _ `/ -_) ___(_-</ // / __/ _ \/ _ \
  138. /_/  /_/\_,_/\_, /\__/_/  /___/\_, /\__/_//_/\___/
  139.             /___/             /___/
  140.  
  141.  >> Store: http://www.magepsycho.com
  142.  >> Blog: http://www.blog.magepsycho.com
  143.  
  144. ################################################################
  145. EOF
  146. }
  147. ################################################################################
  148. # SCRIPT FUNCTIONS
  149. ################################################################################
  150. function generatePassword()
  151. {
  152.     echo "$(openssl rand -base64 12)"
  153. }
  154.  
  155. function _printUsage()
  156. {
  157.     echo -n "$(basename $0) [OPTION]...
  158.  
  159. Create MySQL db & user.
  160. Version $VERSION
  161.  
  162.    Options:
  163.        -h, --host        MySQL Host
  164.        -d, --database    MySQL Database
  165.        -u, --user        MySQL User
  166.        -p, --pass        MySQL Password (If empty, auto-generated)
  167.        -h, --help        Display this help and exit
  168.        -v, --version     Output version information and exit
  169.  
  170.    Examples:
  171.        $(basename $0) --help
  172.  
  173. "
  174.     _printPoweredBy
  175.     exit 1
  176. }
  177.  
  178. function processArgs()
  179. {
  180.     # Parse Arguments
  181.     for arg in "$@"
  182.     do
  183.         case $arg in
  184.             -h=*|--host=*)
  185.                 DB_HOST="${arg#*=}"
  186.             ;;
  187.             -d=*|--database=*)
  188.                 DB_NAME="${arg#*=}"
  189.             ;;
  190.             -u=*|--user=*)
  191.                 DB_USER="${arg#*=}"
  192.             ;;
  193.              -p=*|--pass=*)
  194.                 DB_PASS="${arg#*=}"
  195.             ;;
  196.             --debug)
  197.                 DEBUG=1
  198.             ;;
  199.             -h|--help)
  200.                 _printUsage
  201.             ;;
  202.             *)
  203.                 _printUsage
  204.             ;;
  205.         esac
  206.     done
  207.     [[ -z $DB_NAME ]] && _error "Database name cannot be empty." && exit 1
  208.     [[ $DB_USER ]] || DB_USER=$DB_NAME
  209. }
  210.  
  211. function createMysqlDbUser()
  212. {
  213.     SQL1="CREATE DATABASE IF NOT EXISTS ${DB_NAME};"
  214.     SQL2="CREATE USER '${DB_USER}'@'%' IDENTIFIED BY '${DB_PASS}';"
  215.     SQL3="GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'%';"
  216.     SQL4="FLUSH PRIVILEGES;"
  217.  
  218.     if [ -f /root/.my.cnf ]; then
  219.         $BIN_MYSQL -e "${SQL1}${SQL2}${SQL3}${SQL4}"
  220.     else
  221.         # If /root/.my.cnf doesn't exist then it'll ask for root password
  222.         _arrow "Please enter root user MySQL password!"
  223.         read rootPassword
  224.         $BIN_MYSQL -h $DB_HOST -u root -p${rootPassword} -e "${SQL1}${SQL2}${SQL3}${SQL4}"
  225.     fi
  226. }
  227.  
  228. function printSuccessMessage()
  229. {
  230.     _success "MySQL DB / User creation completed!"
  231.  
  232.     echo "################################################################"
  233.     echo ""
  234.     echo " >> Host      : ${DB_HOST}"
  235.     echo " >> Database  : ${DB_NAME}"
  236.     echo " >> User      : ${DB_USER}"
  237.     echo " >> Pass      : ${DB_PASS}"
  238.     echo ""
  239.     echo "################################################################"
  240.     _printPoweredBy
  241.  
  242. }
  243.  
  244. ################################################################################
  245. # Main
  246. ################################################################################
  247. export LC_CTYPE=C
  248. export LANG=C
  249.  
  250. DEBUG=0 # 1|0
  251. _debug set -x
  252. VERSION="0.1.0"
  253.  
  254. BIN_MYSQL=$(which mysql)
  255.  
  256. DB_HOST='localhost'
  257. DB_NAME=
  258. DB_USER=
  259. DB_PASS=$(generatePassword)
  260.  
  261. function main()
  262. {
  263.     [[ $# -lt 1 ]] && _printUsage
  264.     _success "Processing arguments..."
  265.     processArgs "$@"
  266.     _success "Done!"
  267.  
  268.     _success "Creating MySQL db and user..."
  269.     createMysqlDbUser
  270.     _success "Done!"
  271.  
  272.     printSuccessMessage
  273.  
  274.     exit 0
  275. }
  276.  
  277. main "$@"
  278.  
  279. _debug set +x
Add Comment
Please, Sign In to add comment