#!/bin/bash
##
####
#   Variables internas
####
DATE=$(date +%Y-%m-%d)
TIME=$(date +%H:%M )
MONTH=$(date +%b)
MONTHD=$(date +%m)
YEAR=$(date +%Y)

# Resumen de los backup de forma mensual
FSTATUS=/cygdrive/c/logs/summary-${MONTH}-${YEAR}.csv

# Resumen de las acciones diarias
LOG=/cygdrive/c/logs/rsync-$DATE.log

# duracion de la sesion
TIMER=/cygdrive/c/logs/duration.log

# localizacion de las evidencias de respaldo (incluye los registros diarios y resumen mensual)
EVIDENCEDIR=/cygdrive/c/logs/EvidenciaRespaldo

#Fuente de donde se van a tomar los archivos originales
ROOTSRC="/cygdrive/c"
SRC="/quickbooks /SistemaArchivos /logs"
EXCL=$(pwd)/excludes.lst



#Destino donde los backups se van a colocar.
DST="1132@rsync1.virtualhostcr.com:backup/$CUSTUSR"

#Proxy intermedio
PROXY="${CUSTUSR}@bkpserver.virtualhostcr.com"

function use_vss() {
echo '#### Creating VSS Snapshot for backup '

vssadmin create shadow /for=${DRIVE} > /tmp/${VSSF}
VSSPATH=$(tail -n 1 /tmp/${VSSF}  |awk '{print $5}')
VSSUUID=$(tail -n 2  /tmp/${VSSF} |head -n1 |awk '{print $4}')

#mkdir /cygdrive/${DRIVE%:}/vss-$$
cmd /c mklink /d ${DRIVE}\\vss\\vss-$$ $VSSPATH\\
ROOTSRC="/cygdrive/${DRIVE%:}/vss//vss-$$"

}

function cleanup() {

 cmd /c rmdir ${DRIVE}\\vss\\vss-$$
 cmd /c vssadmin delete shadows /quiet /shadow=$VSSUUID
}



[ -f config ] && . config

if [ -f functions.sh ]; then
  . functions.sh
else
  echo "Critical file (functions.sh) Not found... exit"
fi


# Archivo lock
PRGNAME=$(basename $0)
LOCK=/tmp/${PRGNAME}.lck
##
touch $EXCL

###
#   Variables internas
####

L_ERR=error.txt
M_SUBJ="Error de backup $CUSTOMER"
RUNNING=0

if [ -e $LOCK ]; then
   RUNNING=1
   else
     touch $LOCK
fi


if [ $RUNNING -eq 0 ]; then
{ ### Personalizacion del cliente
if [ "BACKUPDB" = "Yes" ]; then
  echo "Limpiando archivos viejos"
  find $BDLOCATION -mtime +30 -type f -delete
 fi
  if [ "$COMPRESSBD" = "Yes" ]; then
  {
   ## comprimir los  backups de DB pendientes.
    find  $BDLOCATION -name "*.bak" -exec gzip -f {} \;
    chmod 777  $BDLOCATION/*.gz
  } fi
  ####
  if [ $VSS = "Yes" ]; then
   use_vss
  fi
  echo "##########"
  echo "Inicio de sincronizacion: $DATE $TIME"
  ####
  # La sincronizacion inicia aqu�.
  ####

  TOTALTIME=0
  TOTAL_BYTES=0
  for i in $SRC; do
   echo "Respaldando $i"
  ( TIMEFORMAT='%0R' ;time rsync -avz --delete --stats -e "ssh -a -p2200 $PROXY ssh" --exclude-from=$EXCL ${ROOTSRC}$i $DST 2> error.log >> $LOG ) 2> $TIMER
   TOTALTIME=$(( $(cat $TIMER) + $TOTALTIME ))
   T=$(cat $LOG | grep "Total bytes sent"| tail -n 1 |cut -d':' -f2 | sed -e 's/,//g')
   TOTAL_BYTES=$(( $T + $TOTAL_BYTES ))
  done
   MIN=$(( $TOTALTIME / 60 ))
   SEC=$(( $TOTALTIME - $(( $MIN * 60 )) ))
   echo ${MIN}m${SEC}s > $TIMER
  ## fin de sincronizacion
  ## Verifique si hay error
  ERRNO=$?
  if [ $ERRNO -eq 0 ]; then
  {
    STATUS="OK. Completado sin problemas"
    STATE="OK"
    echo "Backup para $DATE: ok"

    }
  else {
    get_error $ERRNO >> $L_ERR
    STATUS="Error. Se  present� error al realizar la sincronzacion"
    STATE="ERROR"
    #echo "Backup $CUSTOMER para $DATE: ERROR" | email -r smtp.virtualhostcr.com -p 587 -s "$M_SUBJ" -a $L_ERR -tls -u aguila -i "27lapi8" -b -f alerts@virtualhostcr.com -n "Notificaciones Virtualhostcr" aguila@virtualhostcr.com
    send_email "Backup $CUSTOMER para $DATE: ERROR" "$M_SUBJ" $L_ERR
   }
  fi
  }
  else {
     PID1=$(ps -ef |grep rsync |awk '{print $2;};')
	STATUS="Ya hay una instancia de backup corriendo ($PID1)"
	STATE="ERROR"
  send_email "Backup $CUSTOMER para $DATE: ERROR" "$M_SUBJ" $L_ERR
	#echo "Backup $CUSTOMER para $DATE: ERROR" | email -r smtp.virtualhostcr.com -p 587 -s "$M_SUBJ" -a $L_ERR -tls -u aguila -i "27lapi8" -b -f alerts@virtualhostcr.com -n "Notificaciones Virtualhostcr" aguila@virtualhostcr.com
 }

fi
## Salva el resumen
  T=$TOTAL_BYTES
  RTIME=$(cat $TIMER)
 SIZE=$(( $T /1024 / 1024  ))
         LENTRY=$(echo "Servidor", "$DATE" "$TIME", "Archivos en la nube", "$SIZE MB", "$RTIME", rsync, Archivos y BD, Diaria, "$STATUS ")
		 # curl http://webtool.virtualhostcr.com/backups -d data="${CUSTOMER}, $LENTRY" -X POST
		 # TXTERR=$(curl_err $?)
		  echo "$LENTRY" >> $FSTATUS
		  echo "$LENTRY"
		  M_SUBJ="Reporte backup ${CUSTOMER}"
		  echo -e "Backup $CUSTOMER para $DATE: \n  $LENTRY" | email -r smtp.virtualhostcr.com -p 587 -s "$M_SUBJ" -a $FSTATUS -tls -u aguila -i "27lapi8" -b -f alerts@virtualhostcr.com -n "Notificaciones Virtualhostcr" aguila@virtualhostcr.com

#
umask 000
#set -x
cp $FSTATUS "$EVIDENCEDIR/"
# me aseguro que la carpeta correspondiente est� para dejar los logs.
if [ ! -d "$EVIDENCEDIR/${MONTHD}-${YEAR}" ]; then
  mkdir -p   "$EVIDENCEDIR/${MONTHD}-${YEAR}"
fi
cp $LOG "$EVIDENCEDIR/${MONTHD}-${YEAR}"
#set +x

# cleanning up.
rm -f $LOCK

if [ $VSS = "Yes" ]; then
  cleanup
 fi
echo "Finaliza sincronizacion: $(date)"
echo "#####"
echo
### Limpieza de los archivos viejos
