#!/bin/bash

PATH=/usr/sbin:/usr/local/bin:/usr/bin:/bin:/sbin
date=`date +%w`

mysql_user=`/usr/local/mssBak/script/encryption 1 "627273706e69687c727979"`
mysql_pw=`/usr/local/mssBak/script/encryption 1 "6f716c7866697074795a4c2f434639"`

#Get the msc-0 ip
msc1_host=`/usr/bin/mysql -u$mysql_user -p$mysql_pw <<_EOF_
SELECT ip FROM OMC_PUB.sysInfo WHERE sysTypeNo=320 and sysNo=0;
_EOF_`

#Get the msc-1 ip
msc2_host=`/usr/bin/mysql -u$mysql_user -p$mysql_pw <<_EOF_
SELECT ip FROM OMC_PUB.sysInfo WHERE sysTypeNo=320 and sysNo=1;
_EOF_`

msc1_hostip=`echo $msc1_host | awk '{print $2}'`
msc2_hostip=`echo $msc2_host | awk '{print $2}'`

#echo msc1_hostip=$msc1_hostip
#echo $msc2_hostip

if [ $msc1_hostip != " " ] ; then
	hostip=$msc1_hostip
else
	if [ $msc2_hostip !=" " ] ; then
		hostip=$msc2_hostip
	else
		exit
	fi
fi

ftpUserNameStr=`/usr/bin/mysql -u$mysql_user -p$mysql_pw <<_EOF_
SELECT pubVarValue FROM OMC_PUB.omcPubVarConf WHERE pubVarName='globalFTPUserName';
_EOF_`

ftpPasswordStr=`/usr/bin/mysql -u$mysql_user -p$mysql_pw <<_EOF_
SELECT pubVarValue FROM OMC_PUB.omcPubVarConf WHERE pubVarName='globalFTPPassword';
_EOF_`

ftpUserName=`echo $ftpUserNameStr | awk '{print $2}'`
ftpPassword=`echo $ftpPasswordStr | awk '{print $2}'`

#tmpDir=/usr/local/apache/htdocs/db_backup/other
tmpDir=/tmp/

#echo ftpUserName=$ftpUserName ftpPassword=$ftpPassword

#ftp -i -n $hostip  <<haha
#user $ftpUserName $ftpPassword
#binary
#lcd $tmpDir
#cd /usr/local/backup
#mget *.dat
#bye
#haha
file_list="HlrSubsBk.dat AucSubsBk.dat EirMesBk.dat ppsSubsBak.dat ppsRechargeCardBak.dat ppsTariffBak.dat OprSubsBk.dat npdbMemBak.dat palimSubsBk.dat"
for file in $file_list; do
	sed -e "/$hostip/d" ~/.ssh/known_hosts > /tmp/known_hosts
	mv -f /tmp/known_hosts ~/.ssh/known_hosts
	chmod 644 ~/.ssh/known_hosts
	expect /usr/local/mssBak/script/getfile $hostip $ftpUserName $ftpPassword $tmpDir $file /usr/local/backup/
done

homeDir=/usr/local/apache/htdocs

file_mode=644

mv -f $tmpDir/HlrSubsBk.dat $homeDir/mssBakData/hlr/HlrSubBk_$date.dat
chmod $file_mode $homeDir/mssBakData/hlr/HlrSubBk_$date.dat
cd $homeDir/mssBakData/hlr/
tar czvf HlrSubBk_$date.tgz HlrSubBk_$date.dat
mv -f $tmpDir/AucSubsBk.dat $homeDir/mssBakData/auc/AucSubsBk_$date.dat
chmod $file_mode $homeDir/mssBakData/auc/AucSubsBk_$date.dat
cd $homeDir/mssBakData/auc/
tar czvf AucSubsBk_$date.tgz AucSubsBk_$date.dat
mv -f $tmpDir/EirMesBk.dat $homeDir/mssBakData/eir/EirMesBk_$date.dat
chmod $file_mode $homeDir/mssBakData/eir/EirMesBk_$date.dat
cd $homeDir/mssBakData/eir/
tar czvf EirMesBk_$date.tgz EirMesBk_$date.dat
mv -f $tmpDir/ppsSubsBak.dat $homeDir/mssBakData/pps/ppsSubsBak_$date.dat
chmod $file_mode $homeDir/mssBakData/pps/ppsSubsBak_$date.dat
cd $homeDir/mssBakData/pps/
tar czvf ppsSubsBak_$date.tgz ppsSubsBak_$date.dat
mv -f $tmpDir/ppsRechargeCardBak.dat $homeDir/mssBakData/pps/ppsRechargeCardBak_$date.dat
chmod $file_mode $homeDir/mssBakData/pps/ppsRechargeCardBak_$date.dat
cd $homeDir/mssBakData/pps/
tar czvf ppsRechargeCardBak_$date.tgz ppsRechargeCardBak_$date.dat
mv -f $tmpDir/ppsTariffBak.dat $homeDir/mssBakData/pps/ppsTariffBak_$date.dat
chmod $file_mode $homeDir/mssBakData/pps/ppsTariffBak_$date.dat
cd $homeDir/mssBakData/pps/
tar czvf ppsTariffBak_$date.tgz ppsTariffBak_$date.dat
mv -f $tmpDir/OprSubsBk.dat $homeDir/mssBakData/opr/OprSubsBk_$date.dat
chmod $file_mode $homeDir/mssBakData/opr/OprSubsBk_$date.dat
cd $homeDir/mssBakData/opr/
tar czvf OprSubsBk_$date.tgz OprSubsBk_$date.dat
mv -f $tmpDir/npdbMemBak.dat $homeDir/mssBakData/mnp/npdbMemBak_$date.dat
chmod $file_mode $homeDir/mssBakData/mnp/npdbMemBak_$date.dat
cd $homeDir/mssBakData/mnp/
tar czvf npdbMemBak_$date.tgz npdbMemBak_$date.dat
mv -f $tmpDir/palimSubsBk.dat $homeDir/mssBakData/palim/palimSubsBk_$date.dat
chmod $file_mode $homeDir/mssBakData/palim/palimSubsBk_$date.dat
cd $homeDir/mssBakData/palim/
tar czvf palimSubsBk_$date.tgz palimSubsBk_$date.dat	

rm -rf $tmpDir/*.dat

#Get rcm dat file
#Get the rcm-0 ip
rcm1_host=`/usr/bin/mysql -u$mysql_user -p$mysql_pw <<_EOF_
SELECT ip FROM OMC_PUB.sysInfo WHERE sysTypeNo=395 and sysNo=0;
_EOF_`

#Get the rcm-1 ip
rcm2_host=`/usr/bin/mysql -u$mysql_user -p$mysql_pw <<_EOF_
SELECT ip FROM OMC_PUB.sysInfo WHERE sysTypeNo=395 and sysNo=1;
_EOF_`

rcm1_hostip=`echo $rcm1_host | awk '{print $2}'`
rcm2_hostip=`echo $rcm2_host | awk '{print $2}'`

#echo msc1_hostip=$msc1_hostip
#echo $msc2_hostip

if [ $rcm1_hostip != " " ] ; then
        hostip=$rcm1_hostip
else
        if [ $rcm2_hostip !=" " ] ; then
                hostip=$rcm2_hostip
        else
                exit
        fi
fi



file_list="ppsRechargeCardBak.dat"
for file in $file_list; do
        sed -e "/$hostip/d" ~/.ssh/known_hosts > /tmp/known_hosts
        mv -f /tmp/known_hosts ~/.ssh/known_hosts
        chmod 644 ~/.ssh/known_hosts
        expect /usr/local/mssBak/script/getfile $hostip $ftpUserName $ftpPassword $tmpDir $file /usr/local/backup/
done

mv -f $tmpDir/ppsRechargeCardBak.dat $homeDir/mssBakData/rcm/ppsRechargeCardBak_$date.dat
chmod $file_mode $homeDir/mssBakData/rcm/ppsRechargeCardBak_$date.dat
cd $homeDir/mssBakData/rcm/
tar czvf ppsRechargeCardBak_$date.tgz ppsRechargeCardBak_$date.dat


rm -rf $tmpDir/*.dat
