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