#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2004,2007 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#*===========================================================================*/
#*                                                                           */
#* Module Name:  ctbackup                                                    */
#*                                                                           */
#* Description:                                                              */
#*      Script to copy RSCT data to a backup directory, maintaining          */
#*  consistency of related files, such that normal backup tools can then     */
#*  archive the data found in the backup directory.                          */
#*                                                                           */
#* Procedure:                                                                */
#*   1) Ensure backup directory /var/ct.backup exists and is empty           */
#*                                                                           */
#*   2) Copy cluster independent files to /var/ct.backup                     */
#*                                                                           */
#*   3) For each defined cluster, copy cluster dependent files               */
#*                                                                           */
#*   4) Validate consistency of registry tables under Version Update         */
#*      control. If copy is not consistent, copy again until consistency     */
#*      is validated.                                                        */
#*                                                                           */
#*===========================================================================*/
#*   @(#)54   1.5   src/rsct/utils/cmds/ctbackup.sh, common_utils, rsct_relgh, relghs001a 7/17/06 17:50:42

# include common definitions for backup/restore

. /usr/sbin/rsct/bin/ctbrdefs

#==================================================================
#==================================================================
#                     Main procedure
#==================================================================
#==================================================================

# Need to know which OS on which this script is running

OSname=$(uname -s)
if [[ -z $OSname ]]
then
    OSname=unknown_OS
fi

# Create an empty backup directory

rm -rf $CTBACKUPDIR
mkdir_and_set_ogp $CTBACKUPDIR $FUNC_EXIT

# Create cfg directory in backup

mkdir_and_set_ogp $BCFGDIR $FUNC_EXIT

#==================================================================
# copy files in "root" directory to root of backup dir
#==================================================================

for name in $VAR_CT_FILES
do
	copy_file_to_dir ${CTROOTDIR}/$name $CTBACKUPDIR $FUNC_EXIT
done

#==================================================================
# copy files in cluster independent cfg directory
#==================================================================

for name in $VAR_CT_CFG_FILES
do
	copy_file_to_dir ${CFGDIR}/$name $BCFGDIR $FUNC_EXIT
done


# copy install checkpoint files

for name in $(ls ${CFGDIR}/*.ckpt 2> /dev/null)
do
	copy_file_to_dir $name $BCFGDIR $FUNC_EXIT
done


#==================================================================
# copy files in cluster dependent directories
#==================================================================

# get list of cluster names/IDs and save it in backup directory

if ! lsclcfg -a -x > $CLUSTER_LIST
then
	# should at least be able to get IW 
	exit 1
fi

# do for each cluster

while read CLUSTER_NAME CLUSTER_ID
do
	# create the cluster dir in backup directory

	mkdir_and_set_ogp ${CTBACKUPDIR}/${CLUSTER_ID} $FUNC_EXIT

	# define cluster cfg dir names

	CFGDIR_C=${CTROOTDIR}/${CLUSTER_ID}/cfg
	BCFGDIR_C=${CTBACKUPDIR}/${CLUSTER_ID}/cfg

	# create cluster cfg dir in backup directory

	mkdir_and_set_ogp $BCFGDIR_C $FUNC_EXIT

	# copy the files in the cfg directory

	for name in $CLUSTER_CFG_FILES
	do
		copy_file_to_dir ${CFGDIR_C}/$name $BCFGDIR_C $FUNC_EXIT
	done

	# define cluster registry dir names

	REGDIR_C=${CTROOTDIR}/${CLUSTER_ID}/registry
	BREGDIR_C=${CTBACKUPDIR}/${CLUSTER_ID}/registry

	# create the cluster registry dir in backup directory

	mkdir_and_set_ogp $BREGDIR_C $FUNC_EXIT
	mkdir_and_set_ogp $BREGDIR_C/local_tree $FUNC_EXIT

	# copy the files in the registry directory
	#
	# N.B. this must be done before copying the VU files below !!!

	for name in $(ls ${REGDIR_C}/local_tree/* 2> /dev/null)
	do
		copy_file_to_dir $name ${BREGDIR_C}/local_tree $FUNC_EXIT
	done

	# copy the VU files

	for name in $(ls ${REGDIR_C}/*.vu 2> /dev/null)
	do
		copy_file_to_dir $name $BREGDIR_C $FUNC_EXIT
	done

	# copy miscellaneous directory trees in /var/ct/IW

	if [[ $CLUSTER_NAME = "IW" ]]
	then
		for dirpath in $VAR_CT_IW_MISC_DIRS
		do
			print "Copying directory tree ${CTROOTDIR}/${CLUSTER_ID}/$dirpath"
			if [[ -d ${CTROOTDIR}/${CLUSTER_ID}/$dirpath ]]
			then
				parent=${dirpath%/*}
				if [[ "$parent" != "$dirpath" ]]
				then
					if ! cp -rp ${CTROOTDIR}/${CLUSTER_ID}/$dirpath \
								${CTBACKUPDIR}/${CLUSTER_ID}/$parent
					then
						path_name=${CTROOTDIR}/${CLUSTER_ID}/$dirpath
						print "Cannot copy directory tree $path_name"
						exit 1
					fi
				fi
			fi
		done
	fi

done < $CLUSTER_LIST


#==================================================================
# check the integrity of the copied registry files that are under
# Version Update control
#==================================================================

# do for each cluster

while read CLUSTER_NAME CLUSTER_ID
do
	# define cluster registry dir names

	REGDIR_C=${CTROOTDIR}/${CLUSTER_ID}/registry
	BREGDIR_C=${CTBACKUPDIR}/${CLUSTER_ID}/registry

	# do for each copied VU file

	for vu_name in $(cd ${BREGDIR_C}; ls *.vu 2> /dev/null)
	do

		# set limit on retries

		(( i = 5 ))

		while (( i > 0 ))
		do
			print "checking consistency for ${BREGDIR_C}/${vu_name}"
			TABLE_LIST=$(ctchkvu $BREGDIR_C $vu_name)
			rc=$?
			if [[ $rc -eq 0 ]]
			then
				break		# tables/VU file make sense !!!
			fi

			if [[ $rc -ne 1 ]]
			then
				exit $rc	# unrecoverable error
			fi

			(( i = i - 1 ))
			if (( i == 0 ))
			then
				break		# retry limit exceeded
			fi

			# copy the files again, tables first, then VU

			sleep 2			# give the RM a chance to complete its update

			for tbl_name in $TABLE_LIST
			do
				copy_file_to_dir ${REGDIR_C}/local_tree/$tbl_name \
								 ${BREGDIR_C}/local_tree $FUNC_EXIT
			done

			copy_file_to_dir ${REGDIR_C}/$vu_name $BREGDIR_C $FUNC_EXIT
		done

		if (( i == 0 ))
		then
			print "WARNING: failed consistency check for ${BREGDIR_C}/${vu_name}"
		fi

	done

done < $CLUSTER_LIST


#==================================================================
# copy miscellaneous directory trees
#==================================================================

for dirpath in $VAR_CT_MISC_DIRS
do
	print "Copying directory tree ${CTROOTDIR}/$dirpath"
	if [[ -d ${CTROOTDIR}/$dirpath ]]
	then
		parent=${dirpath%/*}
		if [[ "$parent" != "$dirpath" ]]
		then
			if ! cp -rp ${CTROOTDIR}/$dirpath ${CTBACKUPDIR}/$parent
			then
				print "Cannot copy directory tree ${CTROOTDIR}/$dirpath"
				exit 1
			fi
		fi
	fi
done


#==================================================================
# save the versions of RSCT installed. ctrestore may need it
#==================================================================

if [[ $OSname = "AIX" ]]
then
	lslpp -Lcq 'rsct.core.*'              \
			   'rsct.crypt.*'             \
			   'rsct.basic.*'             \
			   'csm.client'               \
			   'csm.server'               \
			   'csm.core'  2> /dev/null | \
	awk -F':' '{print $2, $3}' > $CTBACKUPDIR/rsct.version
fi

if [[ $OSname = "Linux" ]]
then
	RPM1=rsct.core
	RPM2=rsct.crypt
	RPM3=rsct.basic
	RPM4=csm.server.hsc
	RPM5=csm_hmc.server
	RPM6=csm.core
	RPM7=csm.client
	RPM8=csm.server

	rpm -q -a | grep -E "$RPM1|$RPM2|$RPM3|$RPM4|$RPM5|$RPM6|$RPM7|$RPM8" | \
	awk -F'-' '{print $1, $2}' > $CTBACKUPDIR/rsct.version
fi

exit 0
