|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Copyright (c) 2016 MariaDB Corporation |
| 4 | +# |
| 5 | +# This program is free software; you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation; version 2 of the License. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program; if not, write to the Free Software |
| 16 | +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 17 | + |
| 18 | + |
| 19 | +# This script is intended to be executed by systemd. It starts mysqld with |
| 20 | +# --wsrep-recover to recover from a non-graceful shutdown, determines the |
| 21 | +# last stored global transaction ID and echoes it in --wsrep-start-position=XX |
| 22 | +# format. The output is then captured and used by systemd to start mysqld. |
| 23 | +# If the server was configured to start without wsrep, nothing is echoed. |
| 24 | + |
| 25 | +cmdline_args=$@ |
| 26 | +user="@MYSQLD_USER@" |
| 27 | +print_defaults="@bindir@/my_print_defaults" |
| 28 | +log_file=$(mktemp /tmp/wsrep_recovery.XXXXXX) |
| 29 | +euid=$(id -u) |
| 30 | +recovered_pos="" |
| 31 | +skipped="" |
| 32 | +start_pos="" |
| 33 | +start_pos_opt="" |
| 34 | +ret=0 |
| 35 | +wsrep_on=0 |
| 36 | + |
| 37 | +log () |
| 38 | +{ |
| 39 | + local msg="$1" |
| 40 | + # Print all messages to stderr as we reserve stdout for printing |
| 41 | + # --wsrep-start-position=XXXX. |
| 42 | + echo "$msg" >&2 |
| 43 | +} |
| 44 | + |
| 45 | +finish() |
| 46 | +{ |
| 47 | + rm -f "$log_file" |
| 48 | +} |
| 49 | + |
| 50 | +trap finish EXIT |
| 51 | + |
| 52 | +parse_arguments() { |
| 53 | + for arg do |
| 54 | + val=`echo "$arg" | sed -e "s;--[^=]*=;;"` |
| 55 | + case "$arg" in |
| 56 | + --wsrep[-_]on) wsrep_on=1 ;; |
| 57 | + --skip[-_]wsrep[-_]on) wsrep_on=0 ;; |
| 58 | + --wsrep[-_]on=*) |
| 59 | + if echo $val | grep -iq '\(ON\|1\)'; then |
| 60 | + wsrep_on=1 |
| 61 | + else |
| 62 | + wsrep_on=0 |
| 63 | + fi |
| 64 | + ;; |
| 65 | + esac |
| 66 | + done |
| 67 | +} |
| 68 | + |
| 69 | +wsrep_recover_position() { |
| 70 | + # Redirect server's error log to the log file. |
| 71 | + eval /usr/sbin/mysqld $cmdline_args --user=$user --wsrep_recover 2> "$log_file" |
| 72 | + ret=$? |
| 73 | + if [ $ret -ne 0 ]; then |
| 74 | + # Something went wrong, let us also print the error log so that it |
| 75 | + # shows up in systemctl status output as a hint to the user. |
| 76 | + log "WSREP: Failed to start mysqld for wsrep recovery: '`cat $log_file`'" |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | + |
| 80 | + # Parse server's error log for recovered position. The server prints |
| 81 | + # "..skipping position recovery.." if started without wsrep. |
| 82 | + |
| 83 | + recovered_pos="$(grep 'WSREP: Recovered position:' $log_file)" |
| 84 | + |
| 85 | + if [ -z "$recovered_pos" ]; then |
| 86 | + skipped="$(grep WSREP $log_file | grep 'skipping position recovery')" |
| 87 | + if [ -z "$skipped" ]; then |
| 88 | + log "WSREP: Failed to recover position: '`cat $log_file`'" |
| 89 | + exit 1 |
| 90 | + else |
| 91 | + log "WSREP: Position recovery skipped." |
| 92 | + fi |
| 93 | + else |
| 94 | + start_pos="$(echo $recovered_pos | sed 's/.*WSREP\:\ Recovered\ position://' \ |
| 95 | + | sed 's/^[ \t]*//')" |
| 96 | + log "WSREP: Recovered position $start_pos" |
| 97 | + start_pos_opt="--wsrep_start_position=$start_pos" |
| 98 | + fi |
| 99 | +} |
| 100 | + |
| 101 | +# Safety checks |
| 102 | +if [ -n "$log_file" -a -f "$log_file" ]; then |
| 103 | + [ "$euid" = "0" ] && chown $user $log_file |
| 104 | + chmod 600 $log_file |
| 105 | +else |
| 106 | + log "WSREP: mktemp failed" |
| 107 | +fi |
| 108 | + |
| 109 | +parse_arguments `$print_defaults $cmdline_args --loose-verbose \ |
| 110 | + mariadb mariadb_safe mysqld mysqld_safe safe_mysqld galera` |
| 111 | + |
| 112 | +# Perform wsrep position recovery if wsrep_on=1, skip otherwise. |
| 113 | +if [ "$wsrep_on" -eq 1 ]; then |
| 114 | + wsrep_recover_position |
| 115 | +fi |
| 116 | + |
| 117 | +echo "$start_pos_opt" |
| 118 | + |
0 commit comments