#!/bin/bash #------------------------------------------------------------------------------ ### WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING ### THIS SCRIPT SHOULD BE USED WITH GREAT CAUTION. #------------------------------------------------------------------------------ set -u # This script loads a specified checkpoint simultaneously into /p4/N/root # and /p4/N/offline_db. function msg () { echo -e "$*"; } function bail () { msg "${1:-Unknown Error}\n"; exit ${2:-1}; } function usage () { msg "Sample Usage (bash syntax): \tcd /p4/common/bin \tsource ./p4_vars 1 \tnohup ./load_checkpoint.sh 1 /p4/1/checkpoints/p4_1.ckp.4025.gz < /dev/null > \$LOGS/load_checkpoint.\$(date +'%Y%m%d-%H%M').log 2>&1 &\n Then, monitor with: \ttailf \$(ls -t \$LOGS/load_checkpoint.*.log|head -1)\n" exit 1 } function usageError () { msg "\nUsage Error: ${1:-Bad Usage}\n"; usage; } export SDP_INSTANCE=${1:-Unset} [[ "$SDP_INSTANCE" == Unset ]] && \ usageError "Missing required SDP Instance parameter." declare Version=1.0.3 declare Checkpoint=${2:-Unset} declare SDPInstanceVarsFile="/p4/common/config/p4_${SDP_INSTANCE}.vars" declare OfflineDB= declare OfflineDBUsableFile= declare declare ExcludedTables=db.have,db.working,db.resolve,db.locks,db.revsh,db.workingx,db.resolvex declare P4DInitScript="/p4/${SDP_INSTANCE}/bin/p4d_${SDP_INSTANCE}_init" declare P4BrokerInitScript="/p4/${SDP_INSTANCE}/bin/p4broker_${SDP_INSTANCE}_init" declare P4BrokerCfg= declare P4DPids= declare ThisHost=$(hostname -s) [[ "$Checkpoint" == Unset ]] && usageError "Missing checkpoint parameter." [[ -r "$SDPInstanceVarsFile" ]] ||\ bail "Missing SDP instance vars file. Typo in instance name? Expected: $SDPInstanceVarsFile" [[ -r "$Checkpoint" ]] ||\ bail "Specified checkpoint file does not exist: $Checkpoint" source /p4/common/bin/p4_vars $SDP_INSTANCE source /p4/common/bin/ps_functions.sh ||\ bail "Failed to load ps_functions.sh." declare CheckpointMD5= msg "Started ${0##*/} v$Version at $(date)" cd "$P4ROOT" [[ "$PWD" == "/p4/${SDP_INSTANCE}/root" ]] || bail "Unexpected P4ROOT value of: $P4ROOT" CheckpointMD5="${Checkpoint}.md5" if [[ -r "$CheckpointMD5" ]]; then msg "Verified: MD5 file for checkpoint exists: $CheckpointMD5" else CheckpointMD5="${Checkpoint/.gz/.md5}" if [[ -r "$CheckpointMD5" ]]; then msg "Verified: MD5 file for checkpoint exists: $CheckpointMD5" else bail "Could not find MD5 file for checkpoint. Tried ${Checkpoint}.md5 and ${Checkpoint/.gz/.md5}" fi fi [[ -x "$P4DInitScript" ]] || bail "Aborting: P4D Init script is missing or not executable: $P4DInitScript" if [[ -r "/p4/common/config/p4_${SDP_INSTANCE}.broker.${ThisHost}.cfg" ]]; then P4BrokerCfg="/p4/common/config/p4_${SDP_INSTANCE}.broker.${ThisHost}.cfg" else P4BrokerCfg="/p4/common/config/p4_${SDP_INSTANCE}.broker.cfg" fi if [[ -x "$P4BrokerInitScript" && -r "$P4BrokerCfg" ]]; then msg "Checking p4broker status." $P4BrokerInitScript status if [[ $? -eq 0 ]]; then msg "Shutting down p4broker." $P4BrokerInitScript stop else msg "Verified: p4broker is down." fi fi msg "Checking p4d status." P4DPids=$(get_pids "$P4DBIN") if [[ -n "$P4DPids" ]]; then msg "Shutting down p4d." $P4DInitScript stop else msg "Verified: No processes are running for: $P4DBIN" fi # Remove db.* and state* files, but not server.id or license files. Cmd="/bin/rm -f db.* state*" msg "Cleaning up databases with this command in $PWD:\n$Cmd" # Move P4LOG and P4JOURAL aside if they exist. if [[ -r "$P4LOG" ]]; then Cmd="mv $P4LOG $P4LOG.moved.$(date +'%Y%m%d-%H%M')" msg "Moving P4LOG aside with this command:\n$Cmd" fi if [[ -r "$P4JOURNAL" ]]; then Cmd="mv $P4JOURNAL $P4JOURNAL.moved.$(date +'%Y%m%d-%H%M')" msg "Moving P4JOURNAL aside with this command:\n$Cmd" fi if [[ "${SERVERID^^}" == "P4D_EDGE_"* ]]; then Cmd="time $P4DBIN -r $P4ROOT -K $ExcludedTables -z -jr $Checkpoint" else Cmd="time $P4DBIN -r $P4ROOT -z -jr $Checkpoint" fi msg "Replaying checkpoint to P4ROOT with this command:\n$Cmd" $Cmd || bail "Checkpoint replay to P4ROOT failed." Cmd="$P4DBIN -r $P4ROOT -xu" msg "Ensuring databases are upgraded with this command:\n$Cmd" $Cmd || bail "Database upgrade in P4ROOT failed." OfflineDB=${P4ROOT/root/offline_db} OfflineDBUsableFile="$OfflineDB/offline_db_usable.txt" cd "$OfflineDB" [[ "$PWD" == "/p4/${SDP_INSTANCE}/offline_db" ]] || bail "Unexpected OfflineDB value of: $OfflineDB" # Remove db.* and state* files, but not server.id or license files. Cmd="/bin/rm -f db.* state* $OfflineDBUsableFile" msg "Cleaning up databases and offline_db_usable.txt file with this command in $PWD:\n$Cmd" if [[ "${SERVERID^^}" == "P4D_EDGE_"* ]]; then Cmd="time $P4DBIN -r $OfflineDB -K $ExcludedTables -z -jr $Checkpoint" else Cmd="time $P4DBIN -r $OfflineDB -z -jr $Checkpoint" fi msg "Replaying checkpoint to offline_db with this command:\n$Cmd" $Cmd || bail "Checkpoint replay to offline_db failed." Cmd="$P4DBIN -r $OfflineDB -xu" msg "Ensuring databases in offline_db are upgraded with this command:\n$Cmd" $Cmd || bail "Database upgrade in offline_db failed." echo "Database restored successfully." > $OfflineDBUsableFile ||\ bail "Failed to write this filfe: $OfflineDBUsableFile" msg "Starting p4d." $P4DInitScript start sleep 1 msg "Logging in." $P4CBIN/p4login -v # Now that we have loaded a checkpoint, re-load the shell environment to # ensure that P4REPLICA is set correctly, as some db.config settings are # determined dynamically. source /p4/common/bin/p4_vars $SDP_INSTANCE if [[ "$P4REPLICA" == TRUE ]]; then msg "Logging in service user." p4login -v -service sleep 3 Cmd="$P4BIN -s pull -lj" msg "Checking replication status with this command:\n$Cmd" $Cmd || bail "Replica is not replicating properly." fi msg "\nAll processing completed successfully.\n" if [[ -x "$P4BrokerInitScript" && -r "$P4BrokerCfg" ]]; then msg "NOT stating broker. Start it manually when ready with:\n\t$P4BrokerInitScript start\n" fi msg "That took $(($SECONDS/3600)) hours $(($SECONDS%3600/60)) minutes $(($SECONDS%60)) seconds.\n"
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 24292 | gmc | "Forking branch Dev of perforce-software-sdp to gmc-sdp." | ||
//guest/perforce_software/sdp/dev/Server/Unix/p4/common/bin/load_checkpoint.sh | |||||
#1 | 24198 | C. Thomas Tyler |
Added utility script to load a specified checkpoint into both /p4/N/root and /p4/N/offline_db, including logic to managing p4d/p4broker processes, ensure no db.* files exist prior to replaying the checkoint, managing the offline_db_usable.txt file, etc. |