recover-edge.ps1 #1

  • //
  • guest/
  • perforce_software/
  • sdp/
  • dev/
  • Server/
  • Windows/
  • p4/
  • common/
  • bin/
  • recover-edge.ps1
  • View
  • Commits
  • Open Download .zip Download (4 KB)
# ============================================================================
# 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
# ----------------------------------------------------------------------------

<#
    .Synopsis
        Recovers an edge server from specified Master checkpoint (which may be filtered)
        
    .Description
        Recover the edge server.
        
    .Parameter SDPInstance
        The specified instance to process
        
    .Parameter EdgeServer
        The specified id of edge server (from 'p4 servers')
        
    .Parameter CkpFile
        The specified (master) checkpoint file to process
        
    .Example
        recover-edge.ps1 1 Edge p4_1.ckp.1234.gz
        
#>

[CmdletBinding()]
param ([string]$SDPInstance = $(throw "SDPInstance parameter is required."),
       [string]$EdgeServer = $(throw "EdgeServer parameter is required."),
       [string]$CkpFile = $(throw "CKPFile parameter is required."))

Set-StrictMode -Version 2.0

# Source the SDP Functions shared between scripts
$SDPFunctionsPath = Split-Path -parent $MyInvocation.MyCommand.Path | Join-Path -childpath "SDP-Functions.ps1" 
. $SDPFunctionsPath

$global:ScriptName = "recover-edge.ps1"
$global:ScriptTask = "Recover edge server from master checkpoint"
$global:LogFileName = "recover-edge.log"

Parse-SDPConfigFile $MyInvocation.MyCommand.Path
Create-LogFile

$OrigPath = convert-path .
Set-Location -Path $global:LOGS_DIR

try {
    remove-files "${global:OFFLINE_DB_DIR}\db." 0

    # With -K we filter out the various Edge-specific tables which will be replaced with 
    # current live versions.
    $EXCLUDED_TABLES = "db.have,db.working,db.resolve,db.locks,db.revsh,db.workingx,db.resolvex"

    log "Recover checkpoint from master into offline_db skipping tables not used on the edge."
    $cmd = "$global:P4DEXE -r $global:OFFLINE_DB_DIR -K $EXCLUDED_TABLES -jr -z $CkpFile"
    run-cmd-with-check $cmd "ERROR - attempting to recover offline db files"

    log "Stopping the edge server."
    Stop-LiveService
    
    # With -k we filter and only checkpoint the specified tables from the current live Edge DB.
    $CKP_TABLES = "$EXCLUDED_TABLES,db.view,db.label,db.revsx,db.revux"

    log "Creating a dump of the edge specific data from P4ROOT"
    $edgedump = -join($global:CHECKPOINTS_DIR, "\edgedump")
    $cmd = "$global:P4DEXE -r $global:P4ROOT -k $CKP_TABLES -jd $edgedump"
    run-cmd-with-check $cmd "ERROR - attempting to create edgedump"

    log "Recover the edge dump into offline_db"
    $cmd = "$global:P4DEXE -r $global:OFFLINE_DB_DIR -k $CKP_TABLES -jr $edgedump"
    run-cmd-with-check $cmd "ERROR - attempting to recover from edgedump"

    log "Reset the replication state and clear the P4ROOT folder db files."
    remove-files "${global:P4ROOT}\db." 0
    remove-files "${global:P4ROOT}\state" 0
    remove-files "${global:P4ROOT}\rdb.lbr" 0
    remove-files "$global:P4JOURNAL" 0

    log "Move the rebuilt database to P4ROOT"
    move-files "${global:offline_db_dir}\db.*" $global:P4ROOT

    log "Start the edge server back up."
    Start-LiveService
    
    log "Recreate the offline_db"
    $cmd = "$global:P4DEXE -r $global:OFFLINE_DB_DIR -K $EXCLUDED_TABLES -jr -z $CkpFile"
    run-cmd-with-check $cmd "ERROR - attempting to recreate offline_db"
    $cmd = "$global:P4DEXE -r $global:OFFLINE_DB_DIR -k $CKP_TABLES -jr $edgedump"
    run-cmd-with-check $cmd "ERROR - attempting to recreate offline_db from edgedump"
    $offline_db_usable = -join($global:OFFLINE_DB_DIR, "\offline_db_usable.txt")
    "Offline db file restored successfully." | set-content -path $offline_db_usable

    log "Create a new edge checkpoint from offline_db"
    $rebuilt_edge_dump = "rebuilt_edge_dump.gz"
    $cmd = "$global:P4DEXE -r $global:OFFLINE_DB_DIR -jd -z $rebuilt_edge_dump"
    run-cmd-with-check $cmd "ERROR - attempting to create new edge checkpoint"

    Log "End ${global:SDP_INSTANCE_P4SERVICE_NAME} ${global:ScriptTask}"
    Signal-CheckpointComplete
    Write-Output "`r`n${global:ScriptTask} completed successfully - see ${global:logfile}"
}
Catch
{
    write-error $error[0].ScriptStackTrace
    LogException $_.Exception
    Write-Output "`r`nFAILED - ${global:ScriptTask} - see ${global:logfile}"
}

Set-Location -Path $OrigPath
# Change User Description Committed
#8 26659 Robert Cowham Removing out-of-date files and ancient utilities.
Updating SDP Guide for Windows with includes to various .ps1 scripts
Add new sync-replica.ps1 and call it from sync_replica.bat
#7 26528 Robert Cowham Start/stop services with svcinst now
Refactor run-cmd to log command output for ease of support,
e.g. when daily-checkpoint fails.
#6 26120 Robert Cowham Fix daily_checkpoint for edge/standby servers
Make edge tables to include/exclude version specific
Move password files to config dir
#5 22759 Robert Cowham Improve documentation within scripts.
#4 22727 Robert Cowham Rotate md5 where necessary
#3 22726 Robert Cowham Fix quoting issues
#2 22725 Robert Cowham Remove unused parameter.
Cope with multiple runs.
#1 22721 Robert Cowham Equivalent of Unix recover_edge.sh