Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
| mysql_backups_using_replication [13.11.2025 08:56] – Pascal Suter | mysql_backups_using_replication [13.11.2025 09:20] (current) – [Backup and Monitoring] Pascal Suter | ||
|---|---|---|---|
| Line 322: | Line 322: | ||
| 0 */2 * * * / | 0 */2 * * * / | ||
| add the dbbackup.log to your logrotate config | add the dbbackup.log to your logrotate config | ||
| + | |||
| + | ===== replica check script ===== | ||
| + | here is a script to quickly check if the replica is working and up to date.. useful when messing around with the config for example or for monitoring purposes other than through the bakcup script: | ||
| + | |||
| + | <code bash checkReplica.sh> | ||
| + | # | ||
| + | set -euo pipefail | ||
| + | |||
| + | ######################################## | ||
| + | # Settings | ||
| + | ######################################## | ||
| + | |||
| + | # Directory where your docker-compose.yml lives | ||
| + | COMPOSE_DIR="/ | ||
| + | |||
| + | # Name of the replica service in docker-compose | ||
| + | REPLICA_SERVICE=" | ||
| + | |||
| + | # Monitoring DB user (created on PRIMARY, replicated to replica) | ||
| + | MONITOR_USER=" | ||
| + | MONITOR_PASSWORD=" | ||
| + | |||
| + | ######################################## | ||
| + | # Colors | ||
| + | ######################################## | ||
| + | |||
| + | if [ -t 1 ]; then | ||
| + | RED=" | ||
| + | GREEN=" | ||
| + | YELLOW=" | ||
| + | BOLD=" | ||
| + | RESET=" | ||
| + | else | ||
| + | RED=""; | ||
| + | fi | ||
| + | |||
| + | ######################################## | ||
| + | # Helper | ||
| + | ######################################## | ||
| + | |||
| + | # Extract " | ||
| + | # " | ||
| + | get_field() { | ||
| + | local key=" | ||
| + | printf ' | ||
| + | | sed -n " | ||
| + | | head -n1 | ||
| + | } | ||
| + | |||
| + | ######################################## | ||
| + | # Main | ||
| + | ######################################## | ||
| + | |||
| + | cd " | ||
| + | |||
| + | # Try SHOW REPLICA STATUS first (MySQL 8+), fall back to SHOW SLAVE STATUS (older) | ||
| + | STATUS=$(docker-compose -f " | ||
| + | mysql -u" | ||
| + | |||
| + | if [[ -z " | ||
| + | STATUS=$(docker-compose -f " | ||
| + | mysql -u" | ||
| + | fi | ||
| + | |||
| + | if [[ -z " | ||
| + | echo " | ||
| + | echo " | ||
| + | echo " | ||
| + | exit 1 | ||
| + | fi | ||
| + | |||
| + | # IO / SQL thread status (new names first, fall back to old) | ||
| + | IO_Running=$(get_field " | ||
| + | if [[ -z " | ||
| + | IO_Running=$(get_field " | ||
| + | fi | ||
| + | |||
| + | SQL_Running=$(get_field " | ||
| + | if [[ -z " | ||
| + | SQL_Running=$(get_field " | ||
| + | fi | ||
| + | |||
| + | # Seconds behind (new name first, fall back) | ||
| + | Seconds_Behind=$(get_field " | ||
| + | if [[ -z " | ||
| + | Seconds_Behind=$(get_field " | ||
| + | fi | ||
| + | |||
| + | # GTID sets – allow indent before the key | ||
| + | Executed_Gtid_Set=$( | ||
| + | printf ' | ||
| + | | sed -n ' | ||
| + | | head -n1 | ||
| + | ) | ||
| + | Retrieved_Gtid_Set=$( | ||
| + | printf ' | ||
| + | | sed -n ' | ||
| + | | head -n1 | ||
| + | ) | ||
| + | |||
| + | # Errors (names are the same on old/new) | ||
| + | Last_IO_Error=$(get_field " | ||
| + | Last_SQL_Error=$(get_field " | ||
| + | |||
| + | ######################################## | ||
| + | # Determine overall status | ||
| + | ######################################## | ||
| + | |||
| + | overall_color=" | ||
| + | overall_text=" | ||
| + | |||
| + | if [[ " | ||
| + | overall_color=" | ||
| + | overall_text=" | ||
| + | elif [[ -z " | ||
| + | overall_color=" | ||
| + | overall_text=" | ||
| + | elif [[ " | ||
| + | # More than 5 minutes behind -> warn | ||
| + | overall_color=" | ||
| + | overall_text=" | ||
| + | fi | ||
| + | |||
| + | ######################################## | ||
| + | # Output | ||
| + | ######################################## | ||
| + | |||
| + | echo | ||
| + | echo " | ||
| + | echo " | ||
| + | echo | ||
| + | |||
| + | echo " | ||
| + | echo " | ||
| + | |||
| + | if [[ -z " | ||
| + | echo " | ||
| + | else | ||
| + | echo " | ||
| + | fi | ||
| + | |||
| + | echo | ||
| + | echo " | ||
| + | echo " | ||
| + | echo " | ||
| + | echo " | ||
| + | echo | ||
| + | |||
| + | if [[ -n " | ||
| + | echo " | ||
| + | echo " | ||
| + | echo | ||
| + | fi | ||
| + | |||
| + | if [[ -n " | ||
| + | echo " | ||
| + | echo " | ||
| + | echo | ||
| + | fi | ||
| + | </ | ||
| + | |||