From 0e9148d6a4f286c7a069203bdb91131b0eb9eeb2 Mon Sep 17 00:00:00 2001 From: Christoph Date: Sun, 5 May 2019 04:07:21 +0200 Subject: [PATCH] restore_nextcloud.sh: drop database tables befor restoring it. --- restore_nextcloud.sh | 53 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/restore_nextcloud.sh b/restore_nextcloud.sh index 9a94e27..28c91fd 100755 --- a/restore_nextcloud.sh +++ b/restore_nextcloud.sh @@ -5,7 +5,7 @@ script_dir="$(dirname $(realpath $0))" conf_dir="${script_dir}/conf" -declare -a unsorted_website_arr+ +declare -a unsorted_website_arr declare -a website_arr log_file="$(mktemp)" @@ -202,7 +202,7 @@ else terminal=false fi -clear +#clear echo "" echo -e "\033[32m-----\033[m" @@ -512,6 +512,55 @@ elif [[ "$DATABASE_TYPE" = 'postgres' ]]; then fi +if [[ "$DATABASE_TYPE" = 'mysql' ]]; then + + echononl " Get table list from database '$DATABASE_NAME' .." + _TABLES="$(mysql $MYSQL_CREDENTIALS $DATABASE_NAME -N -s -e "show tables" 2> $log_file)" + if [[ $? -ne 0 ]] ; then + echo_failed + fatal "$(cat $log_file)" + else + echo_ok + fi + + if [[ -z "$_TABLES" ]]; then + + warn "No tables found in database '$DATABASE_NAME'." + + else + + echononl " Drop tables from database '$DATABASE_NAME' .." + _failed=false + > $log_file + for _table in $_TABLES ; do + mysql $MYSQL_CREDENTIALS $DATABASE_NAME -N -s -e "DROP TABLE \`$_table\`" >> $log_file 2>&1 + if [[ $? -ne 0 ]] ; then + _failed=true + _tables_not_deleted+=("$_table") + fi + done + if $_failed ; then + + echo_failed + error "$(cat $log_file)" + + if [[ ${#_tables_not_deleted[@]} -gt 0 ]] ; then + echo "" + echo "Tables NOT deleted:" + for _table in "${_tables_not_deleted[@]}" ; do + echo " $_table" + done + echo "" + fi + else + echo_ok + fi + + fi + +fi + + # - Backup current data directory # - echononl " Backup current data directory"