Add/Redesign scripts for creating/dropping databases

This commit is contained in:
Christoph 2017-09-23 01:09:29 +02:00
parent 1a89bbbd84
commit e04e224888
7 changed files with 964 additions and 0 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
*.swp
/conf/*.conf /conf/*.conf
/BAK/* /BAK/*
/databases/*
databases.txt* databases.txt*

View File

@ -5,6 +5,7 @@ mysql_credential_args="--login-path=local"
_drop=false _drop=false
#_drop=true
echo "" echo ""
while read _db_name _db_user _db_pass ; do while read _db_name _db_user _db_pass ; do

View File

@ -0,0 +1,42 @@
## ===================================================================
## - Configuration File for "create_drop_database.sh" Script
## ===================================================================
# - ACTION
# -
# - What to do ? Create or drop a given Database
# -
# - Only possible values are 'create' or 'drop'
# -
#ACTION=""
# - MYSQL_CREDENTIAL_ARGS
# -
# - MySQL / MariaDB credentials
# -
# - Giving password on command line is insecure an sind mysql 5.5
# - you will get a warning doing so.
# -
# - Reading username/password fro file ist also possible, using MySQL/MariaDB
# - commandline parameter '--defaults-file'.
# -
# - Since Mysql Version 5.6, you can read username/password from
# - encrypted file.
# -
# - Create (encrypted) option file:
# - $ mysql_config_editor set --login-path=local --socket=/tmp/mysql.sock --user=root --password
# - $ Password:
# -
# - Use of option file:
# - $ mysql --login-path=local ...
# -
# - Example
# - MYSQL_CREDENTIAL_ARGS="--login-path=local"
# - MYSQL_CREDENTIAL_ARGS="--defaults-file=/etc/mysql/debian.cnf" (Debian default)
# - MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
# -
# - Defaults to MYSQL_CREDENTIAL_ARGS="--login-path=local"
# -
#MYSQL_CREDENTIAL_ARGS=""

448
create_database.sh Executable file
View File

@ -0,0 +1,448 @@
#!/usr/bin/env bash
working_dir="$(dirname $(realpath $0))"
log_dir="${working_dir}/log"
conf_file="${working_dir}/conf/create_drop_database.conf"
tmp_log_file="$(mktemp)"
# -------------
# - Variable settings
# -------------
#DEFAULT_ACTION='create'
DEFAULT_MYSQL_CREDENTIAL_ARGS="--login-path=local"
DATABASE_NAME=""
DATABASE_USER=""
DATABASE_PASSWD=""
DATABASE_NAME_NEEDED=true
DATABASE_USER_NEEDED=true
DATABASE_PASSWD_NEEDED=true
QUIET_MODE=false
NON_INTERACTIVE_MODE=false
# -------------
# --- Some functions
# -------------
usage() {
echo
[ -n "$1" ] && echo -e "Error: $1\n"
cat<<EOF
Usage: $(basename $0) [Options]
Script creates a MySQL database. The database name is either given on command
line using parameter '-d' or will be asked for otherwise.
Options:
-d <db-name>
The name of the domain, which is requested for deletion. If not set, script
will ask for the database name.
-I
Non-interactive mode. Script will act in non-interactice mode. At least the
database name must be given using parameter '-d'. Default is acting in
interactive mode
-p <db-passwd>
Password for the given user. If not set, script looks for a file named
'databases/<db-name>'. If found, script reads database password from that file.
If not running in quiet mode, you will be asked to confirm or set the db-user.
-q
Be quiet. Setting this parameter implies non-interactive mode (parameter -I). So
if '-q' is set, at least the database name must be given using parameter '-d'.
-u <db-user>
The name of the database user who will be granted full access to the given
database. If not set, script looks for a file 'databases/<db-name>'. If
found, script reads database settings (user/password) from that file. If not
running in quiet mode, you will be asked to confirm or set the db-user.
EOF
clean_up 1
}
clean_up() {
# Perform program exit housekeeping
rm -f $tmp_log_file
exit $1
}
fatal(){
echo ""
if $terminal ; then
if [[ -n "$*" ]] ; then
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
else
echo " \033[31m\033[1mFatal error\033[m:"
fi
echo ""
echo -e " \033[31m\033[1mScript will be interrupted.\033[m\033[m"
else
if [[ -n "$*" ]] ; then
echo " [ Fatal ]: $*"
else
echo " Fatal error:"
fi
echo ""
echo " Script was terminated...."
fi
echo ""
clean_up 1
}
echononl(){
if $terminal && ! $QUIET_MODE ; then
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
fi
}
error(){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mError\033[m ]: $*"
else
echo "Error: $*"
fi
echo ""
}
warn (){
if $terminal && ! $QUIET_MODE ; then
echo ""
echo -e " [ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
fi
}
info (){
if $terminal && ! $QUIET_MODE ; then
echo ""
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
fi
}
echo_ok() {
if $terminal && ! $QUIET_MODE ; then
echo -e "\033[80G[ \033[32mok\033[m ]"
fi
}
echo_failed(){
if $terminal && ! $QUIET_MODE ; then
echo -e "\033[80G[ \033[1;31mfailed\033[m ]"
fi
}
echo_skipped() {
if $terminal && ! $QUIET_MODE ; then
echo -e "\033[80G[ \033[37mskipped\033[m ]"
fi
}
trap clean_up SIGHUP SIGINT SIGTERM
# -------------
# - Read Commandline Parameters
# -------------
while getopts d:hIp:qu: opt ; do
case $opt in
d) DATABASE_NAME="$OPTARG"
DATABASE_NAME_NEEDED=false
;;
I) NON_INTERACTIVE_MODE=true
;;
p) DATABASE_PASSWD="$OPTARG"
DATABASE_PASSWD_NEEDED=false
;;
q) QUIET_MODE=true
NON_INTERACTIVE_MODE=true
;;
u) DATABASE_USER="$OPTARG"
DATABASE_USER_NEEDED=false
;;
h) usage
;;
*) usage
esac
done
shift $(expr $OPTIND - 1)
[[ "$#" -gt 0 ]] && usage "Wrong number of arguments given!"
# - If not running in a terminal, be silent and non-interactive
# -
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
QUIET_MODE=true
NON_INTERACTIVE_MODE=true
fi
if [[ -z "$DATABASE_NAME" ]] ; then
DATABASE_USER_NEEDED=true
DATABASE_PASSWD_NEEDED=true
fi
if $NON_INTERACTIVE_MODE && [[ -z "$DATABASE_NAME" ]]; then
fatal "In quiet mode at least the database name must ibe given on th command line!"
fi
if [[ -n "$DATABASE_NAME" ]] ; then
if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then
if [[ -f "${working_dir}/databases/$DATABASE_NAME" ]]; then
while read -r _db_name _db_user _db_pass ; do
# - if var '_db_name' begins with '#', that means the readed line
# - is a comment line, then empty this variable
# -
# -
_db_name="$(echo $_db_name | grep -vE "^\s*#")"
# - Do not consider empty lines or commented lines
# -
[[ -z "$_db_name" ]] && continue
if [[ "$_db_name" = "$DATABASE_NAME" ]]; then
[[ -z "$DATABASE_USER" ]] && DATABASE_USER="$_db_user"
[[ -z "$DATABASE_PASSWD" ]] && DATABASE_PASSWD="$_db_pass"
break
fi
done < "${working_dir}/databases/$DATABASE_NAME"
fi
fi
fi
if $NON_INTERACTIVE_MODE ; then
if [[ -z "$DATABASE_USER" ]]; then
fatal "Database user not given. Maybe missing or wrong file '${working_dir}/databases/$DATABASE_NAME'."
fi
if [[ -z "$DATABASE_PASSWD" ]]; then
fatal "Database user's password nit given. Maybe missing or wrong file '${working_dir}/databases/$DATABASE_NAME'."
fi
fi
# -------------
# - Load Settings from configuration file create_drop_database.conf
# -------------
if ! $QUIET_MODE ; then
echo ""
fi
echononl " Loading configuration settings from $(basename ${conf_file}).."
if [[ -f "$conf_file" ]]; then
source "$conf_file" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_skipped
warn "No Configuration File found. Loading defaults.."
fi
[[ -n "$MYSQL_CREDENTIAL_ARGS" ]] || MYSQL_CREDENTIAL_ARGS="$DEFAULT_MYSQL_CREDENTIAL_ARGS"
#[[ -n "$ACTION" ]] || ACTION="$DEFAULT_ACTION"
if ! $NON_INTERACTIVE_MODE ; then
clear
if $DATABASE_NAME_NEEDED ; then
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert Database name which should be created.."
echo ""
echo ""
echononl "Database name: "
read DATABASE_NAME
while [ "X$DATABASE_NAME" = "X" ] ; do
echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n"
echononl "Database name: "
read DATABASE_NAME
done
fi
if $DATABASE_USER_NEEDED ; then
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert Database user who will grant full access to database '${DATABASE_NAME}'.."
echo ""
echo ""
if [ -z "$DATABASE_USER" ]; then
echononl "Database user for database '${DATABASE_NAME}': "
read DATABASE_USER
while [ "X$DATABASE_USER" = "X" ] ; do
echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n"
echononl "Database user for database '${DATABASE_NAME}': "
read DATABASE_USER
done
else
_DATABASE_USER="$DATABASE_USER"
echononl "Database user for database '${DATABASE_NAME}' [${_DATABASE_USER}]: "
read DATABASE_USER
if [[ "X$DATABASE_USER" = "X" ]]; then
DATABASE_USER=$_DATABASE_USER
fi
fi
fi
if $DATABASE_PASSWD_NEEDED ; then
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert password for Database user '${DATABASE_USER}'.."
echo ""
echo ""
if [ -z "$DATABASE_PASSWD" ]; then
echononl "Password for Database user '${DATABASE_USER}': "
read DATABASE_PASSWD
while [ "X$DATABASE_PASSWD" = "X" ] ; do
echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n"
echononl "Password for Database user '${DATABASE_USER}': "
read DATABASE_PASSWD
done
else
_DATABASE_PASSWD="$DATABASE_PASSWD"
echononl "Password for Database user '${DATABASE_USER}' [${_DATABASE_PASSWD}]: "
read DATABASE_PASSWD
if [[ "X$DATABASE_PASSWD" = "X" ]]; then
DATABASE_PASSWD=$_DATABASE_PASSWD
fi
fi
fi
fi
if ! $QUIET_MODE ; then
echo ""
echo ""
echo -e "\033[32m\033[1m====================\033[m"
echo "Create MySQL Database settings"
echo -e "\033[32m\033[1m====================\033[m"
echo ""
echo " Database name................: $DATABASE_NAME"
echo " Database user................: $DATABASE_USER"
echo " Database password............: $DATABASE_PASSWD"
echo ""
echo ""
fi
if ! $NON_INTERACTIVE_MODE ; then
echo -e -n "\033[1mContinue creating database with above settings? [y/n]:\033[m "
read OK
while [[ "X${OK}X" = "XX" ]] ; do
echo ""
echo -e -n "\033[1mContinue with above settings? [y/n]:\033[m "
read OK
done
if [[ "${OK,,}" != 'yes' ]] && [[ "${OK,,}" != 'y' ]]; then
fatal "Abort by user request."
fi
fi
if ! $QUIET_MODE ; then
echo ""
echo ""
echo -e " Processing database \033[1m$DATABASE_NAME\033[m"
echo ""
fi
# - Test if Database already exists
# -
_result="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SHOW DATABASES LIKE '$DATABASE_NAME'")"
if [[ "$_result" = "$DATABASE_NAME" ]] ; then
fatal "Database '$DATABASE_NAME' already exists"
fi
echononl " Create database \033[1m$DATABASE_NAME\033[m"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE DATABASE $DATABASE_NAME CHARACTER SET utf8 COLLATE utf8_general_ci" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
else
echo_ok
fi
echononl " Grant full access to user '$DATABASE_USER' on Database '$DATABASE_NAME'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
else
echo_ok
fi
echononl " Also grant 'Super_priv' privilege to '$DATABASE_USER' on Database '$DATABASE_NAME'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
"USE mysql; UPDATE user SET Super_priv = 'Y' WHERE User = '$DATABASE_USER'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
else
echo_ok
fi
echononl " Flush Privileges.."
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
else
echo_ok
fi
echononl " Create/Renew file '${working_dir}/databases/$DATABASE_NAME'"
cat <<EOF > "${working_dir}/databases/$DATABASE_NAME" 2> $tmp_log_file 2>&1
# <db-name> <db-user> <dp-pass>
$DATABASE_NAME $DATABASE_USER $DATABASE_PASSWD
EOF
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
else
echo_ok
fi
if ! $QUIET_MODE ; then
echo ""
fi
clean_up 0

423
drop_database.sh Executable file
View File

@ -0,0 +1,423 @@
#!/usr/bin/env bash
working_dir="$(dirname $(realpath $0))"
log_dir="${working_dir}/log"
conf_file="${working_dir}/conf/create_drop_database.conf"
tmp_log_file="$(mktemp)"
# -------------
# - Variable settings
# -------------
#DEFAULT_ACTION='create'
DEFAULT_MYSQL_CREDENTIAL_ARGS="--login-path=local"
DATABASE_NAME=""
DATABASE_USER=""
DATABASE_NAME_NEEDED=true
DATABASE_USER_NEEDED=true
DO_NOT_DELTE_USER=false
QUIET_MODE=false
NON_INTERACTIVE_MODE=false
# -------------
# --- Some functions
# -------------
usage() {
echo
[ -n "$1" ] && echo -e "Error: $1\n"
cat<<EOF
Usage: $(basename $0) [Options]
Script drops an existing MySQL database. The database name is either given on
command line using parameter '-d' or will be asked for otherwise.
If a user is given or
Options:
-d <db-name>
The name of the domain, which is requested for deletion. If not set, script
will ask for the database name.
-I
Non-interactive mode. Script will act in non-interactice mode. At least the
database name must be given using parameter '-d'. Default is acting in
interactive mode
-q
Be quiet. Setting this parameter implies non-interactive mode (parameter -I). So
if '-q' is set, at least the database name must be given using parameter '-d'.
-u <db-user>
The name of the database user who will be granted full access to the given
database. If not set, script looks for a file 'databases/<db-name>'. If
found, script reads database settings (user/password) from that file. If not
running in quiet mode, you will be asked to confirm or set the db-user.
-U
No User will be deleted from table mysql.user.
EOF
clean_up 1
}
clean_up() {
# Perform program exit housekeeping
rm -f $tmp_log_file
exit $1
}
fatal(){
echo ""
if $terminal ; then
if [[ -n "$*" ]] ; then
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
else
echo " \033[31m\033[1mFatal error\033[m:"
fi
echo ""
echo -e " \033[31m\033[1mScript will be interrupted.\033[m\033[m"
else
if [[ -n "$*" ]] ; then
echo " [ Fatal ]: $*"
else
echo " Fatal error:"
fi
echo ""
echo " Script was terminated...."
fi
echo ""
clean_up 1
}
echononl(){
if $terminal && ! $QUIET_MODE ; then
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
fi
}
error(){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mError\033[m ]: $*"
else
echo "Error: $*"
fi
echo ""
}
warn (){
if $terminal && ! $QUIET_MODE ; then
echo ""
echo -e " [ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
fi
}
info (){
if $terminal && ! $QUIET_MODE ; then
echo ""
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
fi
}
echo_ok() {
if $terminal && ! $QUIET_MODE ; then
echo -e "\033[80G[ \033[32mok\033[m ]"
fi
}
echo_failed(){
if $terminal && ! $QUIET_MODE ; then
echo -e "\033[80G[ \033[1;31mfailed\033[m ]"
fi
}
echo_skipped() {
if $terminal && ! $QUIET_MODE ; then
echo -e "\033[80G[ \033[37mskipped\033[m ]"
fi
}
trap clean_up SIGHUP SIGINT SIGTERM
# -------------
# - Read Commandline Parameters
# -------------
while getopts d:hIqu:U opt ; do
case $opt in
d) DATABASE_NAME="$OPTARG"
DATABASE_NAME_NEEDED=false
;;
I) NON_INTERACTIVE_MODE=true
;;
q) QUIET_MODE=true
NON_INTERACTIVE_MODE=true
;;
u) DATABASE_USER="$OPTARG"
DATABASE_USER_NEEDED=false
;;
U) DO_NOT_DELTE_USER=true
DATABASE_USER_NEEDED=false
;;
h) usage
;;
*) usage
esac
done
shift $(expr $OPTIND - 1)
[[ "$#" -gt 0 ]] && usage "Wrong number of arguments given!"
# - If not running in a terminal, be silent and non-interactive
# -
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
QUIET_MODE=true
NON_INTERACTIVE_MODE=true
fi
if [[ -z "$DATABASE_NAME" ]] ; then
DATABASE_USER_NEEDED=true
DATABASE_PASSWD_NEEDED=true
fi
if $NON_INTERACTIVE_MODE && [[ -z "$DATABASE_NAME" ]]; then
fatal "In quiet mode at least the database name must ibe given on th command line!"
fi
if [[ -n "$DATABASE_NAME" ]] ; then
if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then
if [[ -f "${working_dir}/databases/$DATABASE_NAME" ]]; then
while read -r _db_name _db_user _db_pass ; do
# - if var '_db_name' begins with '#', that means the readed line
# - is a comment line, then empty this variable
# -
# -
_db_name="$(echo $_db_name | grep -vE "^\s*#")"
# - Do not consider empty lines or commented lines
# -
[[ -z "$_db_name" ]] && continue
if [[ "$_db_name" = "$DATABASE_NAME" ]]; then
[[ -z "$DATABASE_USER" ]] && DATABASE_USER="$_db_user"
[[ -z "$DATABASE_PASSWD" ]] && DATABASE_PASSWD="$_db_pass"
break
fi
done < "${working_dir}/databases/$DATABASE_NAME"
fi
fi
fi
if $NON_INTERACTIVE_MODE ; then
if [[ -z "$DATABASE_USER" ]]; then
fatal "Database user not given. Maybe missing or wrong file '${working_dir}/databases/$DATABASE_NAME'."
fi
if [[ -z "$DATABASE_PASSWD" ]]; then
fatal "Database user's password nit given. Maybe missing or wrong file '${working_dir}/databases/$DATABASE_NAME'."
fi
fi
# -------------
# - Load Settings from configuration file create_drop_database.conf
# -------------
if ! $QUIET_MODE ; then
echo ""
fi
echononl " Loading configuration settings from $(basename ${conf_file}).."
if [[ -f "$conf_file" ]]; then
source "$conf_file" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_skipped
warn "No Configuration File found. Loading defaults.."
fi
[[ -n "$MYSQL_CREDENTIAL_ARGS" ]] || MYSQL_CREDENTIAL_ARGS="$DEFAULT_MYSQL_CREDENTIAL_ARGS"
#[[ -n "$ACTION" ]] || ACTION="$DEFAULT_ACTION"
if ! $NON_INTERACTIVE_MODE ; then
clear
if $DATABASE_NAME_NEEDED ; then
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert Database name which should be created.."
echo ""
echo ""
echononl "Database name: "
read DATABASE_NAME
while [ "X$DATABASE_NAME" = "X" ] ; do
echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n"
echononl "Database name: "
read DATABASE_NAME
done
fi
if $DATABASE_USER_NEEDED ; then
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert Database user who will grant full access to database '${DATABASE_NAME}'.."
echo ""
echo -e "Type \"\033[33mNone\033[m\" if no Database user should be deleted"
echo ""
echo ""
if [ -z "$DATABASE_USER" ]; then
echononl "Database user for database '${DATABASE_NAME}': "
read DATABASE_USER
while [ "X$DATABASE_USER" = "X" ] ; do
echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n"
echononl "Database user for database '${DATABASE_NAME}': "
read DATABASE_USER
done
else
_DATABASE_USER="$DATABASE_USER"
echononl "Database user for database '${DATABASE_NAME}' [${_DATABASE_USER}]: "
read DATABASE_USER
if [[ "X$DATABASE_USER" = "X" ]]; then
DATABASE_USER=$_DATABASE_USER
fi
fi
fi
if [[ "${DATABASE_USER,,}" = "none" ]]; then
DO_NOT_DELTE_USER=true
fi
fi
if ! $QUIET_MODE ; then
echo ""
echo ""
echo -e "\033[32m\033[1m====================\033[m"
echo "Drop MySQL Database settings"
echo -e "\033[32m\033[1m====================\033[m"
echo ""
echo " Database name................: $DATABASE_NAME"
if $DO_NOT_DELTE_USER ; then
echo -e " Database user................: \033[33mNone\033[m"
else
echo " Database user................: $DATABASE_USER"
fi
echo ""
echo ""
fi
if ! $NON_INTERACTIVE_MODE ; then
info "To Continue dropping database \033[1m$DATABASE_NAME\033[m type uppercase 'YES'"
echo -e -n "\033[1mContinue:\033[m "
read OK
if [[ "$OK" != "YES" ]] ; then
fatal "Abort by user request - Answer as not 'YES'"
fi
fi
if ! $QUIET_MODE ; then
echo ""
echo ""
echo -e " Processing database \033[1m$DATABASE_NAME\033[m"
echo ""
fi
echononl " Drop database '$DATABASE_NAME'.."
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "DROP DATABASE $DATABASE_NAME" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
else
echo_ok
fi
echononl " Delete all entries for database '$DATABASE_NAME' from table mysql.db .."
mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"DELETE FROM db WHERE Db = '$DATABASE_NAME'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
else
echo_ok
fi
# - Test if given user is associated with another database
# -
echononl " Check if db user '$DATABASE_USER' is also associated with another database"
_count_user="$(mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"SELECT count(User) FROM db WHERE User = '$DATABASE_USER'" 2> $tmp_log_file)"
if [[ -z "$_count_user" ]]; then
echo_failed
error $(cat "$tmp_log_file")
else
echo_ok
if [[ $_count_user -gt 0 ]]; then
warn "Db user '$DATABASE_USER' is already in use.\n So user will not be deleted from table 'mysql.user'."
else
echononl " Delete username '$DATABASE_USER' from table mysql.user"
mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"DELETE FROM user WHERE User = '$DATABASE_USER'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
else
echo_ok
fi
fi
fi
echononl " Flush Privileges.."
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
else
echo_ok
fi
if ! $QUIET_MODE ; then
echo ""
fi
clean_up 0

View File

@ -0,0 +1,14 @@
#!/usr/bin/env bash
src_dir=/root
dst_dir="/root/bin/mysql/databases"
while IFS='' read -r -d '' _file || [[ -n $_file ]] ; do
mv "$_file" "${dst_dir}/${_file##*.}"
done < <(find "$src_dir" -maxdepth 1 -type f -name "databases.txt.*" -print0)
#while read -r _file ; do
# mv "$_file" "${dst_dir}/${_file##*.}"
#done < <(find "$src_dir" -maxdepth 1 -type f -name "databases.txt.*")
exit 0

34
utils/recreate_databases.sh Executable file
View File

@ -0,0 +1,34 @@
#!/usr/bin/env bash
mysql_credential_args="--login-path=local"
echo ""
while read _db_name _db_user _db_pass ; do
echo -e "\tprocessing database \"$_db_name\""
mysql $mysql_credential_args -N -s -e \
"DROP DATABASE $_db_name"
mysql $mysql_credential_args mysql -N -s -e \
"DELETE FROM db WHERE Db = '$_db_name'"
echo -e "\t database \"$_db_name\" deleted."
mysql $mysql_credential_args -N -s -e \
"CREATE DATABASE IF NOT EXISTS $_db_name CHARACTER SET utf8 COLLATE utf8_general_ci"
mysql $mysql_credential_args -N -s -e \
"GRANT ALL ON $_db_name.* TO '$_db_user'@'localhost' IDENTIFIED BY '$_db_pass'"
mysql $mysql_credential_args -N -s -e \
"USE mysql; UPDATE user SET Super_priv = 'Y' WHERE User = '$_db_user'"
echo -e "\t database \"$_db_name\" recreated"
done < databases.txt
echo ""
mysql $mysql_credential_args -N -s -e "FLUSH PRIVILEGES"
exit 0