#!/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 designed to rebuild an Edge server from a checkpoint of the master WHILE
# KEEPING THE EXISTING EDGE SPECIFIC DATA.
# You have to first copy the checkpoint from the master to the edge server before running this script.
# Then you run this script on the Edge server with the instance number and full path and filename
# of the master checkpoint as parameters.
#
# Run example:
#  ./recover_edge.sh 1 /depotdata/p4_1.ckp.9188.gz

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}/recover_edge.log

######### Start of Script ##########

check_vars
set_vars
ckp_running

if [[ "$2" == "" ]]; then
   echo You must pass in the full path and filename of the checkpoint you copied over from the master server.
   exit 2
fi

MASTERCKP=$2

log "Remove offline db"
rm -f ${OFFLINE_DB}/db.* > $LOGFILE

# With -K we filter out the various Edge-specific tables which will be replaced with 
# current live versions.
if [[ "${P4D_VERSION:-1970.1}" > "2020.2" ]]; then
   ExcludedTables=db.have,db.working,db.locks,db.resolve,db.revsh,db.workingx,db.resolvex,db.stash,db.haveg,db.workingg,db.locksg,db.resolveg,db.storagesh,db.storagesx
elif [[ "${P4D_VERSION:-1970.1}" > "2019.0" ]]; then
   ExcludedTables=db.have,db.working,db.locks,db.resolve,db.revsh,db.workingx,db.resolvex,db.stash,db.haveg,db.workingg,db.locksg,db.resolveg,db.storagesh
elif [[ "${P4D_VERSION:-1970.1}" > "2015.1" ]]; then
   ExcludedTables=db.have,db.working,db.locks,db.resolve,db.revsh,db.workingx,db.resolvex,db.stash,db.haveg,db.workingg,db.locksg,db.resolveg
else
   ExcludedTables=db.have,db.working,db.locks,db.resolve,db.revsh,db.workingx,db.resolvex,db.stash
fi

log "Recover checkpoint from master into offline_db skipping tables not used on the edge."
if [[ "${P4D_VERSION:-1970.1}" > "2022.1" ]]; then
  ${P4DBIN} -r ${OFFLINE_DB} -R edge-server -jr -z $MASTERCKP >> $LOGFILE 2>&1
else
  ${P4DBIN} -r ${OFFLINE_DB} -K $ExcludedTables -jr -z $MASTERCKP >> $LOGFILE 2>&1
fi

log "Stopping the edge server."
stop_p4d  >> $LOGFILE 2>&1

# With -k we filter and only checkpoint the specified tables from the current live Edge DB.
CKP_TABLES=$ExcludedTables,db.view,db.label,db.revsx,db.revux,db.storage

log "Creating a dump of the edge specific data from P4ROOT"
${P4DBIN} -r ${P4ROOT} -k $CKP_TABLES -jd ${CHECKPOINTS}/edgedump  >> $LOGFILE 2>&1

log "Recover the edge dump into offline_db"
${P4DBIN} -r ${OFFLINE_DB} -jr ${CHECKPOINTS}/edgedump  >> $LOGFILE 2>&1

log "Reset the replication state and clear the P4ROOT folder db files."
rm -f ${P4ROOT}/db.*  >> $LOGFILE 2>&1
rm -f ${P4ROOT}/state  >> $LOGFILE 2>&1
rm -f ${P4ROOT}/rdb.lbr  >> $LOGFILE 2>&1
rm -f ${P4JOURNAL}  >> $LOGFILE 2>&1

log "Move the rebuilt database to P4ROOT"
mv ${OFFLINE_DB}/db.* ${P4ROOT}  >> $LOGFILE 2>&1

log "Start the edge server back up."
start_p4d  >> $LOGFILE 2>&1

log "Recreate the offline_db"
if [[ "${P4D_VERSION:-1970.1}" > "2022.1" ]]; then
  ${P4DBIN} -r ${OFFLINE_DB} -R edge-server -jr -z $MASTERCKP  >> $LOGFILE 2>&1
  ${P4DBIN} -r ${OFFLINE_DB} -jr ${CHECKPOINTS}/edgedump  >> $LOGFILE 2>&1
else
  ${P4DBIN} -r ${OFFLINE_DB} -K $ExcludedTables -jr -z $MASTERCKP  >> $LOGFILE 2>&1
  ${P4DBIN} -r ${OFFLINE_DB} -jr ${CHECKPOINTS}/edgedump  >> $LOGFILE 2>&1
fi

log "Create a new edge checkpoint from offline_db"
get_offline_journal_num

rm -f ${CHECKPOINTS}/${P4SERVER}.ckp.${OFFLINECHECKPOINTNUM}.gz >> $LOGFILE 2>&1
rm -f ${CHECKPOINTS}/${P4SERVER}.ckp.${OFFLINECHECKPOINTNUM}.gz.md5 >> $LOGFILE 2>&1
${P4DBIN} -r ${OFFLINE_DB} -jd -z ${CHECKPOINTS}/${P4SERVER}.ckp.${OFFLINECHECKPOINTNUM}.gz  >> $LOGFILE 2>&1

ckp_complete
log "End $P4SERVER Recover Edge"
mail_log_file "$HOSTNAME $P4SERVER Recover Edge log."

