#!/bin/bash
#
# cgrecover
# --------
# utility for recover a tableset from an online backup
#                                        
# Design and Implementation by Bjoern Lemke               
#     
# (C)opyright 2009 Bjoern Lemke

if [ -z $1 ] || [ -z $2 ]
then
    echo "Usage : mkrecover <tableset> <recoverfile>"
    exit 1
fi

# please customize the following parameters
TS=$1
BUFILE=$2
CGADM=cgadm
USER=cgadm/cgadm
HOST=localhost
PORT=2000

### end of customizing ####

RUNSTATE=`$CGADM --server=$HOST --port=$PORT --user=$USER --cmd="show tableset ${TS}" --raw | grep RunState | awk '{ print $2 }'`

echo "Runstate is $RUNSTATE"

if [ "$RUNSTATE" = "ONLINE" ]
then

    echo "Stopping tableset $TS .."
    
    $CGADM --server=$HOST --port=$PORT --user=$USER --cmd="stop tableset ${TS};"
    if [ $? -ne 0 ]
    then
	echo "Cannot stop tableset $TS"
	exit 1
    fi
fi

echo "Restoring datafiles .."
tar -xvf $BUFILE

$CGADM --server=$HOST --port=$PORT --user=$USER --cmd="recover tableset ${TS};"

if [ $? -ne 0 ]
then
    echo "Cannot recover tableset $TS"
    exit 1
fi

echo "Recovery completed."
exit 0
