ansible-roles/mysql/files/mysql_skip.sh
Jérémy Lecour 8706a35705
All checks were successful
Ansible Lint |Total|New|Outstanding|Fixed|Trend |:-:|:-:|:-:|:-:|:-: |2776|4|2772|5|:+1: Reference build: <a href="https://jenkins.evolix.org/job/gitea/job/ansible-roles/job/unstable/254//ansiblelint">Evolix » ansible-roles » unstable #254</a>
gitea/ansible-roles/pipeline/head This commit looks good
mysql: improve shell syntax for mysql_skip script
2023-05-22 14:16:50 +02:00

47 lines
1.3 KiB
Bash

#!/bin/sh
# File containing error messages to skip (one per line).
error_messages="/etc/mysql_skip.conf"
# Sleep interval between 2 check.
sleep_interval="1"
# Exit when Seconds_Behind_Master reached 0.
exit_when_uptodate="false"
# Options to pass to mysql.
#mysql_opt="-P 3307"
# File to log skipped queries to (leave empty for no logs).
log_file="/var/log/mysql_skip.log"
mysql_skip_error() {
error="$1"
mysql ${mysql_opt} -e 'SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE;'
printf 'Skipping: %s\n' "$error"
[ -n "$log_file" ] && printf '%s Skipping: %s\n' "$(date --iso-8601=seconds)" "$error" >>"$log_file"
}
while true; do
slave_status="$(mysql ${mysql_opt} -e 'SHOW SLAVE STATUS\G')"
seconds_behind_master=$(echo "${slave_status}" |grep 'Seconds_Behind_Master: ' |awk -F ' ' '{print $2}')
last_SQL_error="$(echo "${slave_status}" |grep 'Last_SQL_Error: ' |sed 's/^.\+Last_SQL_Error: //')"
if [ "${seconds_behind_master}" = "0" ]; then
#printf 'Replication is up to date!\n'
if [ "${exit_when_uptodate}" = "true" ]; then
exit 0
fi
elif [ -z "$last_SQL_error" ]; then
sleep ${sleep_interval}
elif echo "$last_SQL_error" |grep -q -f $error_messages; then
mysql_skip_error "${last_SQL_error}"
fi
sleep 1
done