#!/bin/bash # Script to manage MariaDB events during replication role changes. # # Author: Xavi Lleó & GhatGPT ® # Copyright (c) 2025 Verdnatura S.L. All rights reserved. # Version: 1.4.0 # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # Description: # This script toggles MariaDB events during replication role changes. # It supports three modes: # --promote : Re-enable events previously marked SLAVESIDE_DISABLED # --demote : Disable currently ENABLED events using DISABLE ON SLAVE # --status : Display current status of all events # All actions are logged to /var/log/mariadb_admin.log. set -euo pipefail LOG_FILE="/var/log/mariadb_admin.log" TIMESTAMP="$(date '+%F %T')" log() { echo "$TIMESTAMP - $1" | tee -a "$LOG_FILE" } disable_scheduler() { log "Disabling event_scheduler..." if mysql -e "SET GLOBAL event_scheduler = OFF"; then log "Successfully disabled event_scheduler" else log "ERROR: Failed to disable event_scheduler" exit 1 fi } enable_scheduler() { log "Re-enabling event_scheduler..." if mysql -e "SET GLOBAL event_scheduler = ON"; then log "Successfully re-enabled event_scheduler" else log "ERROR: Failed to re-enable event_scheduler" exit 1 fi } final_status() { log "Final event_scheduler status:" mysql --table -e "SHOW VARIABLES LIKE 'event_scheduler';" | tee -a "$LOG_FILE" log "Final state of all events:" mysql --table -e "SELECT db, name, status FROM mysql.event;" | tee -a "$LOG_FILE" } if [[ $# -ne 1 ]]; then echo "Usage: $0 --promote | --demote | --status" exit 1 fi MODE="$1" case "$MODE" in --status) log "Fetching current event_scheduler status:" mysql --table -e "SHOW VARIABLES LIKE 'event_scheduler';" | tee -a "$LOG_FILE" log "Fetching current event status:" mysql --table -e "SELECT db, name, status FROM mysql.event;" | tee -a "$LOG_FILE" ;; --promote) disable_scheduler log "Promoting: Enabling events with SLAVESIDE_DISABLED status..." mysql --raw --silent -e "SELECT db, name FROM mysql.event WHERE status = 'SLAVESIDE_DISABLED'" | \ awk '{ gsub("`", "``", $1); gsub("`", "``", $2); print "`"$1"`.`"$2"`" }' | \ while read -r event; do if mysql -e "ALTER EVENT $event ENABLE"; then log "Enabled event: $event" else log "ERROR: Failed to enable event: $event" fi done enable_scheduler final_status ;; --demote) disable_scheduler log "Demoting: Disabling events currently ENABLED..." mysql --raw --silent -e "SELECT db, name FROM mysql.event WHERE status = 'ENABLED'" | \ awk '{ gsub("`", "``", $1); gsub("`", "``", $2); print "`"$1"`.`"$2"`" }' | \ while read -r event; do if mysql -e "ALTER EVENT $event DISABLE ON SLAVE"; then log "Disabled event: $event" else log "ERROR: Failed to disable event: $event" fi done enable_scheduler final_status ;; *) echo "Invalid mode: $MODE" echo "Usage: $0 --promote | --demote | --status" exit 1 ;; esac