From aa98cc0fbc5744aa6f159dd9f8def1cb001ac3d2 Mon Sep 17 00:00:00 2001 From: Christoph Date: Tue, 5 Nov 2019 15:14:58 +0100 Subject: [PATCH] Only one configuration file for all database creation/drop scripts. --- conf/create_drop_database.conf.sample | 45 --------------- conf/drop_tables.conf.sample | 37 ------------ conf/mysql_credetials.conf.sample | 7 +++ conf/optimize_mysql_tables.conf.sample | 78 -------------------------- create_database.sh | 67 ++++++++++++++++++---- drop_database.sh | 27 +++++---- drop_tables.sh | 50 +++++++++++++---- optimize_mysql_tables-ND.sh | 8 ++- optimize_mysql_tables.sh | 8 ++- repair_mysql_tables.sh | 2 +- 10 files changed, 129 insertions(+), 200 deletions(-) delete mode 100644 conf/create_drop_database.conf.sample delete mode 100644 conf/drop_tables.conf.sample delete mode 100644 conf/optimize_mysql_tables.conf.sample diff --git a/conf/create_drop_database.conf.sample b/conf/create_drop_database.conf.sample deleted file mode 100644 index 92f7052..0000000 --- a/conf/create_drop_database.conf.sample +++ /dev/null @@ -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="" diff --git a/conf/drop_tables.conf.sample b/conf/drop_tables.conf.sample deleted file mode 100644 index 0ec976d..0000000 --- a/conf/drop_tables.conf.sample +++ /dev/null @@ -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="" diff --git a/conf/mysql_credetials.conf.sample b/conf/mysql_credetials.conf.sample index 8af3ed9..575a713 100644 --- a/conf/mysql_credetials.conf.sample +++ b/conf/mysql_credetials.conf.sample @@ -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" diff --git a/conf/optimize_mysql_tables.conf.sample b/conf/optimize_mysql_tables.conf.sample deleted file mode 100644 index 54698ea..0000000 --- a/conf/optimize_mysql_tables.conf.sample +++ /dev/null @@ -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" diff --git a/create_database.sh b/create_database.sh index 78d883a..5043c3e 100755 --- a/create_database.sh +++ b/create_database.sh @@ -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)" diff --git a/drop_database.sh b/drop_database.sh index 20774ec..f5ae73d 100755 --- a/drop_database.sh +++ b/drop_database.sh @@ -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 < "${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 < "${working_dir}/databases/DELETED/$DATABASE_NAME" 2> $tmp_log_file 2>&1 # $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 diff --git a/drop_tables.sh b/drop_tables.sh index 7d9121b..efe198d 100755 --- a/drop_tables.sh +++ b/drop_tables.sh @@ -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 diff --git a/optimize_mysql_tables-ND.sh b/optimize_mysql_tables-ND.sh index 0a2ed14..2c8d53a 100755 --- a/optimize_mysql_tables-ND.sh +++ b/optimize_mysql_tables-ND.sh @@ -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 diff --git a/optimize_mysql_tables.sh b/optimize_mysql_tables.sh index 6ca2c1f..5c5b61a 100755 --- a/optimize_mysql_tables.sh +++ b/optimize_mysql_tables.sh @@ -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 diff --git a/repair_mysql_tables.sh b/repair_mysql_tables.sh index 9953e3a..09e3c03 100755 --- a/repair_mysql_tables.sh +++ b/repair_mysql_tables.sh @@ -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