#!/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 # function StartProcedure() { echo '' > ${xmlOut} if [[ ${includeStyleSheet} ]]; then echo '' >> ${xmlOut} fi echo '' >> ${xmlOut} echo "" >> ${xmlOut} } function P4Info() { pushd "${workspace}" > /dev/null echo ' ' >> ${xmlOut} # Output the P4Info variables p4 info| sed -n -e 's,\([^:]*\): \(.*\), ,p' >> ${xmlOut} echo ' ' >> ${xmlOut} popd > /dev/null } function Environment() { echo ' ' >> ${xmlOut} # Replace " characters with equivalent xml entities export | sed -n -e 's,declare -x \(.*\)="\(.*\)", ,p' -e 's/\\"/\"/g' >> ${xmlOut} echo ' ' >> ${xmlOut} } function StartSteps() { echo ' ' >> ${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 ' ' >> ${xmlOut} P4Set echo " " >> ${xmlOut} echo ' > ${xmlOut} if [[ ${runCommands} ]]; then eval ${command} > "${output}" 2>> ${xmlOut}; fi local result=${?} echo ' ]]>' >> ${xmlOut} StartOutputs File "${output}" VerificationResultsFile EndOutputs echo " " >> ${xmlOut} echo ' ' >> ${xmlOut} popd > /dev/null return ${result} } function SearchVerifyOutput() { local startTime=$(date "${dateFormat}") local output="${verifyOut}" local command="grep '!$' \"${verifyOut}\"" echo ' ' >> ${xmlOut} StartInputs File "${verifyOut}" VerificationResultsFile EndInputs echo " " >> ${xmlOut} echo ' > ${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 ' ]]>' >> ${xmlOut} echo " " >> ${xmlOut} echo ' ' >> ${xmlOut} return ${result} } function Checkpoint() { pushd "${workspace}" > /dev/null local startTime=$(date "${dateFormat}") local output=${checkpoint} local command="p4 admin checkpoint -z" echo ' ' >> ${xmlOut} P4Set echo " " >> ${xmlOut} echo ' > ${xmlOut} if [[ ${runCommands} ]]; then eval ${command} >> ${xmlOut} 2>&1; fi local result=${?} echo ' ]]>' >> ${xmlOut} StartOutputs File "${output}" CheckpointFile EndOutputs echo " " >> ${xmlOut} echo ' ' >> ${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 ' ' >> ${xmlOut} P4Set StartInputs File "${checkpoint}" CheckpointFile EndInputs echo " " >> ${xmlOut} echo ' > ${xmlOut} if [[ ${runCommands} ]]; then eval ${command} >> ${xmlOut} 2>&1; fi local result=${?} echo ' ]]>' >> ${xmlOut} echo " " >> ${xmlOut} echo ' ' >> ${xmlOut} popd > /dev/null return ${result} } function TestRestore() { # Verify test restore of the checkpoint pushd "${restoreOut}" > /dev/null local startTime=$(date "${dateFormat}") echo ' ' >> ${xmlOut} echo " " >> ${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 " " >> ${xmlOut} echo ' ' >> ${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 ' ' >> ${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 " " >> ${xmlOut} echo ' > ${xmlOut} if [[ ${runCommands} ]]; then eval ${command} > "${backupLog}" 2>&1; fi local result=${?} echo ' ]]>' >> ${xmlOut} StartOutputs File "${output}" BackupFile File "${snapshotFile}" BackupSnapshotFile File "${backupLog}" BackupLogFile EndOutputs echo " " >> ${xmlOut} echo ' ' >> ${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 ' ' >> ${xmlOut} } function Storage() { echo ' ' >> ${xmlOut} Directory "${serverRoot}" ServerRoot Directory "${logDirectory}" LogDirectory Directory "${backupDirectory}" BackupDirectory echo ' ' >> ${xmlOut} } function EndProcedure() { echo '' >> ${xmlOut} } function P4Set() { echo ' ' >> ${xmlOut} p4 set | sed -n -e 's,\(.*\)=\(.*\), ,p' >> ${xmlOut} echo ' ' >> ${xmlOut} } function StartInputs() { echo ' ' >> ${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 " " >> ${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 " " >> ${xmlOut} } function EndInputs() { echo ' ' >> ${xmlOut} } function StartOutputs() { echo ' ' >> ${xmlOut} } function EndOutputs() { echo ' ' >> ${xmlOut} } # # # 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" # #
startTime=$(date "${dateFormat}") StartProcedure P4Info Environment StartSteps Steps EndSteps Storage echo " " >> ${xmlOut} EndProcedure #