From c6d446594db4087f046c12b9b98941e0b62449ff Mon Sep 17 00:00:00 2001 From: Eric Renfro Date: Wed, 13 Dec 2023 12:51:18 -0500 Subject: [PATCH] Added mysql_backup --- scripts/mysql_backup | 60 ++++++++++++++++++++++++++++++++++++++ templates/config/mysql.cfg | 4 +++ 2 files changed, 64 insertions(+) create mode 100644 scripts/mysql_backup create mode 100644 templates/config/mysql.cfg diff --git a/scripts/mysql_backup b/scripts/mysql_backup new file mode 100644 index 0000000..737eb4b --- /dev/null +++ b/scripts/mysql_backup @@ -0,0 +1,60 @@ +#!/bin/bash + +restoreDir=/etc/restore +mysqlDir="$restoreDir/mysql" +scriptDir="$(dirname "$0")" + +function readConfig() { + local $config + + if [[ -r "${scriptDir}/../config/mysql_backup.cfg" ]]; then + source "${scriptDir}/../config/mysql_backup.cfg" + fi +} + +function runBackups() { + local dbname + + readConfig + + if [[ ! -d "/var/backups/mysql" ]]; then + mkdir -p "/var/backups/mysql" || return 1 + fi + echo "Clearing out old MySQL backups..." + rm -f /var/backups/mysql/* + + while read dbname + do + case "$dbname" in + sys) continue;; + information_schema) continue;; + performance_schema) continue;; + esac + + echo "Backing up database: $dbname" + mysqldump --complete-insert --routines --triggers --single-transaction "$dbname" > /var/backups/mysql/"$dbname".sql + done < <(mysql -N -e 'show databases') +} + +hook_before() { + runBackups || exit $? +} + +hook_after() { + rm -rf "$mysqlDir" || exit 1 +} + +hook_fail() { + : +} + +hook_final() { + : +} + +case "$1" in + before) hook_before || exit $? ;; + after) hook_after || exit $? ;; + fail) hook_fail || exit $? ;; + finally) hook_final || exit $? ;; +esac diff --git a/templates/config/mysql.cfg b/templates/config/mysql.cfg new file mode 100644 index 0000000..13421c4 --- /dev/null +++ b/templates/config/mysql.cfg @@ -0,0 +1,4 @@ +# This configuration file is used for providing MySQL login +# credentials as needed. You can use the environment variables +# MYSQL_HOST, MYSQL_PWD, and MYSQL_HOME, for example. +# See: https://mariadb.com/kb/en/mariadb-environment-variables/