#
# make_backup
# (c) 2007-2009 Fabian Herschel
#
# License GPL
#
# ## Changes ##
#
# 0.3 2009-04-03 - also backup LSB resources (/etc/init.d)
#

function archiveit() # cmd archive-name tar-options
{
   # changes external vars: host_archive_list
        cmd=$1
        archive_name=$2
	if [ $verbose -ge 1 ]; then
		printf "%s " "$archive_name"
	else 
		printf "."
	fi
	if [ "$cmd" != "" -a $verbose -le 1 ]; then
                cmd="$cmd 1>/dev/null 2>/dev/null"
        fi
        long_archive_name="${h}.${ID}.${archive_name}.tgz"
        host_archive_list="$host_archive_list $long_archive_name"
        tar_options="$3"
        remote_dir=/root/suse/tmptgz
	if [ "$h" = "$(hostname)" ];
	then
		if [ -z "$cmd" ]; then
		    cd $remote_dir; tar -C / -czf ${long_archive_name} $tar_options
		else
		    cd $remote_dir; bash -c "$cmd"; tar -C / -czf ${long_archive_name} $tar_options
		fi
	else
		if [ -z "$cmd" ]; then
		    ssh root@$h "cd $remote_dir; tar -C / -czf ${long_archive_name} $tar_options"
		else
		    ssh root@$h "cd $remote_dir; $cmd; tar -C / -czf ${long_archive_name} $tar_options"
		fi
	fi
	if [ $verbose -gt 0 ]; then
		printf "DONE\n" 
	fi
}

verbose=0
hosts=$(hostname)

while [ $# -gt 0 ]; do
    case "$1" in
        --hosts=*)
			hosts=${hosts#--hosts=}
			;;
	--hosts )
			hosts="$2"
			;;
        --autodetect )
			hosts=$(cl_status listnodes -n 2>/dev/null)
                        if [ -z "$hosts" ]; then
                           echo "autodetect failed (empty node list)".
                        fi
			;;
	--verbose | -v )
			(( verbose++ ))
			;;
        -?|-h|--help )
			echo "$0 "'[--hosts="hostlist"|--hosts "hostlist"|--autodetect|--help]'
			echo "hostlist: entries are separated by blanks"
			exit 0;
    esac
    shift
done

if [ -z "$hosts" ]; then
   echo "Please specify the hostlist. Try $0 --help to get more help."
   exit 1
fi

archive="/root/suse/archive/archive_cluster_$(echo $hosts|tr ' ' '-')"

ID0=$(date "+%Y%m%d")
count=1
ID="$ID0-$count"



while [ -f ${archive}.${ID}.tgz ]
do
	(( count++ ))
	ID="$ID0-$count"
done


echo Backup ID is "$ID"
mkdir -p /root/suse/archive
mkdir -p /root/suse/tmptgz

complete_archive_list=""
for h in ${hosts}
do
	echo "Backup heartbeat config $h..."
        host_archive_list=""
	if [ "$h" = "$(hostname)" ];
	then
		mkdir -p /root/suse/tmptgz
	else
		ssh root@$h 'mkdir -p /root/suse/tmptgz'
	fi
        archiveit ""	etc_ha.d				"etc/ha.d"
        archiveit ""	clusterconf				"clusterconf"
	archiveit ""	usr_lib64_stonith_plugins_external 	"usr/lib64/stonith/plugins/external"
	archiveit ""	usr_lib_ocf_resource.d_heartbeat 	"usr/lib/ocf/resource.d/heartbeat"
	archiveit ""	var_lib_heartbeat 			"--exclude core var/lib/heartbeat"	
	archiveit ""	root_suse				'--exclude=archive --exclude=tmptgz root/suse'
	archiveit ""	etc_init.d				'--exclude="rc*.d" --exclude="boot.d" etc/init.d'
	archiveit ""	var_log_messages			"var/log/messages*"
#	archiveit "siga write" siga 	"tmp/siga"
	archiveit "supportconfig" supportconfig "/var/log/nts_${h}_$(date "+%y%m%d")_*.tbz"


	if [ "$h" != "$(hostname)" ]; then
		rsync -a root@$h:/root/suse/tmptgz/* /root/suse/tmptgz 
	fi
	echo " (DONE)"
        complete_archive_list="$complete_archive_list $host_archive_list"
done

echo "archive list: $complete_archive_list"
tar -C /root/suse/tmptgz -czf ${archive}.${ID}.tgz .
for h in ${hosts}
do
	echo "Cleanup tmp archives on $h"
	if [ "$h" = "$(hostname)" ]; then
		ls -lh /root/suse/tmptgz/*.tgz
		rm     /root/suse/tmptgz/*.tgz
	else
		ssh root@$h 'ls -lh "/root/suse/tmptgz/"*".tgz"'
		ssh root@$h 'rm     "/root/suse/tmptgz/"*".tgz"'
	fi
done
