diff --git a/instance-applications/120-ibm-db2u-database/files/CopyDBScripts.sh b/instance-applications/120-ibm-db2u-database/files/CopyDBScripts.sh index 0395ae254..c95d180a3 100644 --- a/instance-applications/120-ibm-db2u-database/files/CopyDBScripts.sh +++ b/instance-applications/120-ibm-db2u-database/files/CopyDBScripts.sh @@ -30,7 +30,7 @@ cp -rp RUN_OnDemandFULL_BKP.sh ${INSTHOME}/bin/ cp -rp runstats_rebind.sh ${INSTHOME}/bin/ cp -rp CreateRoles.sh ${INSTHOME}/bin/ cp -rp grant_check.sh ${INSTHOME}/bin/ -cp -rp reorgTablesIndexesInplace2.sh ${INSTHOME}/bin/ +cp -rp reorgTablesIndexesInplace.sh ${INSTHOME}/bin/ cp -rp extract_authorization.sh ${INSTHOME}/bin cp -rp HADRMON.sh ${INSTHOME}/bin @@ -48,7 +48,7 @@ cp PostBackFlow.sh ${INSTHOME}/Managed cp OwnerCheck.txt ${INSTHOME}/Managed echo -e "\nCopying files to maintenance directory under Instance Home . . . "; -cp -rp reorgTablesIndexesInplace2_maintenance.sh ${INSTHOME}/maintenance/reorgTablesIndexesInplace2.sh +cp -rp reorgTablesIndexesInplace2_maintenance.sh ${INSTHOME}/maintenance/reorgTablesIndexesInplace.sh if [ ! -d ${INSTHOME}/maintenance/logs ] ; then mkdir -p ${INSTHOME}/maintenance/logs echo "${DATETIME}:Creating directory ${INSTHOME}/maintenance/logs" diff --git a/instance-applications/120-ibm-db2u-database/files/DB2_Backup.sh b/instance-applications/120-ibm-db2u-database/files/DB2_Backup.sh index d8f21436c..b5112aeb9 100755 --- a/instance-applications/120-ibm-db2u-database/files/DB2_Backup.sh +++ b/instance-applications/120-ibm-db2u-database/files/DB2_Backup.sh @@ -1,12 +1,9 @@ #!/bin/ksh -set -x - ######################################################### -# DB2_Backup.sh -# -# Things to do: -# Recovery history retention (days) (REC_HIS_RETENTN) = 0 >>> Need to set to 15 days +# DB2_Backup.sh # +# Things to do: +# Recovery history retention (days) (REC_HIS_RETENTN) = 0 >>> Need to set to 15 days # # The cron job on the cluster will supply the needed parameters for this script # If an on demand backup (Full) is required, the DB2_Backup.sh script can be called with the following parameters @@ -18,242 +15,266 @@ set -x # ######################################################### - -. /mnt/backup/bin/.PROPS +# -- Script Usage +if [[ $# -eq 4 ]]; then + typeset -l instance=$1 dbname=$2 + typeset -u INSTANCE=$1 DBNAME=$2 + typeset -i NUM_BACKUPS_TO_KEEP=$3 + typeset -l BKUP_TYPE=$4 -#### COSBACKUPBUCKET=masms-pp-1-cos-backup-pseg-test-pr-wdc -#### For testing -TESTMSG="######## TESTING ###########" -echo ${COSBACKUPBUCKET} -COSBACKUPBUCKET=${CONTAINER} -#### TESTING URL +else + print `tput smso` "Usage! $0 instance database number_of_backups_to_keep" `tput rmso` + exit 1 +fi -Server=`hostname` -instance=`whoami` -FULLIMAGE= -DATETIME=`date +%Y-%m-%d_%H%M%S`; -BACKUP_BASE=/mnt/backup -BACKUP_LOGS=${BACKUP_BASE}/${DB2INSTANCE} -BACKUP_PATH=DB2REMOTE://AWSCOS/${COSBACKUPBUCKET}/backups-manage/${HOSTNAME} -ARCBKP_PATH=${BACKUP_PATH}/${DATETIME} -CLEAN_LOG=${BACKUP_PATH}/.cleanup.log -instance_home=`/usr/local/bin/db2greg -dump | grep -ae "I," | grep -v "/das," | grep "${instance}" | awk -F ',' '{print $5}'| cut -d/ -f 1,2,3,4,5` +# -- Standard Parameters +HOSTNAME=`hostname` +NAMESPACE=`hostname -A | awk -F '.' '{print $3}'` +DBINSTANCE=`whoami` +DATETIME=`date +'%F_%T'`; +INSTANCE_HOME=`/usr/local/bin/db2greg -dump | grep -ae "I," | grep -v "/das," | grep "${DBINSTANCE}" | awk -F ',' '{print $5}'| cut -d/ -f 1,2,3,4,5` IP=`/sbin/ifconfig | grep "inet" | grep broadcast | awk '{print $2}'` -BACK_LOG=$instance_home/bin/.$2_BackupLOG.out +CUSTNAME=`hostname | sed 's/c-db2wh-//; s/c-//; s/-db2u-0//; s/db2u/-/; s/-manage//;' | tr '[:lower:]' '[:upper:]'` +SCRIPT_DIR=${INSTANCE_HOME}/bin + +# -- Source DB2 Profile + +if [[ ! -f "${INSTANCE_HOME}/sqllib/db2profile" ]]; then + echo "ERROR - ${INSTANCE_HOME}/sqllib/db2profile not found" + EXIT_STATUS=1 +else + . ${INSTANCE_HOME}/sqllib/db2profile +fi + +# -- Debug Mode +# set -x; # Uncomment to debug this shell script +# set -n; # Uncomment to check your syntax, without execution. + +# -- Source the PROPS file +. /mnt/backup/bin/.PROPS + +# -- Backup Parameters + +COSBACKUPBUCKET="${CONTAINER}" +BUCKET_ALIAS=`db2 list storage access | grep ${COSBACKUPBUCKET} -B4 | grep ALIAS | awk -F '=' '{print $2}'` +BACKUP_BASE="/mnt/backup" +BACKUP_LOGS=${SCRIPT_DIR}/${DBINSTANCE} +BACKUP_PATH=DB2REMOTE://${BUCKET_ALIAS}/${COSBACKUPBUCKET}/backups-${APPENV}/${HOSTNAME} +ARCBKP_PATH=${BACKUP_LOGS}/${DATETIME} +CLEAN_LOG=${BACKUP_LOGS}/.cleanup.LOG Maillog="/tmp/.backup_maillog" - +BACK_LOG=${SCRIPT_DIR}/.${DBNAME}_BackupLOG.out +ICD_LOG=${SCRIPT_DIR}/.Maillive.log +HSTYPE="Backup" -SLACK_NOTIFY() -{ - des="$instance - Backup - $Server ${database} -- DATABASE Backup issues" - echo "${CUSTNAME} - $instance - Backup - $Server $IP ${database} DATABASE Backup issues" > .Maillive.log - echo "############################" >> .Maillive.log - cat ${BACK_LOG} >> .Maillive.log - longdes=`cat .Maillive.log | sed 's/"//g' | sed "s/'//g"` - slackdes=" BACKUP FAILED for ${Server} - ${CONTAINER}} ...Please investigate " - -### Send Failure notification to a slack channel ## -cat << ! >.curl_$database.sh - curl -X POST -H 'Content-type: application/json' --data '{"text":"$slackdes"}' ${SLACKURL} -! -if [[ -n "${SLACKURL}" ]]; then - /bin/bash .curl_$database.sh > .curl_$database.out 2>&1 +# -- Valid only for MAS-CP4D Customers +if (( ${CUSTNAME} )) ; then + CUSTNAME=`echo ${CONTAINER} | awk -F '-backup-' '{print $2}' | awk -F '-pr-' '{print $1}' | tr '[:lower:]' '[:upper:]'` +fi + +# -- Database Environment +if [[ ${BUCKET_ALIAS} == "IBMCOS" ]]; then + DBENV="MAS MS" +else + DBENV="MAS SaaS" fi - ##### Create ICD Incident #### - ####### If Backup fails ### - des="${CUSTNAME} - ${instance} - Backup - ${HOSTNAME} ${database} - MASMS -- Backup Failed" - echo "############################" >> .Maillive.log - longdes=`cat .Maillive.log | sed 's/"//g' | sed "s/'//g"` - longdes=`echo "
 ${longdes} 
"` - ICD_URL="https://servicedesk.mro.com" - if ! curl -k -s --connect-timeout 3 ${ICD_URL} >/dev/null; then - ICD_URL="https://servicedesk.cds.mro.com" - fi -cat << ! >.curl_${database}_ICD.sh - curl --insecure --location --request POST "${ICD_URL}/maximo_mif/oslc/os/hsincident?lean=1" \ - --header "Authorization: Basic ${ICD_AUTH_KEY}" \ - --header 'Content-Type: application/json' \ - --data '{ - "description":"$des", - "reportedpriority":4, - "internalpriority":4, - "reportedby":"DB2", - "affectedperson":"CTGINST1", - "description_longdescription":"$longdes", - "siteid":"001", - "classstructureid":"1341", - "classificationid":"IN-DBPERF", - "hshost":"{servicedesk-pdb-sjc03-2.cds.mro.com:0:50}", - "hstype":"BACKUP" - }' +# -- Function to send Slack notification + +SLACK_NOTIFY() { + + SLACKDES="$1" + # -- Send Failure notification to a slack channel + cat << ! >.curl_${DBNAME}.sh + curl -X POST -H 'Content-type: application/json' --data '{"text":"${SLACKDES}"}' ${SLACKURL} ! -if [[ -n "${ICD_AUTH_KEY}" ]]; then - /bin/bash .curl_${database}_ICD.sh > .curl_${database}_ICD.out 2>&1 -fi + /bin/bash .curl_${DBNAME}.sh > .curl_${DBNAME}.out 2>&1 } +# -- Create ICD Incident , If Backup fails +CREATE_ICD() { + HTYPE=`echo ${HSTYPE} | tr '[:lower:]' '[:upper:]'` + DES="$1" +# echo "############################" >> ${ICD_LOG} + LONGDES=`cat ${ICD_LOG} | sed 's/"//g' | sed "s/'//g"` + LONGDES=`echo "
 ${LONGDES} 
"` -if [ ! -f "$instance_home/sqllib/db2profile" ] -then - echo "ERROR - $instance_home/sqllib/db2profile not found" - EXIT_STATUS=1 -else - . $instance_home/sqllib/db2profile -fi + # -- Verify the ICD Status + if curl -k -s --connect-timeout 3 ${ICD_URL_SAAS} >/dev/null; then + CURL_REQ="--request POST --url ${ICD_URL_SAAS} " + AUTH_REQ="apikey: ${ICD_API_KEY}" + fi + # -- Generate Curl Syntax to push to ICD + cat << ! >.curl_${DBNAME}_ICD.sh + curl ${CURL_REQ} \ + --header '${AUTH_REQ}' \ + --header 'Content-Type: application/json' \ + --data '{ + "description":"${DES}", + "reportedpriority":4, + "internalpriority":4, + "reportedby":"DB2", + "affectedperson":"${DBENV}", + "ownergroup":"HSDBA", + "description_longdescription":"${LONGDES}", + "siteid":"001", + "classstructureid":"1341", + "classificationid":"IN-DBPERF", + "hshost":"${HOSTNAME}", + "hstype":"${HTYPE}" + }' +! + /bin/bash .curl_${DBNAME}_ICD.sh > .curl_${DBNAME}_ICD.out 2>&1 + +} -if [ -f $Maillog ] -then - rm $Maillog +# -- Delete old log file +if [[ -f $Maillog ]]; then + rm $Maillog fi -echo "COS bucket = ${COSBACKUPBUCKET} " > $BACK_LOG -echo "BACKUP Start time : ${DATETIME}" >> $BACK_LOG -echo " " >> $BACK_LOG -echo ${HOSTNAME} >> $BACK_LOG -echo " " >> $BACK_LOG -echo " " >> $BACK_LOG - -if [[ $# -eq 4 ]] -then - typeset -l instance=$1 database=$2 - typeset -u INSTANCE=$1 DATABASE=$2 - typeset -i num_backups_to_keep=$3 - typeset -l BKUP_TYPE=$4 - - ##### until the db is bounced to pickup the TRACKMOD parm..We have to hardcode a FULL backup - #####BKUP_TYPE=full - ### BKUP_TYPE = full or inc #### -else - print `tput smso` "Usage! $0 instance database number_of_backups_to_keep" `tput rmso` - exit 1 +# -- Create the backup log directory if it doesnt exists +if [[ ! -d ${BACKUP_LOGS} ]]; then + mkdir -m 755 ${BACKUP_LOGS} fi - -### Check for the existance of /home/ctginst1/sqllib/db2dump/libdb2compr.so...if it exists, delete it -COMPRESS_LOC=$instance_home/sqllib/db2dump/libdb2compr.so -if [[ -f ${COMPRESS_LOC} ]] -then + +# -- Setting backup type +if [[ ${BKUP_TYPE} == 'full' ]]; then + BKPTYPE="FULL" +else + BKPTYPE="DIFF" +fi + +# -- Script Execution starts from here + +echo -e "\n-------------------------------------" | tee ${BACK_LOG} +echo -e "Backup Start Time \t :: ${DATETIME}" | tee -a ${BACK_LOG} +echo -e "COS Bucket \t\t :: ${COSBACKUPBUCKET}" | tee -a ${BACK_LOG} +echo -e "\nHostname \t\t :: ${HOSTNAME}" | tee -a ${BACK_LOG} +echo -e "Namespace \t\t :: ${NAMESPACE}" | tee -a ${BACK_LOG} +echo -e "HostIP \t\t\t :: ${IP}" | tee -a ${BACK_LOG} +echo -e "-------------------------------------\n" | tee -a ${BACK_LOG} + +# -- Check for the existance of /home/ctginst1/sqllib/db2dump/libdb2compr.so...if it exists, delete it +COMPRESS_LOC=${INSTANCE_HOME}/sqllib/db2dump/libdb2compr.so +if [[ -f ${COMPRESS_LOC} ]]; then rm ${COMPRESS_LOC} fi -### Check to see if the database is Running -ps -ef | grep db2sys | grep -v grep > /dev/null 2>&1 -if [ $? -eq 1 ]; then - echo "Database is not active " - echo "$Server,Database Not Active,BACKUP Not Run" > $instance_home/bin/LASTbkupRUN - - ### Send error alert - SLACK_NOTIFY +# -- Check to see if the Instance is up and Running +ps -ef | grep db2sysc | grep -v grep > /dev/null 2>&1 +if [[ $? -eq 1 ]]; then + #echo "Instance is not active " + echo "Instance is not Active, Backup cannot Initiate !!!" | tee ${INSTANCE_HOME}/bin/LASTbkupRUN ${ICD_LOG} >/dev/null +# echo "############################" >> ${ICD_LOG} + cat ${BACK_LOG} >> ${ICD_LOG} + + SLACKDES="${CUSTNAME} - ${DBENV} - ${HOSTNAME}, Instance is not Active, Backup cannot Initiate !! " + DES="${CUSTNAME} - ${DBENV} - ${DBNAME} - ${HOSTNAME} -- Instance is not Active, Backup cannot Initiate !! " + + # -- Send error notification to Slack + SLACK_NOTIFY "${SLACKDES}" + + # -- Create ICD ticket if fails + CREATE_ICD "${DES}" + + # -- End the script execution exit fi - -### Check to see if the database is HADR -db2pd -hadr -db ${database} | awk -F= '/HADR_ROLE/ {print $2}' | grep STANDBY > /dev/null 2>&1 -if [ $? -eq 0 ]; then - echo "This is a HADR database" - echo "Backup successful. The timestamp for this backup image is : HADR_DB" - echo "$Server,HADR,NO BACKUPS" > $instance_home/bin/LASTbkupRUN - exit 0 -fi - -### Create the backup directory if it doesnt already exist -if [[ -d $BACKUP_LOGS ]] -then : - else mkdir -m 755 ${BACKUP_LOGS} + +# -- Verify whether Database is Standby +db2pd -hadr -db ${DBNAME} | awk -F= '/HADR_ROLE/ {print $2}' | grep STANDBY > /dev/null 2>&1 +if [[ $? -eq 0 ]]; then + echo "This is a HADR Database" + echo "Backup successful. The timestamp for this backup image is : HADR_DB" + echo "${HOSTNAME}, HADR, NO BACKUPS" > ${INSTANCE_HOME}/bin/LASTbkupRUN + exit 0 fi -echo "BACKUP Start time : ${DATETIME}" -echo " " -echo $Server -echo " " -echo " " - -db2 -v archive log for db $database | tee -a $BACK_LOG -sleep 30 - -if [[ $num_backups_to_keep -gt 0 ]] -then - ### Backup database - if [ ${BKUP_TYPE} = 'full' ] ; then - db2 -v backup db $database online to $BACKUP_PATH compress UTIL_IMPACT_PRIORITY 50 include logs without prompting | tee -a $BACK_LOG +# -- Archive the logs for Database +db2 -v "ARCHIVE LOG FOR DB ${DBNAME}" | tee -a ${BACK_LOG} +sleep 20 + +# -- Starting backup for the database +if [[ ${NUM_BACKUPS_TO_KEEP} -gt 0 ]]; then + + if [[ ${BKUP_TYPE} = 'full' ]]; then + db2 -v "BACKUP DB ${DBNAME} ONLINE TO ${BACKUP_PATH} COMPRESS UTIL_IMPACT_PRIORITY 50 INCLUDE LOGS WITHOUT PROMPTING" | tee -a ${BACK_LOG} else - db2 -v backup db $database online INCREMENTAL DELTA to $BACKUP_PATH compress UTIL_IMPACT_PRIORITY 50 include logs without prompting | tee -a $BACK_LOG + db2 -v "BACKUP DB ${DBNAME} ONLINE INCREMENTAL DELTA TO ${BACKUP_PATH} COMPRESS UTIL_IMPACT_PRIORITY 50 INCLUDE LOGS WITHOUT PROMPTING" | tee -a ${BACK_LOG} fi - grep -Fq "Backup successful." $BACK_LOG - if [ $? = 0 ]; then - Backup_timestamp=`grep timestamp $BACK_LOG | cut -d: -f2` - - ### Need to find all the files associate with the backup ## - ### - ### - ### Need to change this to the db2RemStgManager command to get a list of all backup images just created - ### - ### - - # fi - else - SLACK_NOTIFY - #exit + + grep -Fq "Backup successful." ${BACK_LOG} + if [[ $? -ne 0 ]]; then + echo "${CUSTNAME} - ${DBENV} - ${HOSTNAME} - ${BKPTYPE} ${HSTYPE} Failed !!!" > ${ICD_LOG} + # echo "############################" >> ${ICD_LOG} + cat ${BACK_LOG} >> ${ICD_LOG} + + SLACKDES="${CUSTNAME} - ${DBENV} - ${HOSTNAME} -- ${BKPTYPE} ${HSTYPE} Failed . . . Please investigate ! ! ! " + DES="${CUSTNAME} - ${DBENV} - ${DBNAME} - ${HOSTNAME} -- ${BKPTYPE} ${HSTYPE} Failed !" + + # -- Send error notification to Slack + SLACK_NOTIFY "${SLACKDES}" + # -- Create ICD ticket if fails + CREATE_ICD "${DES}" fi fi -######## Copy keystore to COS -set -x +# -- Copy keystore to COS SOURCE1=/mnt/blumeta0/db2/keystore/keystore.p12 SOURCE2=/mnt/blumeta0/db2/keystore/keystore.sth -TARGET1=backups-manage/${HOSTNAME}/KEYSTORE/keystore.p12 -TARGET2=backups-manage/${HOSTNAME}/KEYSTORE/keystore.sth +TARGET1=backups-${APPENV}/${HOSTNAME}/KEYSTORE/keystore.p12 +TARGET2=backups-${APPENV}/${HOSTNAME}/KEYSTORE/keystore.sth DB2V=`db2level | grep Inform | awk '{print $5}' | sed 's/",//'` -if [ ${DB2V} = "v11.5.7.0" ] -then - db2RemStgManager S3 put server=${SERVER} auth1=${PARM1} auth2=${PARM2} container=${CONTAINER} source=${SOURCE1} target=${TARGET1} - db2RemStgManager S3 put server=${SERVER} auth1=${PARM1} auth2=${PARM2} container=${CONTAINER} source=${SOURCE2} target=${TARGET2} +if [[ ${DB2V} == "v11.5.7.0" ]]; then + + db2RemStgManager S3 put server=${HOSTNAME} auth1=${PARM1} auth2=${PARM2} container=${CONTAINER} source=${SOURCE1} target=${TARGET1} + db2RemStgManager S3 put server=${HOSTNAME} auth1=${PARM1} auth2=${PARM2} container=${CONTAINER} source=${SOURCE2} target=${TARGET2} + else - db2RemStgManager ALIAS PUT source=${SOURCE1} target=DB2REMOTE://AWSCOS//${TARGET1} - db2RemStgManager ALIAS PUT source=${SOURCE2} target=DB2REMOTE://AWSCOS//${TARGET2} + db2RemStgManager ALIAS PUT source=${SOURCE1} target=DB2REMOTE://${BUCKET_ALIAS}//${TARGET1} + db2RemStgManager ALIAS PUT source=${SOURCE2} target=DB2REMOTE://${BUCKET_ALIAS}//${TARGET2} fi -# exclude files that arent backups, e.g. backhist listing. -typeset -i no_backups=`./CheckCOS.sh | grep -i ${database}| cut -d/ -f3| grep 001 |wc -l` -echo " number of backups $no_backups" -### Prune the history file, if and only if the last backup succeeded. -### Remove archive transaction logs for expired backups, if there are a requisite number of successful backups. -### Remove expired backups in step. - -if [[ $num_backups_to_keep -gt 0 && $no_backups -ge $num_backups_to_keep ]] -then - db2 -v connect to $database | tee -a $BACK_LOG +# -- Exclude files that arent backups, e.g. backhist listing. + +typeset -i NO_BACKUPS=`~/bin/CheckCOS.sh | grep -i ${DBNAME} | cut -d/ -f3 | grep 001 | wc -l` +echo "Number of Backups in the bucket :: ${NO_BACKUPS}" + +# -- Prune the history file, if and only if the last backup succeeded. - timestmp=$(db2 -x "select coalesce(max(start), 17890713235959) from \ - (select bigint(start_time) - 1 as start, \ - row_number() over(order by start_time desc) as backup \ - from sysibmadm.db_history \ - where operation = 'B' \ - and objecttype = 'D' \ - and devicetype = 'D' \ - and sqlcode is null \ - and sqlwarn is null \ - ) as zzz \ - where backup = $num_backups_to_keep" ) - - db2 -v prune history $timestmp WITH FORCE OPTION and delete | tee -a $BACK_LOG - - ### loop until the recovery history file is stable and then report it +if [[ ${NUM_BACKUPS_TO_KEEP} -gt 0 && ${NO_BACKUPS} -ge ${NUM_BACKUPS_TO_KEEP} ]]; then + db2 -v CONNECT TO ${DBNAME} | tee -a ${BACK_LOG} + + TIMESTMP=$(db2 -x "select coalesce(max(start), 17890713235959) from \ + (select bigint(start_time) - 1 as start, \ + row_number() over(order by start_time desc) as backup \ + from sysibmadm.db_history \ + where operation = 'B' \ + and objecttype = 'D' \ + and devicetype = 'D' \ + and sqlcode is null \ + and sqlwarn is null \ + ) as zzz \ + where backup = ${NUM_BACKUPS_TO_KEEP}" ) + + db2 -v "PRUNE HISTORY ${TIMESTMP} WITH FORCE OPTION AND DELETE" | tee -a ${BACK_LOG} + + # -- Loop until the recovery history file is stable and then report it RC=999 typeset -i no_loops=0 while [[ $RC -gt 0 ]] do - db2 -v list history backup since $timestmp for $database > ${BACKUP_LOGS}/backhist + db2 -v list history backup since ${TIMESTMP} for ${DBNAME} > ${BACKUP_LOGS}/backhist RC=$? print RC for list history was $RC - cat ${BACKUP_LOGS}/backhist >> $BACK_LOG - if [[ $no_loops -gt 720 ]] - then - ### then youve been waiting an hour + cat ${BACKUP_LOGS}/backhist >> ${BACK_LOG} + if [[ $no_loops -gt 720 ]]; then + # -- then youve been waiting an hour print $0 "Im tired of waiting for the recovery history file to stabilise. Im giving up" break else @@ -261,44 +282,39 @@ then let no_loops=no_loops+1 fi done - echo "Content of backhist file:" + + echo "Content of backhist file: " cat ${BACKUP_LOGS}/backhist - db2 -v commit | tee -a $BACK_LOG - - db2 -v connect reset | tee -a $BACK_LOG - db2 -v terminate | tee -a $BACK_LOG + db2 -v commit | tee -a ${BACK_LOG} + db2 -v connect reset | tee -a ${BACK_LOG} + db2 -v terminate | tee -a ${BACK_LOG} fi -sleep 30 - -if [[ $num_backups_to_keep -eq 0 ]] -then - db2 -v connect to $database | tee -a $BACK_LOG - db2 -x "select location \ - from sysibmadm.db_history \ - where operation = 'X' \ - and operationtype = '1' " > $BACKUP_LOGS/archivelog.zaplist - for log in `cat $BACKUP_LOGS/archivelog.zaplist` +sleep 20 + +# -- Prune the archive logs and archive log history file +if [[ ${NUM_BACKUPS_TO_KEEP} -eq 0 ]]; then + db2 -v connect to ${DBNAME} | tee -a ${BACK_LOG} + db2 -x "select location from sysibmadm.db_history where operation = 'X' and operationtype = '1' " > ${BACKUP_LOGS}/archivelog.zaplist + + for LOG in `cat ${BACKUP_LOGS}/archivelog.zaplist` do - printf "`date +'%F %T'`\t%-110s\t%12d k\n" "${log}" "`du -sk ${log} | awk '{print $1}'`" >> ${CLEAN_LOG} + printf "`date +'%F %T'`\t%-110s\t%12d k\n" "${LOG}" "`du -sk ${LOG} | awk '{print $1}'`" >> ${CLEAN_LOG} done - ### prune history in step - timestmp=$(db2 -x "select max(start_time) from sysibmadm.db_history where operation = 'X' and operationtype = '1'") - db2 -v prune history $timestmp WITH FORCE OPTION and delete | tee -a $BACK_LOG + + # -- prune history in step + TIMESTMP=$(db2 -x "select max(start_time) from sysibmadm.db_history where operation = 'X' and operationtype = '1'") + db2 -v PRUNE HISTORY ${TIMESTMP} WITH FORCE OPTION AND DELETE | tee -a ${BACK_LOG} wait - db2 -v commit | tee -a $BACK_LOG - db2 -v connect reset | tee -a $BACK_LOG - db2 -v terminate | tee -a $BACK_LOG + db2 -v commit | tee -a ${BACK_LOG} + db2 -v connect reset | tee -a ${BACK_LOG} + db2 -v terminate | tee -a ${BACK_LOG} fi - + DATETIME=`date +%Y-%m-%d_%H%M%S`; -echo "BACKUP End time : ${DATETIME}" >> $BACK_LOG +echo "BACKUP End time :: ${DATETIME}" >> ${BACK_LOG} -if [[ ${BKUP_STATUS} -gt 0 ]] -then - ### Send error alert - SLACK_NOTIFY -fi +# -- Copy the current backup LOG to the Backup LOG history file +cat ${BACK_LOG} >> ${BACKUP_LOGS}/.BackupLOG -### Copy the current backup log to the Backup log history file -cat $BACK_LOG >> ${BACKUP_LOGS}/.BackupLOG +# -- END OF SCRIPT \ No newline at end of file diff --git a/instance-applications/120-ibm-db2u-database/files/RUN_OnDemandFULL_BKP.sh b/instance-applications/120-ibm-db2u-database/files/RUN_OnDemandFULL_BKP.sh index f8b97fd73..7124e9f80 100755 --- a/instance-applications/120-ibm-db2u-database/files/RUN_OnDemandFULL_BKP.sh +++ b/instance-applications/120-ibm-db2u-database/files/RUN_OnDemandFULL_BKP.sh @@ -1,103 +1,132 @@ #!/bin/bash -#set -x - ######################################################### -# Run_Backup.sh -# Run_Backup.sh will be called from the Cron Jobs +# RUN_OnDemandFULL_BKP.sh +# RUN_OnDemandFULL_BKP.sh will be called by OnDemand Jobs # This script will list all local databases running in the instance on a node. It will call the # DB2_Backup.sh script to run a backup for each running database. # Variables are set at the top of the DB2_Backup.sh script to determine if a full backup needs to be run # based on the day of the week. Currently, Saturday is when the full backup runs, incremental backups run # every all other days. # -# Variables to be set +# Variables to be set # SLACKURL = The channel were notifications are send -# BACKUP_SCRIPT = The backup script that Run_Backup.sh calls +# BACKUP_SCRIPT = The backup script that RUN_OnDemandFULL_BKP.sh calls # DAYOFFULL = Defines the day of the week that the full backup will on on (must match the same format as the output from `date`) # NUMOFBKUPTOKEEP = This defines the number of days to keep a backup image on local disk # -# Variables determined by the environment +# Variables determined by the environment # BACKUPTYPE = Is determined from the `date` command and the DAYOFFULL value # DB2INSTANCE = Pulled from the environment # HOSTNAME # DBNAME = Pulled from the `db2 list db directory` # -# Backup command issued +# Backup command issued # ./DB2_Backup.sh ${DB2INSTANCE} ${DBNAME} ${NUMOFBKUPTOKEEP} ${BACKUPTYPE} 2>>.BackupLOG.stderr > .BackupLOG.out ######################################################### +# -- Source the PROPS File . /mnt/backup/bin/.PROPS +# -- Standard Parameters DBINSTANCE=`whoami` HOSTNAME=`hostname` -BACKUP_DIR=${HOME}/bin -BACKUP_SCRIPT=DB2_Backup.sh +INSTANCE_HOME=`/usr/local/bin/db2greg -dump | grep -ae "I," | grep -v "/das," | grep "${DBINSTANCE}" | awk -F ',' '{print $5}'| cut -d/ -f 1,2,3,4,5` +SCRIPT_DIR=${INSTANCE_HOME}/bin DATETIME=`date +%Y-%m-%d_%H%M%S`; -if [ ! -f "${HOME}/sqllib/db2profile" ] -then - echo "ERROR - ${HOME}/sqllib/db2profile not found" +# -- Verify and source db2profile + +if [[ ! -f "${INSTANCE_HOME}/sqllib/db2profile" ]]; then + echo "ERROR - ${INSTANCE_HOME}/sqllib/db2profile not found" EXIT_STATUS=1 else - . ${HOME}/sqllib/db2profile + . ${INSTANCE_HOME}/sqllib/db2profile +fi + +# -- Debug Mode +# set -x; # Uncomment to debug this shell script +# set -n; # Uncomment to check your syntax, without execution. + +# -- Backup parameters +BACKUPTYPE=full +BACKUP_SCRIPT="${SCRIPT_DIR}/DB2_Backup.sh" +CUSTNAME=`hostname | sed 's/c-db2wh-//; s/c-//; s/-db2u-0//; s/db2u/-/; s/-manage//;' | tr '[:lower:]' '[:upper:]'` +BUCKET_ALIAS=`db2 list storage access | grep ${CONTAINER} -B4 | grep ALIAS | awk -F '=' '{print $2}'` +HSTYPE="Backup" +ICD_LOG=${SCRIPT_DIR}/.Maillive.log + +# -- Valid only for MAS-CP4D customers +if (( ${CUSTNAME} )) ; then + CUSTNAME=`echo ${CONTAINER} | awk -F '-backup-' '{print $2}' | awk -F '-pr-' '{print $1}' | tr '[:lower:]' '[:upper:]'` +fi + +# -- Database Environment +if [[ ${BUCKET_ALIAS} == "IBMCOS" ]]; then + DBENV="MAS MS" +else + DBENV="MAS SaaS" fi +# -- Create ICD Incident , If Backup fails -DOW=`date | awk '{print $1}'` +CREATE_ICD() { + HTYPE=`echo ${HSTYPE} | tr '[:lower:]' '[:upper:]'` + DES="$1" +# echo "############################" >> ${ICD_LOG} + LONGDES=`cat ${ICD_LOG} | sed 's/"//g' | sed "s/'//g"` + LONGDES=`echo "
 ${LONGDES} 
"` -# if [ ${DOW} = ${DAYOFFULL} ] ; then - BACKUPTYPE=full -# else -# BACKUPTYPE=inc -# fi + # -- Verify the ICD URL Status + if curl -k -s --connect-timeout 3 ${ICD_URL_SAAS} >/dev/null; then + CURL_REQ="--request POST --url ${ICD_URL_SAAS} " + AUTH_REQ="apikey: ${ICD_API_KEY}" + fi + + # -- Generate Curl Syntax to push to ICD + cat << ! >.curl_${DBNAME}_ICD.sh + curl ${CURL_REQ} \ + --header '${AUTH_REQ}' \ + --header 'Content-Type: application/json' \ + --data '{ + "description":"${DES}", + "reportedpriority":4, + "internalpriority":4, + "reportedby":"DB2", + "affectedperson":"${DBENV}", + "ownergroup":"HSDBA", + "description_longdescription":"${LONGDES}", + "siteid":"001", + "classstructureid":"1341", + "classificationid":"IN-DBPERF", + "hshost":"${HOSTNAME}", + "hstype":"${HTYPE}" + }' +! + /bin/bash .curl_${DBNAME}_ICD.sh > .curl_${DBNAME}_ICD.out 2>&1 + +} + +# -- Loop through the available databases in the instance DBS=`db2 list db directory | grep -B5 "Indirect" | grep "Database name" | awk '{ print $4 }'` for DBNAME in ${DBS} do - cd ${BACKUP_DIR} - ./DB2_Backup.sh ${DB2INSTANCE} ${DBNAME} ${NUMOFBKUPTOKEEP} ${BACKUPTYPE} 2>.BackupLOG.stderr > .BackupLOG.out + cd ${SCRIPT_DIR} + ${BACKUP_SCRIPT} ${DB2INSTANCE} ${DBNAME} ${NUMOFBKUPTOKEEP} ${BACKUPTYPE} 2>.BackupLOG.stderr > .BackupLOG.out + RC=$? + if [[ ${RC} -ne 0 ]]; then - RC=$? - if [ ${RC} -ne 0 ]; then - - longdes="Failure to start the Backup job ${DATETIME} CUST=${CUSTNAME} ${RC}" - ## Send Failure notification to a slack channel ## - cat << ! >.curl_${DBNAME}_RUN.sh - curl -X POST -H 'Content-type: application/json' --data '{"text":"$longdes"}' $SLACKURL -! -/bin/bash .curl_${DBNAME}_RUN.sh > .curl_${DBNAME}_RUN.out 2>&1 - - ##### Create ICD Incident #### - ####### If Backup fails ### - des="${DBINSTANCE} - Backup - ${HOSTNAME} ${DBNAME} ${CUSTNAME} - MASMS -- Backup Failed" - echo "TESTING $instance - Backup - $ ${DBNAME} - Backup Failed" > .Maillive.log - echo "############################" >> .Maillive.log - #cat $BACK_LOG >> .Maillive.log - longdes=`cat .Maillive.log | sed 's/"//g' | sed "s/'//g"` - ICD_URL="https://servicedesk.mro.com" - if ! curl -k -s --connect-timeout 3 ${ICD_URL} >/dev/null; then - ICD_URL="https://servicedesk.cds.mro.com" - fi - -cat << ! >.curl_${DBNAME}_ICD.sh - curl --insecure --location --request POST "${ICD_URL}/maximo_mif/oslc/os/hsincident?lean=1" \ - --header "Authorization: Basic ${ICD_AUTH_KEY}" \ - --header 'Content-Type: application/json' \ - --data '{ - "description":"$des", - "reportedpriority":4, - "internalpriority":4, - "reportedby":"DB2", - "affectedperson":"CTGINST1", - "description_longdescription":"$longdes", - "siteid":"001", - "classstructureid":"1341", - "classificationid":"IN-DBPERF", - "hshost":"{servicedesk-pdb-sjc03-2.cds.mro.com:0:50}", - "hstype":"BACKUP" - }' + LONGDES="Failure to start the Backup job ${DATETIME} CUST=${CUSTNAME} - ${RC}" + # -- Send Failure notification to a slack channel + cat << ! >.curl_${DBNAME}_RUN.sh + curl -X POST -H 'Content-type: application/json' --data '{"text":"$LONGDES"}' ${SLACKURL} ! -#####/bin/bash .curl_${DBNAME}_ICD.sh > .curl_${DBNAME}_ICD.out 2>&1 + /bin/bash .curl_${DBNAME}_RUN.sh > .curl_${DBNAME}_RUN.out 2>&1 -fi + # -- Create ICD ticket if fails + DES="${CUSTNAME} - ${DBENV} - ${DBNAME} - ${HOSTNAME} -- Failed to Start Backup!! " + CREATE_ICD "${DES}" + fi done + +# -- END OF SCRIPT \ No newline at end of file diff --git a/instance-applications/120-ibm-db2u-database/files/Run_Backup.sh b/instance-applications/120-ibm-db2u-database/files/Run_Backup.sh index 72f29f0c8..21bfe794d 100755 --- a/instance-applications/120-ibm-db2u-database/files/Run_Backup.sh +++ b/instance-applications/120-ibm-db2u-database/files/Run_Backup.sh @@ -1,8 +1,6 @@ #!/bin/bash -#set -x - ######################################################### -# Run_Backup.sh +# Run_Backup.sh # Run_Backup.sh will be called from the Cron Jobs # This script will list all local databases running in the instance on a node. It will call the # DB2_Backup.sh script to run a backup for each running database. @@ -10,100 +8,143 @@ # based on the day of the week. Currently, Saturday is when the full backup runs, incremental backups run # every all other days. # -# Variables to be set +# Variables to be set # SLACKURL = The channel were notifications are send # BACKUP_SCRIPT = The backup script that Run_Backup.sh calls # DAYOFFULL = Defines the day of the week that the full backup will on on (must match the same format as the output from `date`) # NUMOFBKUPTOKEEP = This defines the number of days to keep a backup image on local disk # -# Variables determined by the environment -# BACKUPTYPE = Is determined from the `date` command and the DAYOFFULL value +# Variables determined by the environment +# BKPTYPE = Is determined from the `date` command and the DAYOFFULL value # DB2INSTANCE = Pulled from the environment # HOSTNAME # DBNAME = Pulled from the `db2 list db directory` # -# Backup command issued -# ./DB2_Backup.sh ${DB2INSTANCE} ${DBNAME} ${NUMOFBKUPTOKEEP} ${BACKUPTYPE} 2>>.BackupLOG.stderr > .BackupLOG.out +# Backup command issued +# ./DB2_Backup.sh ${DB2INSTANCE} ${DBNAME} ${NUMOFBKUPTOKEEP} ${BKPTYPE} 2>>.BackupLOG.stderr > .BackupLOG.out +# +# -- Revision of script to include new ICD URL ######################################################### +# -- Source the Props File . /mnt/backup/bin/.PROPS +# -- Standard Parameters DBINSTANCE=`whoami` HOSTNAME=`hostname` -BACKUP_DIR=${HOME}/bin -BACKUP_SCRIPT=DB2_Backup.sh DATETIME=`date +%Y-%m-%d_%H%M%S`; +DOW=`date | awk '{print $1}'` -if [ ! -f "${HOME}/sqllib/db2profile" ] -then +# -- Verify and source db2profile + +if [[ ! -f "${HOME}/sqllib/db2profile" ]]; then echo "ERROR - ${HOME}/sqllib/db2profile not found" EXIT_STATUS=1 else . ${HOME}/sqllib/db2profile fi +# -- Debug Mode +# set -x; # Uncomment to debug this shell script +# set -n; # Uncomment to check your syntax, without execution. -DOW=`date | awk '{print $1}'` +# -- Backup Parameters +INSTANCE_HOME=`/usr/local/bin/db2greg -dump | grep -ae "I," | grep -v "/das," | grep "${DBINSTANCE}" | awk -F ',' '{print $5}'| cut -d/ -f 1,2,3,4,5` +SCRIPT_DIR=${INSTANCE_HOME}/bin +BACKUP_SCRIPT="${SCRIPT_DIR}/DB2_Backup.sh" +CUSTNAME=`hostname | sed 's/c-db2wh-//; s/c-//; s/-db2u-0//; s/db2u/-/; s/-manage//;' | tr '[:lower:]' '[:upper:]'` +BUCKET_ALIAS=`db2 list storage access | grep ${CONTAINER} -B4 | grep ALIAS | awk -F '=' '{print $2}'` +HSTYPE="Backup" +ICD_LOG=${SCRIPT_DIR}/.Maillive.log + +# -- Valid only for MAS-CP4D customers +if (( ${CUSTNAME} )) ; then + CUSTNAME=`echo ${CONTAINER} | awk -F '-backup-' '{print $2}' | awk -F '-pr-' '{print $1}' | tr '[:lower:]' '[:upper:]'` +fi + +# -- Database Environment +if [[ ${BUCKET_ALIAS} == "IBMCOS" ]]; then + DBENV="MAS MS" +else + DBENV="MAS SaaS" +fi + +# -- Create ICD Incident , If Backup fails + +CREATE_ICD() { + HTYPE=`echo ${HSTYPE} | tr '[:lower:]' '[:upper:]'` + DES="$1" +# echo "############################" >> ${ICD_LOG} + LONGDES=`cat ${ICD_LOG} | sed 's/"//g' | sed "s/'//g"` + LONGDES=`echo "
 ${LONGDES} 
"` + + # -- Verify the ICD URL Status + if curl -k -s --connect-timeout 3 ${ICD_URL_SAAS} >/dev/null; then + CURL_REQ="--request POST --url ${ICD_URL_SAAS} " + AUTH_REQ="apikey: ${ICD_API_KEY}" + fi - if [ ${DOW} = ${DAYOFFULL} ] ; then - BACKUPTYPE=full - else - BACKUPTYPE=inc - fi + # -- Generate Curl Syntax to push to ICD + cat << ! >.curl_${DBNAME}_ICD.sh + curl ${CURL_REQ} \ + --header '${AUTH_REQ}' \ + --header 'Content-Type: application/json' \ + --data '{ + "description":"${DES}", + "reportedpriority":4, + "internalpriority":4, + "reportedby":"DB2", + "affectedperson":"${DBENV}", + "ownergroup":"HSDBA", + "description_longdescription":"${LONGDES}", + "siteid":"001", + "classstructureid":"1341", + "classificationid":"IN-DBPERF", + "hshost":"${HOSTNAME}", + "hstype":"${HTYPE}" + }' +! + /bin/bash .curl_${DBNAME}_ICD.sh > .curl_${DBNAME}_ICD.out 2>&1 + +} + +# -- Verify the day of the week +if [[ ${DOW} = ${DAYOFFULL} ]] ; then + BKPTYPE="FULL" +else + BKPTYPE="DIFF" +fi + +# -- Loop through the available databases in the instance DBS=`db2 list db directory | grep -B5 "Indirect" | grep "Database name" | awk '{ print $4 }'` for DBNAME in ${DBS} do - cd ${BACKUP_DIR} - ./DB2_Backup.sh ${DB2INSTANCE} ${DBNAME} ${NUMOFBKUPTOKEEP} ${BACKUPTYPE} 2>.BackupLOG.stderr > .BackupLOG.out - - RC=$? - if [ ${RC} -ne 0 ]; then - - longdes="Failure to start the Backup job ${DATETIME} CUST=${CUSTNAME} ${RC}" - ## Send Failure notification to a slack channel ## - cat << ! >.curl_${DBNAME}_RUN.sh - if [[ -n "${SLACKURL}" ]]; then - curl -X POST -H 'Content-type: application/json' --data '{"text":"$longdes"}' $SLACKURL - fi + cd ${SCRIPT_DIR} + ${BACKUP_SCRIPT} ${DB2INSTANCE} ${DBNAME} ${NUMOFBKUPTOKEEP} ${BKPTYPE} 2>.BackupLOG.stderr > .BackupLOG.out + RC=$? + if [[ ${RC} -ne 0 ]]; then + LONGDES="Failure to start the Backup job ${DATETIME} CUST=${CUSTNAME} ${RC}" + # -- Send Failure notification to a slack channel + cat << ! >.curl_${DBNAME}_RUN.sh + curl -X POST -H 'Content-type: application/json' --data '{"text":"$LONGDES"}' ${SLACKURL} ! -/bin/bash .curl_${DBNAME}_RUN.sh > .curl_${DBNAME}_RUN.out 2>&1 - - ##### Create ICD Incident #### - ####### If Backup fails ### - des="${DBINSTANCE} - Backup - ${HOSTNAME} ${DBNAME} ${CUSTNAME} - MASMS -- Backup Failed" - echo "TESTING $instance - Backup - $ ${DBNAME} - Backup Failed" > .Maillive.log - echo "############################" >> .Maillive.log - #cat $BACK_LOG >> .Maillive.log - longdes=`cat .Maillive.log | sed 's/"//g' | sed "s/'//g"` - ICD_URL="https://servicedesk.mro.com" - if ! curl -k -s --connect-timeout 3 ${ICD_URL} >/dev/null; then - ICD_URL="https://servicedesk.cds.mro.com" - fi + /bin/bash .curl_${DBNAME}_RUN.sh > .curl_${DBNAME}_RUN.out 2>&1 -cat << ! >.curl_${DBNAME}_ICD.sh - curl --insecure --location --request POST "${ICD_URL}/maximo_mif/oslc/os/hsincident?lean=1" \ - --header "Authorization: Basic ${ICD_AUTH_KEY}" \ - --header 'Content-Type: application/json' \ - --data '{ - "description":"$des", - "reportedpriority":4, - "internalpriority":4, - "reportedby":"DB2", - "affectedperson":"CTGINST1", - "description_longdescription":"$longdes", - "siteid":"001", - "classstructureid":"1341", - "classificationid":"IN-DBPERF", - "hshost":"{servicedesk-pdb-sjc03-2.cds.mro.com:0:50}", - "hstype":"BACKUP" - }' -! -if [[ -n "${ICD_AUTH_KEY}" ]]; then - /bin/bash .curl_${DBNAME}_ICD.sh > .curl_${DBNAME}_ICD.out 2>&1 -fi + # -- Create ICD ticket if fails + DES="${CUSTNAME} - ${DBENV} - ${DBNAME} - ${HOSTNAME} -- Failed to Initiate Backup!! " + CREATE_ICD "${DES}" + fi + + # -- Execute Online Reorgs for qualified tables and indexes after every Full Backup + if [[ ${DOW} = ${DAYOFFULL} ]] ; then + /bin/bash ${SCRIPT_DIR}/reorgTablesIndexesInplace.sh -db ${DBNAME} -s MAXIMO -tb_stats -ix_stats -window 120 -tr > ${HOME}/maintenance/logs/reorgTablesIndexesInplace_${DATETIME}.log 2>&1 + fi -fi done -/bin/bash ${HOME}/bin/runstats_rebind.sh >${HOME}/bin/.runstats_rebind.out 2>&1 -/bin/bash ${HOME}/bin/grant_check.sh bludb >${HOME}/bin/.grant_check.out 2>&1 + +# -- Exeucte Runstats and Rebind for all tables on weekly after full backup +/bin/bash ${SCRIPT_DIR}/runstats_rebind.sh >${SCRIPT_DIR}/.runstats_rebind.out 2>&1 +#/bin/bash ${SCRIPT_DIR}/grant_check.sh bludb >${SCRIPT_DIR}/.grant_check.out 2>&1 + +# -- END OF SCRIPT \ No newline at end of file diff --git a/instance-applications/120-ibm-db2u-database/files/reorgTablesIndexesInplace2.sh b/instance-applications/120-ibm-db2u-database/files/reorgTablesIndexesInplace.sh similarity index 100% rename from instance-applications/120-ibm-db2u-database/files/reorgTablesIndexesInplace2.sh rename to instance-applications/120-ibm-db2u-database/files/reorgTablesIndexesInplace.sh diff --git a/instance-applications/120-ibm-db2u-database/files/runstats_rebind.sh b/instance-applications/120-ibm-db2u-database/files/runstats_rebind.sh index 7415d6b4a..a2159405d 100755 --- a/instance-applications/120-ibm-db2u-database/files/runstats_rebind.sh +++ b/instance-applications/120-ibm-db2u-database/files/runstats_rebind.sh @@ -1,41 +1,47 @@ #!/bin/bash # *************************************************************************** -# Author: Fu Le Qing (Roking) -# Email: leqingfu@cn.ibm.com -# Date: 10-31-2018 +# Author: Fu Le Qing (Roking) +# Email: leqingfu@cn.ibm.com +# Date: 10-31-2018 # -# Description: This script updates statistics of tables, -# associated indexes in the database, and sends an email -# to a specified email list. +# Description: This script updates statistics of tables, +# associated indexes in the database, and sends an email +# to a specified email list. # # ******** THIS NEEDS TO BE RUN AS INSTANCE OWNER. ************** # -# Revision history: -# 10-31-2018 Fu Le Qing (Roking) -# Original version -# 11-16-2018 Fu Le Qing (Roking) -# Skip the tables which are ongoing with reorg -# 09-08-2023 Fu Le Qing (Roking) -# Update for MAS +# Revision history: +# 10-31-2018 Fu Le Qing (Roking) +# Original version +# 11-16-2018 Fu Le Qing (Roking) +# Skip the tables which are ongoing with reorg +# 09-08-2023 Fu Le Qing (Roking) +# Update for MAS # # *************************************************************************** # # *************************************************************************** + +# -- Source the Props file if [ -f /mnt/backup/bin/.PROPS ] then . /mnt/backup/bin/.PROPS - DOW=`date | awk '{print $1}'` + DOW=`date | awk '{print $1}'` if [ ${DOW} != ${DAYOFFULL} ] then + echo "Runstats runs only on Day of Full backup. . . !!! Exiting !" exit 0 fi fi -instance=`whoami` -instance_home=`/usr/local/bin/db2greg -dump | grep -ae "I," | grep -v "/das," | grep "${instance}" | awk -F ',' '{print $5}'| sed 's/\/sqllib//'` +# -- Standard Parameters +INSTANCE=`whoami` +INSTANCE_HOME=`/usr/local/bin/db2greg -dump | grep -ae "I," | grep -v "/das," | grep "${INSTANCE}" | awk -F ',' '{print $5}'| sed 's/\/sqllib//'` +mkdir -p ${INSTANCE_HOME}/maintenance/logs +DATESTAMP=`date "+%Y-%m-%d-%H.%M.%S"` -pidfile="$instance_home/.`basename ${0}`.pid" -if [ -e ${pidfile} ] && $kill -0 `cat ${pidfile}` 2>/dev/null +pidfile="${INSTANCE_HOME}/.`basename ${0}`.pid" +if [ -e ${pidfile} ] && $kill -0 `cat ${pidfile}` 2>/dev/null then exit 0 fi @@ -43,48 +49,52 @@ fi echo $$ > ${pidfile} trap "rm -f ${pidfile}; exit" SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM EXIT -if [ ! -f "$instance_home/sqllib/db2profile" ] +if [ ! -f "${INSTANCE_HOME}/sqllib/db2profile" ] then - echo "ERROR - $instance_home/sqllib/db2profile not found" + echo "ERROR - ${INSTANCE_HOME}/sqllib/db2profile not found" exit 1 else - . $instance_home/sqllib/db2profile + . ${INSTANCE_HOME}/sqllib/db2profile fi -RUNSTATS_TMP_FILE="$instance_home/.runstats.sql" -REBIND_TMP_FILE="$instance_home/.rebind.sql" +# -- Debug Mode +# set -x; # Uncomment to debug this shell script +# set -n; # Uncomment to check your syntax, without execution. -mkdir -p $instance_home/maintenance/logs -DATESTAMP=`date "+%Y-%m-%d-%H.%M.%S"` +RUNSTATS_TMP_FILE="${INSTANCE_HOME}/bin/.runstats.sql" +REBIND_TMP_FILE="${INSTANCE_HOME}/bin/.rebind.sql" -for db in `db2 list db directory | grep -B 5 Indirect | grep "Database name" | cut -d= -f2` +for DB in `db2 list db directory | grep -B 5 Indirect | grep "Database name" | cut -d= -f2` do - role=`db2 get db cfg for ${db} | grep "HADR database role" | cut -d= -f2 |sed 's/ //g'` + RUNSTATS_REBIND_LOG="${INSTANCE_HOME}/maintenance/logs/runstats_rebind_${DB}_${DATESTAMP}.log" + + role=`db2 get db cfg for ${DB} | grep "HADR database role" | cut -d= -f2 |sed 's/ //g'` if [ "$role" != "STANDBY" ]; then - if [ -f $RUNSTATS_TMP_FILE ] - then - rm $RUNSTATS_TMP_FILE + if [[ -f ${RUNSTATS_TMP_FILE} ]]; then + rm ${RUNSTATS_TMP_FILE} fi - if [ -f $REBIND_TMP_FILE ] - then - rm $REBIND_TMP_FILE + if [[ -f ${REBIND_TMP_FILE} ]]; then + rm ${REBIND_TMP_FILE} fi - db2 connect to ${db} | tee $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP} - if [ $? -eq 0 ]; then - db2 -x "select 'RUNSTATS ON TABLE \"' ||rtrim(tab.tabschema)||'\".\"'|| tab.tabname ||'\" WITH DISTRIBUTION ON KEY COLUMNS AND DETAILED INDEXES ALL ALLOW WRITE ACCESS;' - from syscat.tables tab left join sysibmadm.SNAPTAB_REORG reg on tab.tabschema=reg.TABSCHEMA and tab.tabname=reg.TABNAME and reg.REORG_STATUS not in ('COMPLETED','STOPPED') - where tab.type='T' and reg.tabname is null" > $RUNSTATS_TMP_FILE - #db2 -x "select 'rebind package \"' ||rtrim(PKGSCHEMA)||'\".\"'|| PKGNAME ||'\";' from syscat.packages where PKGSCHEMA not in ('NULLID','NULLIDR1','NULLIDRA','SYSIBMADM','SYSIBMINTERNAL') and PKGSCHEMA not like 'NULL%' " > $REBIND_TMP_FILE - echo "Begin processing of runstats @ $DATESTAMP ..." | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP} - db2 -txvf $RUNSTATS_TMP_FILE | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP} - echo "End processing of runstats @ $DATESTAMP" | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP} - #echo "Begin processing of rebind @ $DATESTAMP ..." | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP} - #db2 -txvf $REBIND_TMP_FILE | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP} - #echo "End processing of rebind @ $DATESTAMP" | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP} - rm $RUNSTATS_TMP_FILE - #rm $REBIND_TMP_FILE + db2 connect to ${DB} | tee ${RUNSTATS_REBIND_LOG} + if [[ $? -eq 0 ]]; then + db2 -x "select 'RUNSTATS ON TABLE \"' ||rtrim(tab.tabschema)||'\".\"'|| tab.tabname ||'\" ON ALL COLUMNS WITH DISTRIBUTION ON ALL COLUMNS AND DETAILED INDEXES ALL ALLOW WRITE ACCESS;' from syscat.tables tab left join sysibmadm.SNAPTAB_REORG reg on tab.tabschema=reg.TABSCHEMA and tab.tabname=reg.TABNAME and reg.REORG_STATUS not in ('COMPLETED','STOPPED') where tab.type='T' and reg.tabname is null" > ${RUNSTATS_TMP_FILE} + + echo -e "Begin processing of runstats @ ${DATESTAMP} ...\n" | tee -a ${RUNSTATS_REBIND_LOG} + db2 -txvf ${RUNSTATS_TMP_FILE} | tee -a ${RUNSTATS_REBIND_LOG} + echo -e "\nEnd processing of runstats @ ${DATESTAMP}" | tee -a ${RUNSTATS_REBIND_LOG} + rm ${RUNSTATS_TMP_FILE} + + db2 -x "select 'rebind package \"' ||rtrim(PKGSCHEMA)||'\".\"'|| PKGNAME ||'\";' from syscat.packages where PKGSCHEMA not like 'SYSIBM%' and PKGSCHEMA not like 'NULL%' " > ${REBIND_TMP_FILE} + + echo -e "\n ----------------------------------------------- " | tee -a ${RUNSTATS_REBIND_LOG} + echo -e "Begin processing of rebind @ ${DATESTAMP} ...\n" | tee -a ${RUNSTATS_REBIND_LOG} + db2 -txvf ${REBIND_TMP_FILE} | tee -a ${RUNSTATS_REBIND_LOG} + echo -e "\nEnd processing of rebind @ ${DATESTAMP}" | tee -a ${RUNSTATS_REBIND_LOG} + + rm ${REBIND_TMP_FILE} db2 terminate - fi + fi fi done