#!/bin/bash

##################################################################################
  function pn_month {
    typeset ym=$1 pn=$2
  
    (( m = ym % 100 ))
    (( y = ym / 100 ))
  
    while (( pn != 0 )); do
      if (( pn > 0 )); then
        if (( m == 12 ))
        then (( m = 1 )); (( y = y + 1 ))
        else (( m = m + 1 ))
        fi
        (( pn = pn - 1 ))
      else
        if (( m == 1 ))
        then (( m = 12 )); (( y = y - 1 ))
        else (( m = m - 1 ))
        fi
        (( pn = pn + 1 ))
      fi
    done

    printf "%s\n" $(( 100*y + m ))
  }

function end_month {
    typeset ym=$1 y m ld
    ((  y = ym  / 100 ))
    ((  m = ym  % 100 ))
    for ld in $(cal $m $y); do :; done
    printf "%s\n" $(( ym*100 + ld ))
  }

function pn_day() {
    typeset ymd=$1 pn=${2:-0} ym y m d x
  
    ((  d = ymd % 100 ))
    (( ym = ymd / 100 ))
    ((  y = ym  / 100 ))
    ((  m = ym  % 100 ))
  
    if (( pn < 0 )); then
      if (( d > 1 )); then
        (( x = ymd - 1 ))
        (( x > 17520902 && x < 17520914 )) && (( x = 17520902 ))
        pn_day $x $(( pn + 1 ))
      else
        pn_day $(end_month $(pn_month $ym -1)) $(( pn + 1 ))
      fi
    elif (( pn > 0 )); then
      if (( ymd < $(end_month $ym) )); then
        (( x = ymd + 1 ))
        (( x > 17520902 && x < 17520914 )) && (( x = 17520914 ))
        pn_day $x $(( pn - 1 ))
      else
        pn_day $(( 100*$(pn_month $ym +1) + 1 )) $(( pn - 1 ))
      fi
    else
      PN_DAY=$ymd
      return 0
    fi
  }
######################################################################################



DATUM=`date +%Y%m%d`


D1=_${DATUM}.dmp
D2=_${DATUM}.dmp.gz

##rm /home/dmp/${D1}


rm -f /home/dmp/*_${D1}

export ORACLE_SID=orc1

export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/12.2.0/dbhome_1
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export PATH=$ORACLE_HOME/bin:$PATH
export NLS_LANG=SLOVENIAN_SLOVENIA.UTF8

arhiv_baze()
{
###/u01/app/oracle/product/12.2.0/dbhome_1/bin/expdp system/orc1 schemas=$1 directory=dumpdmp dumpfile=$1_${D1}  logfile=exp_orc1.log
cd /home/dmp
/u01/app/oracle/product/12.2.0/dbhome_1/bin/expdp system/orc1 schemas=$1 directory=dumpbkp dumpfile=$1_${D1} logfile=exp_$1_${D1}.log
/usr/bin/gzip -c /home/dmp/$1_${D1} > /home/dmp/$1_${D2}
##cp /mnt/baza/dmp/$1_${D2}  /mnt/baza/backup/baze/$1_${D2}
##scp /mnt/baza/dmp/$1_${D2}  root@192.168.0.14:/mnt/evo/backup/.
rm /home/dmp/$1_${D1}
}




arhiv_baze "marina"


#rsync -a /home/dmp/depo_${D2}.dmp.gz 192.168.0.101://baze/backup/depo/depo.$(date +%a).dmp
#rsync -a /home/dmp/verzije_${D2}.dmp.gz 192.168.0.101://baze/backup/depo/verzije.$(date +%a).dmp.gz
#rsync -a /home/dmp/cr_katalog_ibi_${D2}.dmp.gz 192.168.0.101://baze/backup/depo/cr_katalog_ibi.$(date +%a).dmp.gz
#rsync -a /home/dmp/cr_katalog_marina_${D2}.dmp.gz 192.168.0.101://baze/backup/depo/cr_katalog_marina.$(date +%a).dmp.gz



find /home/dmp/*.dmp.gz -mtime +14 -exec rm {} \;
find /home/dmp/exp_*.log -mtime +3 -exec rm {} \;
##find /home/backup/baze/*.dmp.gz -mtime +6 -exec rm {} \;

 
 




