#!/bin/bash
# Copyright (c) 2009 Exemplics LLC
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Version 2009.2
# execution starts at <Initialize>
# <Functions>
function StartProcedure() {
echo '<?xml version="1.0" encoding="UTF-8"?>' > ${xmlOut}
if [[ ${includeStyleSheet} ]]; then
echo '<?xml-stylesheet type="text/xsl" href="PerforceBackupProcedureHTML.xsl"?>' >> ${xmlOut}
fi
echo '<!DOCTYPE BackupProcedure>' >> ${xmlOut}
echo "<BackupProcedure ServerShortName=\"${serverShortName}\" CheckpointNumber=\"${checkpointNumber}\" BackupType=\"${backupType}\">" >> ${xmlOut}
}
function P4Info() {
pushd "${workspace}" > /dev/null
echo ' <P4Info>' >> ${xmlOut}
# Output the P4Info variables
p4 info| sed -n -e 's,\([^:]*\): \(.*\), <Variable Name="\1" Value="\2" />,p' >> ${xmlOut}
echo ' </P4Info>' >> ${xmlOut}
popd > /dev/null
}
function Environment() {
echo ' <Environment>' >> ${xmlOut}
# Replace " characters with equivalent xml entities
export | sed -n -e 's,declare -x \(.*\)="\(.*\)", <Variable Name="\1" Value="\2" />,p' -e 's/\\"/\"/g' >> ${xmlOut}
echo ' </Environment>' >> ${xmlOut}
}
function StartSteps() {
echo ' <Steps>' >> ${xmlOut}
}
function Steps() {
# Verify and checkpoint Perforce database.
# Do not backup if an error occurs here.
Verify
if [[ ${?} != 0 ]]; then return ${?}; fi
SearchVerifyOutput
if [[ ${?} != 0 ]]; then return ${?}; fi
Checkpoint
if [[ ${?} != 0 ]]; then return ${?}; fi
# Restore the checkpoint and verify it.
# Continue to backup even if these fail.
Restore
TestRestore
Backup
return ${?}
}
function Verify() {
pushd "${workspace}" > /dev/null
local startTime=$(date "${dateFormat}")
local output="${verifyOut}"
local command="p4 verify //..."
echo ' <Step Name="Verify">' >> ${xmlOut}
P4Set
echo " <Command CurrentDirectory=\"${PWD}\"><![CDATA[${command}]]></Command>" >> ${xmlOut}
echo ' <ConsoleText><![CDATA[' >> ${xmlOut}
if [[ ${runCommands} ]]; then eval ${command} > "${output}" 2>> ${xmlOut}; fi
local result=${?}
echo ' ]]></ConsoleText>' >> ${xmlOut}
StartOutputs
File "${output}" VerificationResultsFile
EndOutputs
echo " <Summary ResultCode=\"${result}\" StartTime=\"${startTime}\" EndTime=\""$(date "${dateFormat}")"\" />" >> ${xmlOut}
echo ' </Step>' >> ${xmlOut}
popd > /dev/null
return ${result}
}
function SearchVerifyOutput() {
local startTime=$(date "${dateFormat}")
local output="${verifyOut}"
local command="grep '!$' \"${verifyOut}\""
echo ' <Step Name="SearchVerifyOutput">' >> ${xmlOut}
StartInputs
File "${verifyOut}" VerificationResultsFile
EndInputs
echo " <Command CurrentDirectory=\"${PWD}\"><![CDATA[${command}]]></Command>" >> ${xmlOut}
echo ' <ConsoleText><![CDATA[' >> ${xmlOut}
if [[ ${runCommands} ]]; then
eval ${command} >> ${xmlOut} 2>&1
if [[ ${?} -eq 0 ]]; then
# not finding a line ending with ! is a good thing
local result=1
else
local result=0
fi
else
# Succeed if in test mode
local result=0
fi
echo ' ]]></ConsoleText>' >> ${xmlOut}
echo " <Summary ResultCode=\"${result}\" StartTime=\"${startTime}\" EndTime=\""$(date "${dateFormat}")"\" />" >> ${xmlOut}
echo ' </Step>' >> ${xmlOut}
return ${result}
}
function Checkpoint() {
pushd "${workspace}" > /dev/null
local startTime=$(date "${dateFormat}")
local output=${checkpoint}
local command="p4 admin checkpoint -z"
echo ' <Step Name="Checkpoint">' >> ${xmlOut}
P4Set
echo " <Command CurrentDirectory=\"${PWD}\"><![CDATA[${command}]]></Command>" >> ${xmlOut}
echo ' <ConsoleText><![CDATA'[ >> ${xmlOut}
if [[ ${runCommands} ]]; then eval ${command} >> ${xmlOut} 2>&1; fi
local result=${?}
echo ' ]]></ConsoleText>' >> ${xmlOut}
StartOutputs
File "${output}" CheckpointFile
EndOutputs
echo " <Summary ResultCode=\"${result}\" StartTime=\"${startTime}\" EndTime=\""$(date "${dateFormat}")"\" />" >> ${xmlOut}
echo ' </Step>' >> ${xmlOut}
popd > /dev/null
return ${result}
}
function Restore() {
# Test restore the checkpoint
# Start with a clean folder
rm -rf "${restoreOut}" > /dev/null 2>&1
mkdir "${restoreOut}" > /dev/null 2>&1
pushd "${restoreOut}" > /dev/null
local startTime=$(date "${dateFormat}")
local command="p4d -jr -z \"${checkpoint}\""
echo ' <Step Name="Restore">' >> ${xmlOut}
P4Set
StartInputs
File "${checkpoint}" CheckpointFile
EndInputs
echo " <Command CurrentDirectory=\"${PWD}\"><![CDATA[${command}]]></Command>" >> ${xmlOut}
echo ' <ConsoleText><![CDATA[' >> ${xmlOut}
if [[ ${runCommands} ]]; then eval ${command} >> ${xmlOut} 2>&1; fi
local result=${?}
echo ' ]]></ConsoleText>' >> ${xmlOut}
echo " <Summary ResultCode=\"${result}\" StartTime=\"${startTime}\" EndTime=\""$(date "${dateFormat}")"\" />" >> ${xmlOut}
echo ' </Step>' >> ${xmlOut}
popd > /dev/null
return ${result}
}
function TestRestore() {
# Verify test restore of the checkpoint
pushd "${restoreOut}" > /dev/null
local startTime=$(date "${dateFormat}")
echo ' <Step Name="TestRestore">' >> ${xmlOut}
echo " <Command CurrentDirectory=\"${PWD}\"></Command>" >> ${xmlOut}
# Verify directory contents (db.*)
for file in *; do
local actual="${actual} ${file}"
done
local expected=" db.archmap db.boddate db.bodtext db.change db.changex db.counters db.depot db.desc db.domain db.fix db.fixrev db.group db.have db.integ db.integed db.ixdate db.ixtext db.job db.jobdesc db.label db.locks db.logger db.message db.monitor db.protect db.resolve db.rev db.revcx db.revdx db.revhx db.review db.revpx db.revsx db.traits db.trigger db.user db.view db.working"
if [[ "${actual}" == "${expected}" ]]; then
local result=0
else
local result=1
fi
echo " <Summary ResultCode=\"${result}\" StartTime=\"${startTime}\" EndTime=\""$(date "${dateFormat}")"\" Actual=\"${actual}\" Expected=\"${expected}\"/>" >> ${xmlOut}
echo ' </Step>' >> ${xmlOut}
popd > /dev/null
return ${result}
}
function Backup() {
# tar strips the leading / so the archive can be restored to a different directory.
local snapshotFile="${backupDirectory}/${serverShortName}.snar"
# Force this to be a normal (full) backup.
if [[ ${backupType} == normal ]]; then rm -f "${snapshotFile}" >> /dev/null 2>&1; fi
# Can't have an incremental backup without a listed-incremental snapshot file
if [[ ! -f "${snapshotFile}" ]]; then backupType=normal; fi
local licenseFile="${serverRoot}/license"
local job="${serverShortName} ${checkpointNumber}"
local backupFile="${backupDirectory}/${serverShortName}.${checkpointNumber}.${backupType}.tgz"
local backupLog="${outputPathPrefix}.Backup.txt"
local startTime=$(date "${dateFormat}")
local output="${backupFile}"
echo ' <Step Name="Backup">' >> ${xmlOut}
StartInputs
if [[ -f "${licenseFile}" ]]; then
# licensed server
File "${licenseFile}" ServerLicenseFile
local backupItems="\"${licenseFile}\" "
else
local backupItems=""
fi
File "${checkpoint}" CheckpointFile
backupItems="${backupItems}\"${checkpoint}\" "$(p4 depots | \
sed -n \
-e 's,^Depot [^ ]* [0-9/]* [^r].* \(/.*\)/\.\.\. .*,\1,p' \
-e "s,^Depot [^ ]* [0-9/]* [^r].* \(.*\)/\.\.\. .*,${serverRoot}/\1,p" | FilterDirectoryExists)
EndInputs
local command="tar --verbose --create --label=\"${job}\" --file=\"${backupFile}\" --gzip --listed-incremental=\"${snapshotFile}\" "${backupItems}
echo " <Command CurrentDirectory=\"${PWD}\"><![CDATA["${command}"]]></Command>" >> ${xmlOut}
echo ' <ConsoleText><![CDATA[' >> ${xmlOut}
if [[ ${runCommands} ]]; then eval ${command} > "${backupLog}" 2>&1; fi
local result=${?}
echo ' ]]></ConsoleText>' >> ${xmlOut}
StartOutputs
File "${output}" BackupFile
File "${snapshotFile}" BackupSnapshotFile
File "${backupLog}" BackupLogFile
EndOutputs
echo " <Summary ResultCode=\"${result}\" StartTime=\"${startTime}\" EndTime=\""$(date "${dateFormat}")"\" />" >> ${xmlOut}
echo ' </Step>' >> ${xmlOut}
}
function FilterDirectoryExists() {
while read versionedFileTree; do
# Don't echo if directory doesn't exist.
# This can happen if the depot is empty.
if [[ -d ${versionedFileTree} ]]; then
echo "\"${versionedFileTree}\""
Directory "${versionedFileTree}" DepotVersionedFileTree
fi
done
}
function EndSteps() {
echo ' </Steps>' >> ${xmlOut}
}
function Storage() {
echo ' <Storage>' >> ${xmlOut}
Directory "${serverRoot}" ServerRoot
Directory "${logDirectory}" LogDirectory
Directory "${backupDirectory}" BackupDirectory
echo ' </Storage>' >> ${xmlOut}
}
function EndProcedure() {
echo '</BackupProcedure>' >> ${xmlOut}
}
function P4Set() {
echo ' <P4Set>' >> ${xmlOut}
p4 set | sed -n -e 's,\(.*\)=\(.*\), <Variable Name="\1" Value="\2" />,p' >> ${xmlOut}
echo ' </P4Set>' >> ${xmlOut}
}
function StartInputs() {
echo ' <Inputs>' >> ${xmlOut}
}
function File() {
local pathname="${1}"
local kind="${2}"
local bytes=""
if [[ -f "${pathname}" ]]; then
local bytes=$(wc -c "${pathname}" | sed -n 's/ *\([0-9]*\).*/\1/p')
fi
echo " <File Bytes=\"${bytes}\" Pathname=\"${pathname}\" Kind=\"${kind}\" />" >> ${xmlOut}
}
function Directory() {
local pathname="${1}"
local kind="${2}"
local blocks=$(df -b "${pathname}" | sed -n 's/^.* \([0-9][0-9]*\) .*/\1/p')
local bytesFree
let bytesFree=blocks*512
echo " <Directory BytesFree=\"${bytesFree}\" Pathname=\"${pathname}\" Kind=\"${kind}\" />" >> ${xmlOut}
}
function EndInputs() {
echo ' </Inputs>' >> ${xmlOut}
}
function StartOutputs() {
echo ' <Outputs>' >> ${xmlOut}
}
function EndOutputs() {
echo ' </Outputs>' >> ${xmlOut}
}
# </Functions>
# <Initialize>
# Setup the environment
# get the checkpoint number
pushd "${workspace}" > /dev/null
# Set serverRoot in the Environment
eval $(p4 info | sed -n -e 's/^Server root: \(.*\)/export serverRoot="\1"/p')
let lastCheckpointNumber=$(p4 counter journal)
let checkpointNumber=lastCheckpointNumber+1
export checkpointNumber
popd > /dev/null
# These need to be global for use in multiple steps
# Where checkpoint file is located
export checkpoint="${serverRoot}/checkpoint.${checkpointNumber}.gz"
# Path and file name prefix to use for output files.
export outputPathPrefix="${logDirectory}/${serverShortName}.${checkpointNumber}"
# Where p4 verify results will be saved
export verifyOut="${outputPathPrefix}.Verify.txt"
# Where script results will be stored in xml format
export xmlOut="${outputPathPrefix}.BackupProcedure.xml"
# Where a test restore of the checkpoint will be performed
export restoreOut="${outputPathPrefix}.Restore"
export dateFormat="+%a %m/%d/%Y %T"
# </Initialize>
# <Main>
startTime=$(date "${dateFormat}")
StartProcedure
P4Info
Environment
StartSteps
Steps
EndSteps
Storage
echo " <Summary StartTime=\"${startTime}\" EndTime=\""$(date "${dateFormat}")"\" />" >> ${xmlOut}
EndProcedure
# </Main>
| # | Change | User | Description | Committed | |
|---|---|---|---|---|---|
| #2 | 7192 | Philip Kania |
Version 2009.2. Added XSLT and CSS style sheets for diplayign XML results as HTML. Added a switch to the scripts to include in the XML results file a link to the XSLT style sheet. |
||
| #1 | 7182 | Philip Kania | Added version 2009.1 of backup procedure scripts for Microsoft Windows and Unix |