From 06e41dbd3068566f98b425f73ccd4076a164d5e8 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 7 Jan 2024 02:22:41 +0200 Subject: [PATCH] Fixed couple bugs and upgraded the scripts --- .gitignore | 1 + Makefile | 7 +++++++ backup-mysql.sh | 16 ++++++++++++++-- cleanup-empty-files.sh | 24 ++++++++++++++++++++++++ restore-mysql.sh | 18 +++++++++++++++--- 5 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 Makefile create mode 100755 cleanup-empty-files.sh diff --git a/.gitignore b/.gitignore index ea1472e..2132813 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ output/ +.env diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5c05aae --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +all: + echo OK +backup: + bash backup-mysql.sh + +cleanup-empty-files: + bash cleanup-empty-files.sh diff --git a/backup-mysql.sh b/backup-mysql.sh index af0ca0a..2e67848 100755 --- a/backup-mysql.sh +++ b/backup-mysql.sh @@ -2,13 +2,23 @@ USER="root" PASSWORD="secretpass" -GZIP="1" +GZIP_ENABLED="1" OUTPUT_DIR="./output" HOSTNAME="127.0.0.1" USE_PASSWORD="0" USE_HOSTNAME="0" +# automatically export all variables +set -a +source .env +set +a + +if [ "${DEBUG}" -gt "0" ] +then + set -x +fi + PASSWORD_OPTIONS="-p${PASSWORD}" HOST_OPTIONS="-h ${HOSTNAME}" @@ -32,7 +42,7 @@ for db in ${databases}; do mkdir -vp "${DUMP_DIR}" echo "Dumping database: ${db} into: ${DUMP_FILENAME}" - if [ "${GZIP}" -gt "0" ] + if [ "${GZIP_ENABLED}" -gt "0" ] then mysqldump ${HOST_OPTIONS} -u ${USER} ${PASSWORD_OPTIONS} --default-character-set=utf8mb4 --routines=true --triggers=true --databases ${db} | gzip > "${DUMP_FILENAME}.gz" else @@ -40,3 +50,5 @@ for db in ${databases}; do fi fi done + +set +x diff --git a/cleanup-empty-files.sh b/cleanup-empty-files.sh new file mode 100755 index 0000000..d42c5da --- /dev/null +++ b/cleanup-empty-files.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +USER="root" +PASSWORD="secretpass" +GZIP_ENABLED="1" +OUTPUT_DIR="./output" +HOSTNAME="127.0.0.1" + +USE_PASSWORD="0" +USE_HOSTNAME="0" + +# automatically export all variables +set -a +source .env +set +a + +if [ "${DEBUG}" -gt "0" ] +then + set -x +fi + +find "${OUTPUT_DIR}" -type f -size 0 -exec rm -v {} \; + +set +x diff --git a/restore-mysql.sh b/restore-mysql.sh index 7e710da..707dde2 100755 --- a/restore-mysql.sh +++ b/restore-mysql.sh @@ -4,10 +4,20 @@ USER="root" PASSWORD="secretpass" OUTPUT_DIR="./output" HOSTNAME="127.0.0.1" +GZIP_ENABLED="1" USE_PASSWORD="0" USE_HOSTNAME="0" +set -a # automatically export all variables +source .env +set +a + +if [ "${DEBUG}" -gt "0" ] +then + set -x +fi + PASSWORD_OPTIONS="-p${PASSWORD}" HOST_OPTIONS="-h ${HOSTNAME}" @@ -34,16 +44,16 @@ for db in ${databases}; do file "${DUMP_FILENAME}" | grep "gzip compressed data" >/dev/null if [ "$?" -eq "0" ] then - GZIP="1" + GZIP_ENABLED="1" else - GZIP="0" + GZIP_ENABLED="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" ] + if [ "${GZIP_ENABLED}" -gt "0" ] then gunzip "${DUMP_FILENAME}" | mysql ${HOST_OPTIONS} -u ${USER} ${PASSWORD_OPTIONS} --default-character-set=utf8mb4 ${db} else @@ -51,3 +61,5 @@ for db in ${databases}; do fi fi done + +set +x