#!/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_HOME=/u01/app/oracle/product/11.2.0/xe
export ORACLE_SID=XE

export NLS_LANG=`$ORACLE_HOME/bin/nls_lang.sh`
export ORACLE_BASE=/u01/app/oracle

arhiv_baze()
{
/u01/app/oracle/product/11.2.0/xe/bin/expdp system/orc1 schemas=$1 directory=dumpdmp dumpfile=$1_${D1}  logfile=exp_$1_${D1}.log
/usr/bin/gzip -c /home/dmp/$1_${D1} > /home/dmp/$1_${D2}
cp /home/dmp/$1_${D2}  /mnt/backup/$1_${D2}
rm /home/dmp/$1_${D1}
##/home/dmp/mailnotify /mnt/backup/$1_${D2} support@2gen.com.au
}




arhiv_baze "marina"




find /home/dmp/*.dmp.gz -mtime +180 -exec rm {} \;
find /mnt/backup/*.dmp.gz -mtime +30 -exec rm {} \;

 
 




