#!/bin/ksh

# Author: UNIX Health Check
# Purpose: To sync important files between cluster nodes.
# Date: February 9, 2010

# Test if this is an HACMP cluster. If not, just exit.
if [ ! -x /usr/es/sbin/cluster/clstrmgr ] ; then
	exit
fi

# what is the service address?
SERVICEADDR=`/usr/es/sbin/cluster/utilities/cllsif -c | grep service | grep ether | sort -dfu | cut -f1 -d: | sort -dfu`

if [ -z "${SERVICEADDR}" ] ; then
	# No service address found
	exit
fi

# is this service address configured on this host?
SERVICEIP=`/usr/bin/netstat -i | grep ${SERVICEADDR} | wc -l | awk '{print $1}'`

# what is the current host?
CURRENTHOST=`/usr/es/sbin/cluster/utilities/get_local_nodename`

# what is the other node?
OTHERSERVER=`/usr/es/sbin/cluster/utilities/cllsnode -c | grep -v "^#node" | grep -v "${CURRENTHOST}:" | cut -f1 -d:`

# is there a service address configured on this server?
if [ ${SERVICEIP} -eq 0 ] ; then
	# Service IP is not configured on this server. No need to sync.
	exit
fi

echo "Copying from ${CURRENTHOST} to ${OTHERSERVER}"
scp -p /etc/passwd ${OTHERSERVER}:/etc/passwd
scp -p /etc/security/passwd ${OTHERSERVER}:/etc/security/passwd
scp -p /etc/security/user ${OTHERSERVER}:/etc/security/user
scp -p /etc/security/group ${OTHERSERVER}:/etc/security/group
scp -p /etc/security/limits ${OTHERSERVER}:/etc/security/limits
scp -p /etc/group ${OTHERSERVER}:/etc/group
scp -p /etc/sudoers ${OTHERSERVER}:/etc/sudoers
scp -p /etc/mail/sendmail.cf ${OTHERSERVER}:/etc/mail/sendmail.cf
scp -p /etc/exclude.rootvg ${OTHERSERVER}:/etc/exclude.rootvg

echo "Correct root GECOS field in /etc/passwd on ${OTHERSERVER}"
chuser gecos="${CURRENTHOST} root user" root
ssh ${OTHERSERVER} "chuser gecos='${OTHERSERVER} root user' root"

# copy the crontabs and make sure they get activated on the standby node
# make a temporary directory for storing the crontab files
echo "Synchronizing crontabs"
ssh ${OTHERSERVER} mkdir -p /tmp/cron.$$
# copy over the crontab files to the temporary directory
cd /var/spool/cron/crontabs
tar -cvf - . | ssh ${OTHERSERVER} "cd /tmp/cron.$$; umask 000 ; cat | tar -xvpf -"
# make sure every user can access this temporary crontab directory
ssh ${OTHERSERVER} chmod -R 777 /tmp/cron.$$/
# enable the crontab for every user on the ${OTHERSERVER}
for file in `ls /var/spool/cron/crontabs/*` ; do
	# only update the file if there's a difference found
	tab=`basename $file`
	myfile=`/usr/bin/cksum /var/spool/cron/crontabs/${tab}`
	yourfile=`ssh ${OTHERSERVER} /usr/bin/cksum /var/spool/cron/crontabs/${tab}`
	if [ "${myfile}" != "${yourfile}" ] ; then
		ssh ${OTHERSERVER} su - $tab -c crontab /tmp/cron.$$/$tab > /dev/null 2>/dev/null
		echo "Enabled crontab for user ${tab}."
	else
		echo "No changes for the crontab of user $tab found. Skipping."
	fi
done
# delete our temporary directory
ssh ${OTHERSERVER} rm -rf /tmp/cron.$$
# now make sure the standby node doesn't have any crontabs that the active node doesn't have
ssh ${OTHERSERVER} ls /var/spool/cron/crontabs | sed "s/      //g" | while read file ; do
	unset result
	result=`ls -als /var/spool/cron/crontabs/${file} 2>/dev/null`
	if [ -z "${result}" ] ; then
		# found a file on the standby that doesn't exist on the primary. delete this file
		# run crontab -r. this will only work on crontab files of actual users. 
		# remove the file afterwards, just in case the user didn't exist anymore.
		ssh ${OTHERSERVER} "crontab -r ${file} 2>/dev/null;rm -f /var/spool/cron/crontabs/${file}"
		echo "Crontab of user $file on ${OTHERSERVER} deleted."
	fi
done
