Skip to content

Instantly share code, notes, and snippets.

@icyz
Created October 18, 2021 15:53
Show Gist options
  • Select an option

  • Save icyz/dcb8389ab13249ae3744d70e6b8a8b3b to your computer and use it in GitHub Desktop.

Select an option

Save icyz/dcb8389ab13249ae3744d70e6b8a8b3b to your computer and use it in GitHub Desktop.

Revisions

  1. icyz created this gist Oct 18, 2021.
    43 changes: 43 additions & 0 deletions mysqlrestore.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    #!/bin/bash

    # How to use: bash mysqlrestore.sh DATABASENAME /path/to/dump.sql

    ##########################
    MPATH="/bin/"
    HOST="127.0.0.1"
    USER="root"
    PASS="root"
    ##########################

    # Colors
    BLACK=$(tput setaf 0)
    RED=$(tput setaf 1)
    GREEN=$(tput setaf 2)
    YELLOW=$(tput setaf 3)
    LIME_YELLOW=$(tput setaf 190)
    POWDER_BLUE=$(tput setaf 153)
    BLUE=$(tput setaf 4)
    MAGENTA=$(tput setaf 5)
    CYAN=$(tput setaf 6)
    WHITE=$(tput setaf 7)
    BRIGHT=$(tput bold)
    NORMAL=$(tput sgr0)
    BLINK=$(tput blink)
    REVERSE=$(tput smso)
    UNDERLINE=$(tput smul)


    printf "\n\n${RED}Delete $1\n================================\n${NORMAL}"
    ${MPATH}mysqladmin -h "${HOST}" -u "${USER}" "-p${PASS}" drop "$1"

    printf "\n\n${GREEN}Create $1\n================================\n${NORMAL}"
    ${MPATH}mysql -u "${USER}" "-p${PASS}" << EOF
    CREATE DATABASE $1 COLLATE 'utf8_unicode_ci';
    GRANT ALL PRIVILEGES ON *.* TO '${USER}'@'${HOST}';
    FLUSH PRIVILEGES;
    EOF

    printf "\n\n${CYAN}Load $1 < $2\n================================\n${NORMAL}"
    ${MPATH}mysql -f -h "${HOST}" -u "${USER}" "-p${PASS}" "$1" < "$2"

    printf "\n"