Only one configuration file for all database creation/drop scripts.

This commit is contained in:
Christoph 2019-11-05 15:14:58 +01:00
parent 7894f27be1
commit aa98cc0fbc
10 changed files with 129 additions and 200 deletions

View File

@ -1,45 +0,0 @@
## ===================================================================
## - 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"
# -
# - # MariaDB 10.4.x
# - MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock"
# -
# - Defaults to MYSQL_CREDENTIAL_ARGS="--login-path=local"
# -
#MYSQL_CREDENTIAL_ARGS=""

View File

@ -1,37 +0,0 @@
## ===================================================================
## - Configuration File for "drop_tables.sh" Script
## ===================================================================
# - 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"
# -
# - # MariaDB 10.4.x
# - MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock"
# -
# - Defaults to MYSQL_CREDENTIAL_ARGS="--login-path=local"
# -
#MYSQL_CREDENTIAL_ARGS=""

View File

@ -63,3 +63,10 @@
# - further relevance .
# -
#mysql_credential_args_arr=""
# - log_file - only available for optimize_tables script
# -
# - Defaults to "/var/log/${script_name%%.*}.log"
# -
#log_file="/var/log/optimize_mysql_tables.log"

View File

@ -1,78 +0,0 @@
# ----------------------------------------------------
# ---
# - Parameter Settings for Script 'optimize_mysql_tables.sh'
# ---
# ----------------------------------------------------
# - MySQL / MariaDB / Percona credentials
# - mysql_credential_args
# -
# - 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 Version 5.6, that method is considered as insecure.
# - To avoid giving the password on command line, we use an
# - encrypted option 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"
# -
# - # MariaDB 10.4.x
# - mysql_credential_args="-u root -S /tmp/mysql.sock"
# -
# - Defaults to mysql_credential_args="--login-path=local"
# -
#mysql_credential_args="--login-path=local"
# - mysql_credential_args_arr
# -
# - If multiple MySQL / MariaDB / Percona Installations are present, for each installation,
# - you can give its own credentials.
# -
# - Create (encrypted) option 2 files (Example):
# - $ mysql_config_editor set --login-path=local --socket=/tmp/mysql.sock --user=root --password
# - $ Password:
# -
# - $ mysql_config_editor set --login-path=local-5.6 --socket=/tmp/mysql-5.6.sock --user=root --password
# - $ Password:
# -
# - Note:
# - If this parameter ist present, the parameter 'mysql_credential_args' will be ignored.
# -
# - Examples:
# - mysql_credential_args_arr=("MariaDB 10.2.13:--defaults-file=/etc/mysql/debian.cnf "5.7:--login-path=local")
# - msql_credential_args_arr=("5.6:--login-path=local-5.6" "5.7:--login-path=local")
# - mysql_credential_args_arr=(
# - "5.7:--login-path=local"
# - "5.6:--login-path=mysql-5.6"
# - "8.0:--login-path=mysql-8.0"
# - "MariaDB 10.3:--defaults-file=/usr/local/mariadb-10.3.12/sys-maint.cnf"
# - "MariaDB 10.4:-u root -S /tmp/mariadb-10.4.6.sock"
# - )
# -
# - Note:
# - the first value (before the colon ':') is only used for logging output and has no
# - further relevance .
# -
#mysql_credential_args_arr=""
# - log_file
# -
# - Defaults to "/var/log/optimize_mysql_tables.log"
# -
#log_file="/var/log/optimize_mysql_tables.log"

View File

@ -11,8 +11,7 @@ tmp_log_file="$(mktemp)"
# - Variable settings
# -------------
#DEFAULT_ACTION='create'
DEFAULT_MYSQL_CREDENTIAL_ARGS="--login-path=local"
DEFAULT_MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
DATABASE_NAME=""
DATABASE_USER=""
@ -271,7 +270,7 @@ clear
# -------------
# - Load Settings from configuration file create_drop_database.conf
# - Load Settings from configuration file mysql_credetials.conf
# -------------
if ! $QUIET_MODE ; then
@ -669,15 +668,28 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
echononl " Create database user '$DATABASE_USER'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echononl " Check if user '$DATABASE_USER' already exists for localhost .."
_count="$(mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = 'localhost'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then
echo_failed
error "$(cat $tmp_log_file)"
error $(cat "$tmp_log_file")
elif [[ $_count -eq 0 ]]; then
echo_ok
echononl " Create database user '$DATABASE_USER'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$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
else
echo_ok
warn "User '$DATABASE_USER' already exists for host localhost"
fi
echononl " Grant permissions to access and use the MySQL server to user '$DATABASE_USER'"
@ -706,9 +718,44 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
for _ip in $IP_ADDRESSES ; do
echononl " Check if user '$DATABASE_USER' already exists for '$_ip' .."
_count="$(mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = '$_ip'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then
echo_failed
error $(cat "$tmp_log_file")
elif [[ $_count -eq 0 ]]; then
echo_ok
echononl " Create database user '$DATABASE_USER' for '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
else
echo_ok
fi
else
echo_ok
warn "User '$DATABASE_USER' already exists for host '$_ip'"
fi
echononl " Allow access to user '$DATABASE_USER' on Database '$DATABASE_NAME' from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON `${DATABASE_NAME}`.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
"GRANT USAGE ON \`${DATABASE_NAME}\`.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
else
echo_ok
fi
echononl " Grant all privileges to user '$DATABASE_USER' on database '$DATABASE_NAME' from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL privileges ON \`$DATABASE_NAME\`.* TO '$DATABASE_USER'@'$_ip'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"

View File

@ -3,7 +3,7 @@
working_dir="$(dirname $(realpath $0))"
log_dir="${working_dir}/log"
conf_file="${working_dir}/conf/create_drop_database.conf"
conf_file="${working_dir}/conf/mysql_credetials.conf"
tmp_log_file="$(mktemp)"
@ -11,8 +11,7 @@ tmp_log_file="$(mktemp)"
# - Variable settings
# -------------
#DEFAULT_ACTION='create'
DEFAULT_MYSQL_CREDENTIAL_ARGS="--login-path=local"
DEFAULT_MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
DATABASE_NAME=""
DATABASE_USER=""
@ -255,7 +254,7 @@ fi
# -------------
# - Load Settings from configuration file create_drop_database.conf
# - Load Settings from configuration file mysql_credetials.conf
# -------------
if ! $QUIET_MODE ; then
@ -442,7 +441,7 @@ fi
echononl " Remove file '${working_dir}/databases/$DATABASE_NAME'"
if [[ -f "${working_dir}/databases/$DATABASE_NAME" ]] ; then
rm "${working_dir}/databases/$DATABASE_NAME" > $tmp_log_file 2>&1
mv "${working_dir}/databases/$DATABASE_NAME" "${working_dir}/databases/DELETED/" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
@ -453,16 +452,20 @@ else
echo_skipped
fi
echononl " Create/Renew file '${working_dir}/databases/DELETED/$DATABASE_NAME'"
cat <<EOF > "${working_dir}/databases/DELETED/$DATABASE_NAME" 2> $tmp_log_file 2>&1
echononl " Create file '${working_dir}/databases/DELETED/$DATABASE_NAME'"
if [[ -f "${working_dir}/databases/DELETED/$DATABASE_NAME" ]]; then
echo_skipped
else
cat <<EOF > "${working_dir}/databases/DELETED/$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
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
else
echo_ok
fi
fi

View File

@ -3,7 +3,7 @@
working_dir="$(dirname $(realpath $0))"
log_dir="${working_dir}/log"
conf_file="${working_dir}/conf/drop_tables.conf"
conf_file="${working_dir}/conf/mysql_credetials.conf"
tmp_log_file="$(mktemp)"
@ -11,8 +11,7 @@ tmp_log_file="$(mktemp)"
# - Variable settings
# -------------
#DEFAULT_ACTION='create'
DEFAULT_MYSQL_CREDENTIAL_ARGS="--login-path=local"
DEFAULT_MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
DATABASE_NAME=""
DATABASE_NAME_NEEDED=true
@ -183,7 +182,7 @@ fi
# -------------
# - Load Settings from configuration file create_drop_database.conf
# - Load Settings from configuration file mysql_credetials.conf
# -------------
if ! $QUIET_MODE ; then
@ -203,7 +202,7 @@ else
warn "No Configuration File found. Loading defaults.."
fi
[[ -n "$MYSQL_CREDENTIAL_ARGS" ]] || MYSQL_CREDENTIAL_ARGS="$DEFAULT_MYSQL_CREDENTIAL_ARGS"
[[ -n "$mysql_credential_args" ]] || MYSQL_CREDENTIAL_ARGS="$DEFAULT_MYSQL_CREDENTIAL_ARGS"
if ! $NON_INTERACTIVE_MODE ; then
@ -284,26 +283,55 @@ for _table in $_TABLES ; do
mysql $MYSQL_CREDENTIAL_ARGS $DATABASE_NAME -N -s -e "DROP TABLE \`$_table\`" >> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
_failed=true
_tables_not_deleted+=("$_table")
_tables_not_yet_deleted+=("$_table")
fi
done
if $_failed ; then
echo_failed
error "$(cat $tmp_log_file)"
warn "$(cat $tmp_log_file)"
if [[ ${#_tables_not_deleted[@]} -gt 0 ]] ; then
if [[ ${#_tables_not_yet_deleted[@]} -gt 0 ]] ; then
echo ""
echo "Tables are NOT deleted:"
for _table in "${_tables_not_deleted[@]}" ; do
echo " $_table"
echo " Tables are NOT YET deleted:"
for _table in "${_tables_not_yet_deleted[@]}" ; do
echo " $_table"
done
echo ""
echononl " Mybe some contrains causes the problem. Try again.."
_failed_again=false
: > $tmp_log_file
for _table in "${_tables_not_yet_deleted[@]}" ; do
mysql $MYSQL_CREDENTIAL_ARGS $DATABASE_NAME -N -s -e "DROP TABLE \`$_table\`" >> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
_failed_again=true
_tables_not_deleted+=("$_table")
fi
done
if $_failed_again ; then
echo_failed
error "$(cat $tmp_log_file)"
if [[ ${#_tables_not_deleted[@]} -gt 0 ]] ; then
echo ""
echo " Tables are NOT deleted:"
for _table in "${_tables_yet_deleted[@]}" ; do
echo " $_table"
done
echo ""
fi
else
echo_ok
info "All tables from database '$DATABASE_NAME' are now deleted."
fi
fi
else
echo_ok
fi
if ! $QUIET_MODE ; then
echo ""
fi

View File

@ -1,7 +1,9 @@
#!/usr/bin/env bash
script_name="$(basename $(realpath $0))"
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/optimize_mysql_tables.conf"
conf_file="${working_dir}/conf/mysql_credetials.conf"
tmp_log_file="$(mktemp)"
@ -9,8 +11,8 @@ tmp_log_file="$(mktemp)"
# -------------
# - Variable settings
# -------------
DEFAULT_MYSQL_CREDENTIAL_ARGS="--login-path=local"
DEFAULT_LOG_FILE="/var/log/optimize_mysql_tables.log"
DEFAULT_MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
DEFAULT_LOG_FILE="/var/log/${script_name%%.*}.log"
VERBOSE=false

View File

@ -1,7 +1,9 @@
#!/usr/bin/env bash
script_name="$(basename $(realpath $0))"
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/optimize_mysql_tables.conf"
conf_file="${working_dir}/conf/mysql_credetials.conf"
tmp_log_file="$(mktemp)"
@ -9,8 +11,8 @@ tmp_log_file="$(mktemp)"
# -------------
# - Variable settings
# -------------
DEFAULT_MYSQL_CREDENTIAL_ARGS="--login-path=local"
DEFAULT_LOG_FILE="/var/log/optimize_mysql_tables.log"
DEFAULT_MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
DEFAULT_LOG_FILE="/var/log/${script_name%%.*}.log"
VERBOSE=false

View File

@ -9,7 +9,7 @@ tmp_log_file="$(mktemp)"
# -------------
# - Variable settings
# -------------
DEFAULT_MYSQL_CREDENTIAL_ARGS="--login-path=local"
DEFAULT_MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
ALL_DATABASES=false