8d5b2c
#!/bin/bash -ue
8d5b2c
8d5b2c
# Copyright (C) 2010-2014 Codership Oy
8d5b2c
# Copyright (C) 2017-2020 Damien Ciabrini <damien.ciabrini@gmail.com>
8d5b2c
#
8d5b2c
# This program is free software; you can redistribute it and/or modify
8d5b2c
# it under the terms of the GNU General Public License as published by
8d5b2c
# the Free Software Foundation; version 2 of the License.
8d5b2c
#
8d5b2c
# This program is distributed in the hope that it will be useful,
8d5b2c
# but WITHOUT ANY WARRANTY; without even the implied warranty of
8d5b2c
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8d5b2c
# GNU General Public License for more details.
8d5b2c
#
8d5b2c
# You should have received a copy of the GNU General Public License
8d5b2c
# along with this program; see the file COPYING. If not, write to the
8d5b2c
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston
8d5b2c
# MA  02110-1301  USA.
8d5b2c
8d5b2c
# This is a reference script for rsync-based state snapshot tansfer
8d5b2c
# over an encrypted communication channel, managed by socat
8d5b2c
8d5b2c
RSYNC_PID=                                      # rsync pid file
8d5b2c
RSYNC_CONF=                                     # rsync configuration file
8d5b2c
RSYNC_REAL_PID=                                 # rsync process id
8d5b2c
8d5b2c
SOCAT_PID=                                      # socat pid file
8d5b2c
SOCAT_REAL_PID=                                 # socat process id
8d5b2c
8d5b2c
SOCAT_OPTS=                                     # openssl connection args
8d5b2c
8d5b2c
MODULE="rsync_tunnel_sst"
8d5b2c
8d5b2c
OS=$(uname)
8d5b2c
[ "$OS" == "Darwin" ] && export -n LD_LIBRARY_PATH
8d5b2c
8d5b2c
# Setting the path for lsof on CentOS
8d5b2c
export PATH="/usr/sbin:/sbin:$PATH"
8d5b2c
8d5b2c
. $(dirname $0)/wsrep_sst_common
8d5b2c
8d5b2c
wsrep_check_programs rsync socat
8d5b2c
8d5b2c
cleanup_pid()
8d5b2c
{
8d5b2c
    local real_pid=$1
8d5b2c
    [ "0" != "$real_pid" ]            && \
8d5b2c
    kill $real_pid                    && \
8d5b2c
    sleep 0.5                         && \
8d5b2c
    kill -9 $real_pid >/dev/null 2>&1 || \
8d5b2c
    :
8d5b2c
}
8d5b2c
8d5b2c
cleanup_tunnel()
8d5b2c
{
8d5b2c
    if [ -n "$SOCAT_REAL_PID" ] && ps -p "$SOCAT_REAL_PID" >/dev/null 2>&1; then
8d5b2c
	wsrep_log_info "cleanup socat PID: $SOCAT_REAL_PID"
8d5b2c
	cleanup_pid $SOCAT_REAL_PID
8d5b2c
    fi
8d5b2c
    rm -rf "$SOCAT_PID"
8d5b2c
}
8d5b2c
8d5b2c
cleanup_joiner()
8d5b2c
{
8d5b2c
    wsrep_log_info "Joiner cleanup. rsync PID: $RSYNC_REAL_PID"
8d5b2c
    [ -n "$RSYNC_REAL_PID" ] && cleanup_pid $RSYNC_REAL_PID
8d5b2c
    rm -rf "$RSYNC_CONF"
8d5b2c
    rm -rf "$MAGIC_FILE"
8d5b2c
    rm -rf "$RSYNC_PID"
8d5b2c
8d5b2c
    cleanup_tunnel
8d5b2c
8d5b2c
    wsrep_log_info "Joiner cleanup done."
8d5b2c
    if [ "${WSREP_SST_OPT_ROLE}" = "joiner" ];then
8d5b2c
        wsrep_cleanup_progress_file
8d5b2c
    fi
8d5b2c
}
8d5b2c
8d5b2c
# Check whether process is still running.
8d5b2c
check_pid()
8d5b2c
{
8d5b2c
    local pid_file=$1
8d5b2c
    [ -r "$pid_file" ] && ps -p $(cat $pid_file) >/dev/null 2>&1
8d5b2c
}
8d5b2c
8d5b2c
check_pid_and_port()
8d5b2c
{
8d5b2c
    local pid_file=$1
8d5b2c
    local service_pid=$2
8d5b2c
    local service_port=$3
8d5b2c
    local service_host=$4
8d5b2c
    local service_name=$5
8d5b2c
8d5b2c
    if ! which lsof > /dev/null; then
8d5b2c
      wsrep_log_error "lsof tool not found in PATH! Make sure you have it installed."
8d5b2c
      exit 2 # ENOENT
8d5b2c
    fi
8d5b2c
8d5b2c
    local port_info=$(lsof -i "@"$service_host:$service_port -Pn 2>/dev/null | \
8d5b2c
        grep "(LISTEN)")
8d5b2c
    local is_service=$(echo $port_info | \
8d5b2c
        grep -w '^'"$service_name"'[[:space:]]\+'"$service_pid" 2>/dev/null)
8d5b2c
8d5b2c
    if [ -n "$port_info" -a -z "$is_service" ]; then
8d5b2c
        wsrep_log_error "$service_name daemon port '$service_port' has been taken"
8d5b2c
        exit 16 # EBUSY
8d5b2c
    fi
8d5b2c
8d5b2c
    if ! check_pid $pid_file; then
8d5b2c
        wsrep_log_error "$service_name process terminated unexpectedly"
8d5b2c
        exit 10 # ECHILD
8d5b2c
    fi
8d5b2c
8d5b2c
    [ -n "$port_info" ] && [ -n "$is_service" ] && \
8d5b2c
        [ $(cat $pid_file) -eq $service_pid ]
8d5b2c
}
8d5b2c
8d5b2c
config_from_cnf()
8d5b2c
{
8d5b2c
    local group=$1
8d5b2c
    local key=$2
8d5b2c
    echo $($MY_PRINT_DEFAULTS $group | grep -- "--$key=" | cut -d= -f2- | tail -1)
8d5b2c
}
8d5b2c
8d5b2c
setup_tunnel_args()
8d5b2c
{
8d5b2c
    tca=$(config_from_cnf sst tca)
8d5b2c
    tkey=$(config_from_cnf sst tkey)
8d5b2c
    tcert=$(config_from_cnf sst tcert)
8d5b2c
    sockopt=$(config_from_cnf sst sockopt)
8d5b2c
8d5b2c
    if [ -z "$tcert" ]; then
8d5b2c
        wsrep_log_error "Encryption certificate not found in my.cnf"
8d5b2c
        exit 3
8d5b2c
    else
8d5b2c
        SOCAT_OPTS="cert=$tcert"
8d5b2c
    fi
8d5b2c
    [ -n "$tkey" ] && SOCAT_OPTS="$SOCAT_OPTS,key=$tkey"
8d5b2c
    [ -n "$tca" ] && SOCAT_OPTS="$SOCAT_OPTS,cafile=$tca"
8d5b2c
    wsrep_log_info "Encryption setting to be used for socat tunnel: $SOCAT_OPTS"
8d5b2c
8d5b2c
    [ -n "$sockopt" ] && SOCAT_OPTS="$SOCAT_OPTS,$sockopt"
8d5b2c
}
8d5b2c
8d5b2c
MAGIC_FILE="$WSREP_SST_OPT_DATA/rsync_tunnel_sst_complete"
8d5b2c
rm -rf "$MAGIC_FILE"
8d5b2c
8d5b2c
BINLOG_TAR_FILE="$WSREP_SST_OPT_DATA/wsrep_sst_binlog.tar"
8d5b2c
BINLOG_N_FILES=1
8d5b2c
rm -f "$BINLOG_TAR_FILE" || :
8d5b2c
8d5b2c
if ! [ -z $WSREP_SST_OPT_BINLOG ]
8d5b2c
then
8d5b2c
    BINLOG_DIRNAME=$(dirname $WSREP_SST_OPT_BINLOG)
8d5b2c
    BINLOG_FILENAME=$(basename $WSREP_SST_OPT_BINLOG)
8d5b2c
fi
8d5b2c
8d5b2c
WSREP_LOG_DIR=${WSREP_LOG_DIR:-""}
8d5b2c
# if WSREP_LOG_DIR env. variable is not set, try to get it from my.cnf
8d5b2c
if [ -z "$WSREP_LOG_DIR" ]; then
8d5b2c
    WSREP_LOG_DIR=$($MY_PRINT_DEFAULTS --mysqld \
8d5b2c
                    | grep -- '--innodb[-_]log[-_]group[-_]home[-_]dir=' \
8d5b2c
                    | cut -b 29- )
8d5b2c
fi
8d5b2c
8d5b2c
if [ -n "$WSREP_LOG_DIR" ]; then
8d5b2c
    # handle both relative and absolute paths
8d5b2c
    WSREP_LOG_DIR=$(cd $WSREP_SST_OPT_DATA; mkdir -p "$WSREP_LOG_DIR"; cd $WSREP_LOG_DIR; pwd -P)
8d5b2c
else
8d5b2c
    # default to datadir
8d5b2c
    WSREP_LOG_DIR=$(cd $WSREP_SST_OPT_DATA; pwd -P)
8d5b2c
fi
8d5b2c
8d5b2c
# Old filter - include everything except selected
8d5b2c
# FILTER=(--exclude '*.err' --exclude '*.pid' --exclude '*.sock' \
8d5b2c
#         --exclude '*.conf' --exclude core --exclude 'galera.*' \
8d5b2c
#         --exclude grastate.txt --exclude '*.pem' \
8d5b2c
#         --exclude '*.[0-9][0-9][0-9][0-9][0-9][0-9]' --exclude '*.index')
8d5b2c
8d5b2c
# New filter - exclude everything except dirs (schemas) and innodb files
8d5b2c
FILTER=(-f '- /lost+found' -f '- /.fseventsd' -f '- /.Trashes'
8d5b2c
        -f '+ /wsrep_sst_binlog.tar' -f '+ /ib_lru_dump' -f '+ /ibdata*' -f '+ /*/' -f '- /*')
8d5b2c
8d5b2c
SOCAT_PID="$WSREP_SST_OPT_DATA/$MODULE-socat.pid"
8d5b2c
8d5b2c
if check_pid $SOCAT_PID
8d5b2c
then
8d5b2c
    wsrep_log_error "socat tunnel already running."
8d5b2c
    exit 114 # EALREADY
8d5b2c
fi
8d5b2c
rm -rf "$SOCAT_PID"
8d5b2c
8d5b2c
setup_tunnel_args
8d5b2c
8d5b2c
if [ "$WSREP_SST_OPT_ROLE" = "donor" ]
8d5b2c
then
8d5b2c
8d5b2c
    SOCAT_JOINER_ADDR=$(echo $WSREP_SST_OPT_ADDR | awk -F'/' '{print $1}')
8d5b2c
    # map to name in case we received an IP
8d5b2c
    SOCAT_JOINER_HOST=$(getent hosts $SOCAT_JOINER_ADDR | awk '{ print $2 }')
8d5b2c
    if [ -z "$SOCAT_JOINER_HOST" ]; then
8d5b2c
        SOCAT_JOINER_HOST=$SOCAT_JOINER_ADDR
8d5b2c
    fi
8d5b2c
    SOCAT_PORT=$(echo $SOCAT_JOINER_ADDR | awk -F ':' '{ print $2 }')
8d5b2c
    if [ -z "$SOCAT_PORT" ]
8d5b2c
    then
8d5b2c
        SOCAT_PORT=4444
8d5b2c
    fi
8d5b2c
    TARGET_ADDR=localhost:$SOCAT_PORT/$MODULE
8d5b2c
8d5b2c
    trap cleanup_tunnel EXIT
8d5b2c
8d5b2c
    # Socat forwards rsync connections to the joiner
8d5b2c
    SOCAT_SRC=tcp-listen:$SOCAT_PORT,bind=localhost,reuseaddr,fork
8d5b2c
    SOCAT_DST=openssl:$SOCAT_JOINER_HOST,$SOCAT_OPTS
8d5b2c
    wsrep_log_info "Setting up tunnel for donor: socat $SOCAT_SRC $SOCAT_DST"
8d5b2c
    socat $SOCAT_SRC $SOCAT_DST &
8d5b2c
    SOCAT_REAL_PID=$!
8d5b2c
    # This is ok because a local galera node doesn't run SST concurrently
8d5b2c
    echo $SOCAT_REAL_PID >"$SOCAT_PID"
8d5b2c
    until check_pid_and_port $SOCAT_PID $SOCAT_REAL_PID $SOCAT_PORT localhost "socat"
8d5b2c
    do
8d5b2c
        sleep 0.2
8d5b2c
    done
8d5b2c
8d5b2c
    if [ $WSREP_SST_OPT_BYPASS -eq 0 ]
8d5b2c
    then
8d5b2c
8d5b2c
        FLUSHED="$WSREP_SST_OPT_DATA/tables_flushed"
8d5b2c
        ERROR="$WSREP_SST_OPT_DATA/sst_error"
8d5b2c
8d5b2c
        rm -rf "$FLUSHED"
8d5b2c
        rm -rf "$ERROR"
8d5b2c
8d5b2c
        # Use deltaxfer only for WAN
8d5b2c
        inv=$(basename $0)
8d5b2c
        [ "$inv" = "wsrep_sst_rsync_wan" ] && WHOLE_FILE_OPT="" \
8d5b2c
                                           || WHOLE_FILE_OPT="--whole-file"
8d5b2c
8d5b2c
        echo "flush tables"
8d5b2c
8d5b2c
        # Wait for :
8d5b2c
        # (a) Tables to be flushed, AND
8d5b2c
        # (b) Cluster state ID & wsrep_gtid_domain_id to be written to the file, OR
8d5b2c
        # (c) ERROR file, in case flush tables operation failed.
8d5b2c
8d5b2c
        while [ ! -r "$FLUSHED" ] && ! grep -q ':' "$FLUSHED" >/dev/null 2>&1
8d5b2c
        do
8d5b2c
            # Check whether ERROR file exists.
8d5b2c
            if [ -f "$ERROR" ]
8d5b2c
            then
8d5b2c
                # Flush tables operation failed.
8d5b2c
                rm -rf "$ERROR"
8d5b2c
                exit 255
8d5b2c
            fi
8d5b2c
8d5b2c
            sleep 0.2
8d5b2c
        done
8d5b2c
8d5b2c
        STATE="$(cat $FLUSHED)"
8d5b2c
        rm -rf "$FLUSHED"
8d5b2c
8d5b2c
        sync
8d5b2c
8d5b2c
        if ! [ -z $WSREP_SST_OPT_BINLOG ]
8d5b2c
        then
8d5b2c
            # Prepare binlog files
8d5b2c
            pushd $BINLOG_DIRNAME &> /dev/null
8d5b2c
            binlog_files_full=$(tail -n $BINLOG_N_FILES ${BINLOG_FILENAME}.index)
8d5b2c
            binlog_files=""
8d5b2c
            for ii in $binlog_files_full
8d5b2c
            do
8d5b2c
                binlog_files="$binlog_files $(basename $ii)"
8d5b2c
            done
8d5b2c
            if ! [ -z "$binlog_files" ]
8d5b2c
            then
8d5b2c
                wsrep_log_info "Preparing binlog files for transfer:"
8d5b2c
                tar -cvf $BINLOG_TAR_FILE $binlog_files >&2
8d5b2c
            fi
8d5b2c
            popd &> /dev/null
8d5b2c
        fi
8d5b2c
8d5b2c
        # first, the normal directories, so that we can detect incompatible protocol
8d5b2c
        RC=0
8d5b2c
        rsync --owner --group --perms --links --specials \
8d5b2c
              --ignore-times --inplace --dirs --delete --quiet \
8d5b2c
              $WHOLE_FILE_OPT "${FILTER[@]}" "$WSREP_SST_OPT_DATA/" \
8d5b2c
              rsync://$TARGET_ADDR >&2 || RC=$?
8d5b2c
8d5b2c
        if [ "$RC" -ne 0 ]; then
8d5b2c
            wsrep_log_error "rsync returned code $RC:"
8d5b2c
8d5b2c
            case $RC in
8d5b2c
            12) RC=71  # EPROTO
8d5b2c
                wsrep_log_error \
8d5b2c
                "rsync server on the other end has incompatible protocol. " \
8d5b2c
                "Make sure you have the same version of rsync on all nodes."
8d5b2c
                ;;
8d5b2c
            22) RC=12  # ENOMEM
8d5b2c
                ;;
8d5b2c
            *)  RC=255 # unknown error
8d5b2c
                ;;
8d5b2c
            esac
8d5b2c
            exit $RC
8d5b2c
        fi
8d5b2c
8d5b2c
        # second, we transfer InnoDB log files
8d5b2c
        rsync --owner --group --perms --links --specials \
8d5b2c
              --ignore-times --inplace --dirs --delete --quiet \
8d5b2c
              $WHOLE_FILE_OPT -f '+ /ib_logfile[0-9]*' -f '- **' "$WSREP_LOG_DIR/" \
8d5b2c
              rsync://$TARGET_ADDR-log_dir >&2 || RC=$?
8d5b2c
8d5b2c
        if [ $RC -ne 0 ]; then
8d5b2c
            wsrep_log_error "rsync innodb_log_group_home_dir returned code $RC:"
8d5b2c
            exit 255 # unknown error
8d5b2c
        fi
8d5b2c
8d5b2c
        # then, we parallelize the transfer of database directories, use . so that pathconcatenation works
8d5b2c
        pushd "$WSREP_SST_OPT_DATA" >/dev/null
8d5b2c
8d5b2c
        count=1
8d5b2c
        [ "$OS" == "Linux" ] && count=$(grep -c processor /proc/cpuinfo)
8d5b2c
        [ "$OS" == "Darwin" -o "$OS" == "FreeBSD" ] && count=$(sysctl -n hw.ncpu)
8d5b2c
8d5b2c
        find . -maxdepth 1 -mindepth 1 -type d -not -name "lost+found" -print0 | \
8d5b2c
             xargs -I{} -0 -P $count \
8d5b2c
             rsync --owner --group --perms --links --specials \
8d5b2c
             --ignore-times --inplace --recursive --delete --quiet \
8d5b2c
             $WHOLE_FILE_OPT --exclude '*/ib_logfile*' "$WSREP_SST_OPT_DATA"/{}/ \
8d5b2c
             rsync://$TARGET_ADDR/{} >&2 || RC=$?
8d5b2c
8d5b2c
        popd >/dev/null
8d5b2c
8d5b2c
        if [ $RC -ne 0 ]; then
8d5b2c
            wsrep_log_error "find/rsync returned code $RC:"
8d5b2c
            exit 255 # unknown error
8d5b2c
        fi
8d5b2c
8d5b2c
    else # BYPASS
8d5b2c
        wsrep_log_info "Bypassing state dump."
8d5b2c
8d5b2c
        # Store donor's wsrep GTID (state ID) and wsrep_gtid_domain_id
8d5b2c
        # (separated by a space).
8d5b2c
        STATE="$WSREP_SST_OPT_GTID $WSREP_SST_OPT_GTID_DOMAIN_ID"
8d5b2c
    fi
8d5b2c
8d5b2c
    echo "continue" # now server can resume updating data
8d5b2c
8d5b2c
    echo "$STATE" > "$MAGIC_FILE"
8d5b2c
    rsync --archive --quiet --checksum "$MAGIC_FILE" rsync://$TARGET_ADDR
8d5b2c
8d5b2c
    # to avoid cleanup race, stop tunnel before declaring the SST finished.
8d5b2c
    # This ensures galera won't start a new SST locally before we exit.
8d5b2c
    cleanup_tunnel
8d5b2c
8d5b2c
    echo "done $STATE"
8d5b2c
8d5b2c
elif [ "$WSREP_SST_OPT_ROLE" = "joiner" ]
8d5b2c
then
8d5b2c
    wsrep_check_programs lsof socat
8d5b2c
8d5b2c
    touch $SST_PROGRESS_FILE
8d5b2c
    MYSQLD_PID=$WSREP_SST_OPT_PARENT
8d5b2c
8d5b2c
    RSYNC_PID="$WSREP_SST_OPT_DATA/$MODULE.pid"
8d5b2c
8d5b2c
    if check_pid $RSYNC_PID
8d5b2c
    then
8d5b2c
        wsrep_log_error "rsync daemon already running."
8d5b2c
        exit 114 # EALREADY
8d5b2c
    fi
8d5b2c
    rm -rf "$RSYNC_PID"
8d5b2c
8d5b2c
    ADDR=$WSREP_SST_OPT_ADDR
8d5b2c
    RSYNC_PORT=$(echo $ADDR | awk -F ':' '{ print $2 }')
8d5b2c
    if [ -z "$RSYNC_PORT" ]
8d5b2c
    then
8d5b2c
        RSYNC_PORT=4444
8d5b2c
        ADDR="$(echo $ADDR | awk -F ':' '{ print $1 }'):$RSYNC_PORT"
8d5b2c
    fi
8d5b2c
8d5b2c
    SOCAT_ADDR=$(echo $ADDR | awk -F ':' '{ print $1 }')
8d5b2c
    # map to name in case we received an IP
8d5b2c
    SOCAT_HOST=$(getent hosts $SOCAT_ADDR | awk '{ print $2 }')
8d5b2c
    if [ -z "$SOCAT_HOST" ]; then
8d5b2c
        SOCAT_HOST=$SOCAT_ADDR
8d5b2c
    fi
8d5b2c
    SOCAT_PORT=$RSYNC_PORT
8d5b2c
8d5b2c
    trap "exit 32" HUP PIPE
8d5b2c
    trap "exit 3"  INT TERM ABRT
8d5b2c
    trap cleanup_joiner EXIT
8d5b2c
8d5b2c
    RSYNC_CONF="$WSREP_SST_OPT_DATA/$MODULE.conf"
8d5b2c
8d5b2c
    if [ -n "${MYSQL_TMP_DIR:-}" ] ; then
8d5b2c
      SILENT="log file = $MYSQL_TMP_DIR/rsynd.log"
8d5b2c
    else
8d5b2c
      SILENT=""
8d5b2c
    fi
8d5b2c
8d5b2c
cat << EOF > "$RSYNC_CONF"
8d5b2c
pid file = $RSYNC_PID
8d5b2c
use chroot = no
8d5b2c
read only = no
8d5b2c
timeout = 300
8d5b2c
$SILENT
8d5b2c
[$MODULE]
8d5b2c
    path = $WSREP_SST_OPT_DATA
8d5b2c
[$MODULE-log_dir]
8d5b2c
    path = $WSREP_LOG_DIR
8d5b2c
EOF
8d5b2c
8d5b2c
#    rm -rf "$DATA"/ib_logfile* # we don't want old logs around
8d5b2c
8d5b2c
    # Socat receives rsync connections from the donor
8d5b2c
    SOCAT_SRC=openssl-listen:$SOCAT_PORT,bind=$SOCAT_HOST,reuseaddr,fork,$SOCAT_OPTS
8d5b2c
    SOCAT_DST=tcp:localhost:$RSYNC_PORT
8d5b2c
    wsrep_log_info "Setting up tunnel for joiner: socat $SOCAT_SRC $SOCAT_DST"
8d5b2c
    socat $SOCAT_SRC $SOCAT_DST &
8d5b2c
    SOCAT_REAL_PID=$!
8d5b2c
    # This is ok because a local galera node doesn't run SST concurrently
8d5b2c
    echo $SOCAT_REAL_PID >"$SOCAT_PID"
8d5b2c
    until check_pid_and_port $SOCAT_PID $SOCAT_REAL_PID $SOCAT_PORT $SOCAT_HOST "socat"
8d5b2c
    do
8d5b2c
        sleep 0.2
8d5b2c
    done
8d5b2c
8d5b2c
    wsrep_log_info "rsync --daemon --no-detach --address localhost --port $RSYNC_PORT --config \"$RSYNC_CONF\""
8d5b2c
    rsync --daemon --no-detach --address localhost --port $RSYNC_PORT --config "$RSYNC_CONF" &
8d5b2c
    RSYNC_REAL_PID=$!
8d5b2c
8d5b2c
    until check_pid_and_port $RSYNC_PID $RSYNC_REAL_PID $RSYNC_PORT localhost "rsync"
8d5b2c
    do
8d5b2c
        sleep 0.2
8d5b2c
    done
8d5b2c
8d5b2c
    echo "ready $ADDR/$MODULE"
8d5b2c
8d5b2c
    # wait for SST to complete by monitoring magic file
8d5b2c
    while [ ! -r "$MAGIC_FILE" ] && check_pid "$RSYNC_PID" && \
8d5b2c
          check_pid "$SOCAT_PID" && ps -p $MYSQLD_PID >/dev/null
8d5b2c
    do
8d5b2c
        sleep 1
8d5b2c
    done
8d5b2c
8d5b2c
    # to avoid cleanup race, we can tear down the socat tunnel now
8d5b2c
    # before signaling the end of the SST to galera.
8d5b2c
    cleanup_tunnel
8d5b2c
8d5b2c
    if ! ps -p $MYSQLD_PID >/dev/null
8d5b2c
    then
8d5b2c
        wsrep_log_error \
8d5b2c
        "Parent mysqld process (PID:$MYSQLD_PID) terminated unexpectedly."
8d5b2c
        exit 32
8d5b2c
    fi
8d5b2c
8d5b2c
    if ! [ -z $WSREP_SST_OPT_BINLOG ]
8d5b2c
    then
8d5b2c
8d5b2c
        pushd $BINLOG_DIRNAME &> /dev/null
8d5b2c
        if [ -f $BINLOG_TAR_FILE ]
8d5b2c
        then
8d5b2c
            # Clean up old binlog files first
8d5b2c
            rm -f ${BINLOG_FILENAME}.*
8d5b2c
            wsrep_log_info "Extracting binlog files:"
8d5b2c
            tar -xvf $BINLOG_TAR_FILE >&2
8d5b2c
            for ii in $(ls -1 ${BINLOG_FILENAME}.*)
8d5b2c
            do
8d5b2c
                echo ${BINLOG_DIRNAME}/${ii} >> ${BINLOG_FILENAME}.index
8d5b2c
            done
8d5b2c
        fi
8d5b2c
        popd &> /dev/null
8d5b2c
    fi
8d5b2c
    if [ -r "$MAGIC_FILE" ]
8d5b2c
    then
8d5b2c
        # UUID:seqno & wsrep_gtid_domain_id is received here.
8d5b2c
        cat "$MAGIC_FILE" # Output : UUID:seqno wsrep_gtid_domain_id
8d5b2c
    else
8d5b2c
        # this message should cause joiner to abort
8d5b2c
        echo "rsync process ended without creating '$MAGIC_FILE'"
8d5b2c
    fi
8d5b2c
    wsrep_cleanup_progress_file
8d5b2c
#    cleanup_joiner
8d5b2c
else
8d5b2c
    wsrep_log_error "Unrecognized role: '$WSREP_SST_OPT_ROLE'"
8d5b2c
    exit 22 # EINVAL
8d5b2c
fi
8d5b2c
8d5b2c
rm -f $BINLOG_TAR_FILE || :
8d5b2c
8d5b2c
exit 0