#!/usr/bin/bash
#cron: 
# login as hdbuser: /home/epm/tools/hdbBackup.sh -BACKUP_DIR /BACKUP/data -DB_NAME vm0 -SYS_PASSWORD Wang_2017 -SID 11 -HDBBIN /hdbsql> backup.log 2>&1

#40 3 * * * sudo su - vm0adm /data_disk1/tool/hdbBackup.sh -BACKUP_DIR /data_disk1/HANA/backup/data -DB_NAME VM0 -SYS_PASSWORD Wang_2017
#00 2 * * * find /data_disk1/HANA/backup/data -type f -mtime +2 -exec rm -r {} \;
#./hdbBackup.sh -BACKUP_DIR /BACKUP/data -DB_NAME EPM -SYS_PASSWORD Wang_2017
sysPassword='EPM_user_2022'
dbName='epm'
hdbbin='/usr/sap/hdbclient/hdbsql'
while [ $# -gt 0 ]; do
  COMMAND=$1
  case $COMMAND in   
    -HDBBIN)
      hdbbin=$2
      shift 2
      ;; 
    -SID)
      sid=$2
      shift 2
      ;;
    -BACKUP_DIR)
      backupDir=$2
      shift 2
      ;;
    -DB_NAME)
      dbName=$2
      shift 2
      ;;
    -SYS_PASSWORD)
      sysPassword=$2
      shift 2
      ;;
    *)
      break
      ;;
  esac
done


dateStr=`date '+%Y%m%d-%H%M%S'`
echo " $dateStr db backup begin, BACKUP_DIR: $backupDir, DB_NAME:$dbName, SYS_PASSWORD:$sysPassword"
# hdbsql -i 01 -d SYSTEMDB -u SYSTEM -p Wang_2017 -d EPM "BACKUP DATA FOR EPM using file ('/BACKUP/data/epm_526')"
$hdbbin -i $sid -u SYSTEM -p $sysPassword "BACKUP DATA using file ('$backupDir/$dbName_$dateStr')"

echo "backup end"