From d7fd02427778abf0011a2ae483655f36b0f4c118 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 26 Dec 2023 22:21:03 +0200 Subject: [PATCH] 1 --- .gitignore | 1 + backup-mysql.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 .gitignore create mode 100755 backup-mysql.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea1472e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +output/ diff --git a/backup-mysql.sh b/backup-mysql.sh new file mode 100755 index 0000000..20f1cc2 --- /dev/null +++ b/backup-mysql.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash + +USER="root" +PASSWORD="secretpass" +GZIP="1" +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=$(mysql ${HOST_OPTIONS} -u ${USER} ${PASSWORD_OPTIONS} --batch -s -r -e 'show databases') + +for db in ${databases}; do + if [[ "${db}" != "information_schema" ]] && [[ "${db}" != "performance_schema" ]] && [[ "${db}" != "mysql" ]] && [[ "${db}" != _* ]] + then + DUMP_DIR="${OUTPUT_DIR}/${db}" + DUMP_FILENAME="${DUMP_DIR}/$(date +%Y%m%d_%H:%M:%S).${db}.sql" + mkdir -vp "${DUMP_DIR}" + + echo "Dumping database: ${db} into: ${DUMP_FILENAME}" + if [ "${GZIP}" -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 + mysqldump ${HOST_OPTIONS} -u ${USER} ${PASSWORD_OPTIONS} --default-character-set=utf8mb4 --routines=true --triggers=true --databases ${db} > "${DUMP_FILENAME}" + fi + fi +done