From ce28ffd4678f430ff929555fcfb6895aeb068f4f Mon Sep 17 00:00:00 2001 From: Christoph Date: Sat, 23 Sep 2017 01:59:34 +0200 Subject: [PATCH] Change handling od database files. --- create_database.sh | 42 ++++++++++++++++++++++++-- drop_database.sh | 51 +++++++++++++++++++++++++++++++- utils/move_database.txt-files.sh | 2 ++ 3 files changed, 92 insertions(+), 3 deletions(-) diff --git a/create_database.sh b/create_database.sh index 9be50fc..43b2da3 100755 --- a/create_database.sh +++ b/create_database.sh @@ -212,7 +212,14 @@ fi if [[ -n "$DATABASE_NAME" ]] ; then if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then - if [[ -f "${working_dir}/databases/$DATABASE_NAME" ]]; then + read_file="" + if [[ -f "${working_dir}/databases/DELETED/$DATABASE_NAME" ]]; then + read_file="${working_dir}/databases/DELETED/$DATABASE_NAME" + elif [[ -f "${working_dir}/databases/$DATABASE_NAME" ]]; then + read_file="${working_dir}/databases/$DATABASE_NAME" + fi + + if [[ -n "$read_file" ]]; then while read -r _db_name _db_user _db_pass ; do # - if var '_db_name' begins with '#', that means the readed line @@ -231,11 +238,12 @@ if [[ -n "$DATABASE_NAME" ]] ; then break fi - done < "${working_dir}/databases/$DATABASE_NAME" + done < "$read_file" 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'." @@ -429,6 +437,36 @@ else echo_ok fi +if ! $QUIET_MODE ; then + echo "" +fi + +echononl " Create directory '${working_dir}/databases'.." +if [[ -d "${working_dir}/databases" ]]; then + echo_skipped +else + mkdir ${working_dir}/databases > $tmp_log_file 2>&1 + if [[ $? -ne 0 ]] ; then + echo_failed + error "$(cat $tmp_log_file)" + else + echo_ok + fi +fi + +echononl " Remove file '${working_dir}/databases/DELETED/$DATABASE_NAME'" +if [[ -f "${working_dir}/databases/DELETED/$DATABASE_NAME" ]] ; then + rm "${working_dir}/databases/DELETED/$DATABASE_NAME" > $tmp_log_file 2>&1 + if [[ $? -ne 0 ]] ; then + echo_failed + error "$(cat $tmp_log_file)" + else + echo_ok + fi +else + echo_skipped +fi + echononl " Create/Renew file '${working_dir}/databases/$DATABASE_NAME'" cat < "${working_dir}/databases/$DATABASE_NAME" 2> $tmp_log_file 2>&1 # diff --git a/drop_database.sh b/drop_database.sh index e27f7a4..20774ec 100755 --- a/drop_database.sh +++ b/drop_database.sh @@ -211,7 +211,14 @@ fi if [[ -n "$DATABASE_NAME" ]] ; then if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then + read_file="" if [[ -f "${working_dir}/databases/$DATABASE_NAME" ]]; then + read_file="${working_dir}/databases/$DATABASE_NAME" + elif [[ -f "${working_dir}/databases/DELETED/$DATABASE_NAME" ]]; then + read_file="${working_dir}/databases/DELETED/$DATABASE_NAME" + fi + + if [[ -n "$read_file" ]]; then while read -r _db_name _db_user _db_pass ; do # - if var '_db_name' begins with '#', that means the readed line @@ -230,7 +237,7 @@ if [[ -n "$DATABASE_NAME" ]] ; then break fi - done < "${working_dir}/databases/$DATABASE_NAME" + done < "$read_file" fi fi fi @@ -416,6 +423,48 @@ else echo_ok fi +if ! $QUIET_MODE ; then + echo "" +fi + +echononl " Create directory '${working_dir}/databases/DELETED'.." +if [[ -d "${working_dir}/databases/DELETED" ]]; then + echo_skipped +else + mkdir -p "${working_dir}/databases/DELETED" > $tmp_log_file 2>&1 + if [[ $? -ne 0 ]] ; then + echo_failed + error "$(cat $tmp_log_file)" + else + echo_ok + fi +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 + if [[ $? -ne 0 ]] ; then + echo_failed + error "$(cat $tmp_log_file)" + else + echo_ok + fi +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 +# +$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 "" diff --git a/utils/move_database.txt-files.sh b/utils/move_database.txt-files.sh index ac51354..c223ce5 100755 --- a/utils/move_database.txt-files.sh +++ b/utils/move_database.txt-files.sh @@ -3,6 +3,8 @@ src_dir=/root dst_dir="/root/bin/mysql/databases" +mkdir -p $dst_dir + 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)