#!/bin/bash

###
# Script de sincronizaci�n con la nube
# Para Chic Pasamaneria
# Mauricio Fernandez
# MACAI / Virtualhostcr
# 2021
###

#Variables Necesarias
#Parametros
#Cliente para que se va a relizar
CUSTOMER="chic"
#Username del customer
CUSTUSR="chic"

## Yes/No
BACKUPDB="No"
BDLOCATION="/path/to/bd_backups/"
COMPRESSBD="No"

# VSS
VSS="Yes"
DRIVE="g:"
#INTERNALV
VSSUUID=""
VSSPATH=""
VSSF=vssfile-$$.txt


#####################

##
####
#   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/g/Datos/logs/summary-${MONTH}-${YEAR}.csv

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

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

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

#Fuente de donde se van a tomar los archivos originales
ROOTSRC="/cygdrive/g"
SRC="/Datos /QBKS"
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"



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


function get_error () {

IFS="
"
cat << EOF | grep -w ^$1
0 Success
1 Syntax or usage error
2 Protocol incompatibility
3 Errors selecting input/output files, dirs
4 Requested action not supported: an attempt was made  to  manipulate  64-bit files on a platform that cannot support them
5 Error starting client-server protocol
6 Daemon unable to append to log-file
10 Error in socket I/O
11 Error in file I/O
12 Error in rsync protocol data stream
13 Errors with program diagnostics
14 Error in IPC code
20 Received SIGUSR1 or SIGINT
21 Some error returned by waitpid()
22 Error allocating core memory buffers
23 Partial transfer due to error
24 Partial transfer due to vanished source files
25 The --max-delete limit stopped deletions
30 Timeout in data send/receive
35 Timeout waiting for daemon connection
EOF


}

function use_vss() {

echo '#### Creating VSS Snapshot for backup '

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

#mkdir /cygdrive/${DRIVE%:}/vss-$$
cmd /c md g:\\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
 rm -rf /cygdrive/g/vss
}


function curl_err () {
IFS="
"
if [ $1 -le 7 ] && [ $1 -ge 0 ]; then
cat << EOF | egrep ^$1
1 Unsupported protocol. This build of curl has no support for this protocol.
2 Failed to initialize.
3 URL malformed. The syntax was not correct.
4 A  feature  or  option  that  was  needed to perform the desired request was not enabled.
5 Couldn't resolve proxy.  The  given  proxy  host  could  not  be resolved.
6 Couldn't resolve host. The given remote host was not resolved.
7 Failed to connect to host.
EOF
else [ $1 -ne 0 ] && echo "$1 Unknown nor documented error!"
fi

}


####
#   Variables internas
####

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

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

cmd /c "robocopy d:\ServerFolders\QBKS g:\QBKS /MIR /R:2 /W:2"


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
   }
  fi
  }
  else {
     PID1=$(ps -ef |grep rsync |awk '{print $2;};')
	STATUS="Ya hay una instancia de backup corriendo ($PID1)"
	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
 }

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
