#!/usr/bin/bash
#export from hana test
remoteHana=$1
instId=$2
SCHEMA_LIST=`hdbsql -i $instId -u system -p Wang_2017 -n $remoteHana "\ds"`
schemaRegx="^\"(\w+)\",\"(\w+)\""
#migDir="/hana/mounts/SCHEMA_MIG"
migDir=$3

dateStr=`date '+%F'`
echo "Date: $dateStr"
mkdir $migDir/$dateStr
chmod -R a+w $migDir/$dateStr

for el in $SCHEMA_LIST
do
  
  if [[ $el =~ $schemaRegx ]]; then
          echo $el
          schema=${BASH_REMATCH[2]}
          echo $schema
          if [[ $schema =~ "SYS" || $schema =~ "SYSTEM" ||$schema =~ "EPM_TMP" || $schema =~ "_AUTO_USER_" ]]; then
                echo "skip"
          else
                echo "exporting $schema"
                hdbsql -i $instId -u $schema -p EPM_user_2017 -n $remoteHana "EXPORT $schema.\"*\" AS BINARY INTO '$migDir/$dateStr/$schema' WITH REPLACE"
                if [ $? != "0" ]; then
              echo "exporting fails"
            else
              echo "exporting success"
            fi
          fi

  fi  
done

echo "export end"