Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| mysql_backups_using_replication [13.11.2025 08:24] – [Backup and Monitoring] Pascal Suter | mysql_backups_using_replication [13.11.2025 09:20] (current) – [Backup and Monitoring] Pascal Suter | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| in my case i had a docker-compose.yml which contains the php web app and a mysql server service. i first copied the service in the '' | in my case i had a docker-compose.yml which contains the php web app and a mysql server service. i first copied the service in the '' | ||
| - | < | + | < |
| mysql: | mysql: | ||
| # image: mysql:8.0 | # image: mysql:8.0 | ||
| Line 40: | Line 40: | ||
| for the primary server, make sure the config contains the following settings: | for the primary server, make sure the config contains the following settings: | ||
| - | < | + | < |
| [mysqld] | [mysqld] | ||
| server-id = 1 | server-id = 1 | ||
| Line 58: | Line 58: | ||
| and on the replica the settings should look like this: | and on the replica the settings should look like this: | ||
| - | < | + | < |
| [mysqld] | [mysqld] | ||
| server-id = 2 | server-id = 2 | ||
| Line 64: | Line 64: | ||
| relay_log = relay-bin | relay_log = relay-bin | ||
| - | # keep binlog | + | # disable binary logging as we don't need this on the replica |
| - | log_bin = mysql-bin | + | skip-log-bin |
| - | binlog_format = ROW | + | |
| + | # GTID functionality is needed for replication | ||
| gtid_mode = ON | gtid_mode = ON | ||
| enforce_gtid_consistency = ON | enforce_gtid_consistency = ON | ||
| - | log_slave_updates = ON | ||
| # make it read-only for safety | # make it read-only for safety | ||
| Line 77: | Line 76: | ||
| # don't auto-start replication until we finish setup | # don't auto-start replication until we finish setup | ||
| + | # then comment this out | ||
| skip_slave_start = ON | skip_slave_start = ON | ||
| </ | </ | ||
| 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 | ||
| + | </ | ||
| + | |||