#!/bin/bash
#
###########################################################################
##  DPEXPIMP - Datapump export/import
##
## This script will export all BI related schemas schema
## In addition this script will make a zip archive with the catalog folder and cwallet.sso
##
##
## ABS Boxtel BV, The Netherlands 
## July 2023/CVI 
##  
## 1.0	- 31July2023	- Initial version
##
##  CVI/ABS Laundry Business Solutions
###########################################################################

# Setting variables
export ORACLE_BI_HOME=/u01/app/obiee			    # Location of OBI 	
export BACKUPLOCATION=/u02/oracle/dumpfiles			# Export location for the script
export SYSPASS=<pleaseuseBIOASSYSpassword>			# BI SYS password
#export MNTLOCATION=/mnt/bkp/BI                     # Added mount location for moving file after completion

export PW_DEV_MDS=<pleaseuseDEV_MDS_PASSWORD>				                # Password of DEV_MDS schema
export PW_DEV_BIPLATFORM=<pleaseuseDEV_BIPLATFORM_PASSWORD>				            # Password of DEV_BIPLATFORM schema
export PW_ABSDATAWAREHOUSE_V10=ABSDATAWAREHOUSE_V10		# Password of ABSDATAWAREHOUSE schema	

# Create export of RPD
$ORACLE_BI_HOME/user_projects/domains/bi/bitools/bin/datamodel.sh downloadrpd -O $BACKUPLOCATION/backup.rpd -SI ssi -U SYS -P $SYSPASS -W abssys01 -Y

# Make LDIF file weblogic users
export SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
export WLSTLOCATION=$ORACLE_BI_HOME/oracle_common/common/bin/wlst.sh
echo "connect('SYS','<pleaseuseBIOASSYSpassword>', 't3://localhost:9500');" > $SCRIPTDIR/createLDIF.py
echo "domainRuntime();" >> $SCRIPTDIR/createLDIF.py
echo "cd('/DomainServices/DomainRuntimeService/DomainConfiguration/bi/SecurityConfiguration/bi/DefaultRealm/myrealm/AuthenticationProviders/DefaultAuthenticator');" >> $SCRIPTDIR/createLDIF.py
echo "cmo.exportData('DefaultAtn','$BACKUPLOCATION/export.ldif', Properties());" >> $SCRIPTDIR/createLDIF.py
echo "exit();" >> $SCRIPTDIR/createLDIF.py
$ORACLE_BI_HOME/oracle_common/common/bin/wlst.sh $SCRIPTDIR/createLDIF.py
rm $SCRIPTDIR/createLDIF.py

# Make BAR file
export BARDIR=$BACKUPLOCATION/bardir
mkdir $BARDIR
if [ -f $ORACLE_BI_HOME/user_projects/domains/bi/bitools/bin/exportarchive.sh ]
then
	sh $ORACLE_BI_HOME/user_projects/domains/bi/bitools/bin/exportarchive.sh ssi $BARDIR encryptionpassword=abssys01
else
	echo "exportServiceInstance ('$ORACLE_BI_HOME/user_projects/domains/bi','ssi','$BARDIR/workdir','$BARDIR/exportdir','','','',true,'ABSDATAWAREHOUSE_V10')" > $SCRIPTDIR/createBAR.py
	$ORACLE_BI_HOME/oracle_common/common/bin/wlst.sh $SCRIPTDIR/createBAR.py
	rm $SCRIPTDIR/createBAR.py
fi

# Make exports of the BI schema's
# $SCRIPTDIR/dpexpimp.sh -ex -fu:ABSDATAWAREHOUSE_V10 -fp:$PW_ABSDATAWAREHOUSE_V10 -fn:ABSDATAWAREHOUSE_V10_$(date '+%A') -fd:$BACKUPLOCATION -ed:$BACKUPLOCATION
# $SCRIPTDIR/dpexpimp.sh -ex -fu:DEV_MDS -fp:$PW_DEV_MDS -fn:DEV_MDS_$(date '+%A') -fd:$BACKUPLOCATION -ed:$BACKUPLOCATION
# $SCRIPTDIR/dpexpimp.sh -ex -fu:DEV_BIPLATFORM -fp:$PW_DEV_BIPLATFORM -fn:DEV_BIPLATFORM_$(date '+%A') -fd:$BACKUPLOCATION -ed:$BACKUPLOCATION

# Create zip file
export ZIPNAME=$BACKUPLOCATION/BI_$(date '+%A').zip
rm -f $ZIPNAME
cp $ORACLE_BI_HOME/user_projects/domains/bi/config/fmwconfig/bootstrap/cwallet.sso $BACKUPLOCATION
cd $BACKUPLOCATION
zip -r $ZIPNAME backup.rpd bardir cwallet.sso export.ldif *_$(date '+%A').dmp.gz *_$(date '+%A')_exp.log
rm -r backup.rpd bardir cwallet.sso export.ldif *_$(date '+%A').dmp.gz *_$(date '+%A')_exp.log

# Copy zip to OCI Bucket
# oci os object bulk-upload -ns axrnmo3hd7kn -bn Dumpfiles_FEL_BI --overwrite --src-dir $BACKUPLOCATION/

# Move zip to mount location
#[ -d $MNTLOCATION ] && mv -f $ZIPNAME $MNTLOCATION && touch $BACKUPLOCATION/mnt_ok

# Create mnt_ok after task completion
touch $BACKUPLOCATION/mnt_ok