From 7b7a9cb4aedeb0c436aa82852d402dd745b41913 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 26 Dec 2023 22:40:54 +0200 Subject: [PATCH] Added restore script --- restore-mysql.sh | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 restore-mysql.sh diff --git a/restore-mysql.sh b/restore-mysql.sh new file mode 100755 index 0000000..7e710da --- /dev/null +++ b/restore-mysql.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env bash + +USER="root" +PASSWORD="secretpass" +OUTPUT_DIR="./output" +HOSTNAME="127.0.0.1" + +USE_PASSWORD="0" +USE_HOSTNAME="0" + +PASSWORD_OPTIONS="-p${PASSWORD}" +HOST_OPTIONS="-h ${HOSTNAME}" + +if [ "${USE_HOSTNAME}" -eq "0" ] +then + HOST_OPTIONS="" +fi + +if [ "${USE_PASSWORD}" -eq "0" ] +then + PASSWORD_OPTIONS="" +fi + +databases=$(ls ${OUTPUT_DIR}) + +for db in ${databases}; do + if [[ "${db}" != "information_schema" ]] && [[ "${db}" != "performance_schema" ]] && [[ "${db}" != "mysql" ]] && [[ "${db}" != _* ]] + then + GZIP="0" + DUMP_DIR="${OUTPUT_DIR}/${db}" + NEWEST_DUMP_FILENAME="$(ls -Art ${OUTPUT_DIR}/${db} | tail -n 1)" + DUMP_FILENAME="${DUMP_DIR}/${NEWEST_DUMP_FILENAME}" + + file "${DUMP_FILENAME}" | grep "gzip compressed data" >/dev/null + if [ "$?" -eq "0" ] + then + GZIP="1" + else + GZIP="0" + fi + echo "Restoring database: ${db} from: ${DUMP_FILENAME}" + echo "Trying to create the DB:" + mysql ${HOST_OPTIONS} -u ${USER} ${PASSWORD_OPTIONS} --default-character-set=utf8mb4 -e "CREATE DATABASE ${db} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" + + echo "Restoring DB from backup file" + if [ "${GZIP}" -gt "0" ] + then + gunzip "${DUMP_FILENAME}" | mysql ${HOST_OPTIONS} -u ${USER} ${PASSWORD_OPTIONS} --default-character-set=utf8mb4 ${db} + else + cat "${DUMP_FILENAME}" | mysql ${HOST_OPTIONS} -u ${USER} ${PASSWORD_OPTIONS} --default-character-set=utf8mb4 ${db} + fi + fi +done