#!/bin/bash
#==============================================================================
# Copyright and license info is available in the LICENSE file included with
# the Server Deployment Package (SDP), and also available online:
# https://swarm.workshop.perforce.com/projects/perforce-software-sdp/view/main/LICENSE
#------------------------------------------------------------------------------
# This script is used to change the role of a standby server to the master.
# Be sure to make sure replication is in sync and stop the master first.
# The P4MASTER setting should still point to the original master when you run
# this script since it will rsync the checkpoints folder.
#
# After validating that the new master is working properly, you can then switch DNS.
#
export SDP_INSTANCE=${SDP_INSTANCE:-Undefined}
export SDP_INSTANCE=${1:-$SDP_INSTANCE}
if [[ $SDP_INSTANCE == Undefined ]]; then
echo "Instance parameter not supplied."
echo "You must supply the Perforce instance as a parameter to this script."
exit 1
fi
. /p4/common/bin/p4_vars $SDP_INSTANCE
. /p4/common/bin/backup_functions.sh
LOGFILE=$LOGS/convert_standby_to_master.log
######### Start of Script ##########
check_vars
set_vars
check_uid
check_dirs
log "Start failover"
stop_p4d
rm -f $P4ROOT/server.id.replica
mv $P4ROOT/server.id $P4ROOT/server.id.replica
echo $P4MASTER_ID > $P4ROOT/server.id
echo p4d_master > $P4ROOT/sdp_server_type.txt
jnl=$(cat ${P4ROOT}/statejcopy | cut -d "/" -f 1)
rm -f $P4JOURNAL
cp ${P4HOME}/journals.rep/p4_${SDP_INSTANCE}.jnl.${jnl} $P4JOURNAL
rm -f ${P4ROOT}/state*
rm -f ${P4ROOT}/rdb.lbr
$P4DBIN -r $P4ROOT -jr -f $P4JOURNAL
start_p4d
# The rsync below is to copy over any rotated journals from the original master to the new master so that the
# daily checkpoint process on the new master will not fail due to missing journals.
# The P4MASTER DNS entry MUST still point to the old master when this script runs.
if [[ "$SHAREDDATA" == "FALSE" ]]; then
rsync -avz ${RSYNCUSER}@${P4MASTER}:$CHECKPOINTS/*.jnl.* $CHECKPOINTS > $LOGFILE 2>&1
rsync_exit_code=$?
if [[ $rsync_exit_code -ne 0 ]]; then
die "Error: Failed to pull $CHECKPOINTS from host $P4MASTER. The rsync exit code was: $rsync_exit_code. Aborting."
fi
fi
log "End failover"