#!/bin/bash

CUSTOMER=$(hostname |awk -Fcloud- '{print $2 }')
OC_PATH='/var/www/nextcloud'
BASE_SRC_PATH='/var/www/nextcloud/'

BASE_DST_PATH='fm1359@rsync.virtualhostcr.com:nextcloud'
DB_USER=''
DB_PASS=''
DB_NAME=''
DB_HOST=''
## ..


SNAP=''
LOGDIR='/var/log/backup/'
LOGFILE=$LOGDIR"process.log"
ROTATEDLOG=$LOGDIR"process-$(date +%F).log"
#
## pass used in file .backup_mail_cred.txt
MUTT_CRED=/root/.backup_mail_cred.txt

MUTTRC=/root/work/.muttrc
cat <<EOF > $MUTTRC
set use_from=yes
set realname="Cloud Backup system"
set from="backup@macaitech.com"
set envelope_from="yes"
set ssl_starttls=yes
set smtp_pass=`cat $MUTT_CRED`

set smtp_url='smtps://aguila@virtualhostcr.com@smtp.virtualhostcr.com:465/'
EOF


function do_log(){
	echo -e $(date +"%F %T %Z"): $1 | tee -a $LOGFILE
}

function initialize(){
  if [ -f /usr/bin/snap ]; then
     SNAP=`snap list|grep nextcloud|awk '{print $1}'`
  fi
  if [  "$SNAP" != "nextcloud" ]; then
     DB_USER=`cat  ${OC_PATH}/config/config.php |grep dbuser | awk '{print $3};' | sed "s/[',]//g"`
     DB_PASS=`cat  ${BASE_SRC_PATH}/config/config.php |grep dbpassword | awk '{print $3};' | sed "s/[',]//g"`
     DB_NAME=`cat  ${BASE_SRC_PATH}/config/config.php |grep dbname | awk '{print $3};' | sed "s/[',]//g"`
     DB_HOST=`cat  ${OC_PATH}/config/config.php | grep dbhost | awk '{print $3};' | sed "s/[',]//g"`
  fi
  mv $LOGFILE $ROTATEDLOG
  if [ ! -d /var/log/backup ]; then
     mkdir /var/log/backup
     touch $LOGFILE
 fi
  
 do_log "Running backup with the following params
    { DB_USER: '$DB_USER', 
    DB_HOST: '$DB_HOST', 
    DB_NAME: '$DB_NAME'', 
    CUSTOMER: '$CUSTOMER',
    SNAP: '$SNAP' }
   "
 
}


function do_backup(){
   ## If this nextcloud snap version. 
  if [ "$SNAP" != "nextcloud" ]; then
    do_log "Maitenance mode to on"
    sudo -u www-data php ${OC_PATH}/occ maintenance:mode --on 
    do_log  "Starting BD backup.."
    mysqldump --single-transaction --no-tablespaces -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME > $BASE_SRC_PATH/db_backup.bak
    do_log "Maintenance mode to off"
    sudo -u www-data php ${OC_PATH}/occ maintenance:mode --off
    cd $BASE_SRC_PATH
    do_log "rsync data files"
    rsync -avrz --delete-after --stats  ${BASE_SRC_PATH} ${BASE_DST_PATH}/${CUSTOMER} |tee -a $LOGFILE 
  else 
  ## Else is tradicional instalatio under /var/www directory.
     do_log "SNAP - Exporting config and DB"
     nextcloud.export -a -c| tee -a $LOGFILE > /dev/null
     if [ $? -ne 0 ]; then
        do_log "Error during nextcloud.export command. Exiting..."
        return 1
     fi
     BKPDIR=$(tail $LOGFILE | grep exported | awk '{print $3}')
     do_log "SNAP - rsync config and DB"
     rsync -avrz --delete-after --stats --exclude data "${BKPDIR}/" ${BASE_DST_PATH}/${CUSTOMER} |tee -a $LOGFILE
     do_log "SNAP - rsync data files"
     DATA_SRC=$(nextcloud.occ config:system:get  -- datadirectory )
     rsync -avrz --delete-after ${DATA_SRC}  ${BASE_DST_PATH}/${CUSTOMER} |tee -a $LOGFILE
 fi
 do_log "End of backup - starting clean up"
}

function do_cleanup(){
  do_log "Cleaning up old backups..."
  if [ "$SNAP" = "nextcloud" ]; then
     do_log "Removing SNAP backup..." 
     rm -rf /var/snap/nextcloud/common/backups/${bkpfile}
  else 
     do_log "Removing regular DB backup..."
    rm $BASE_SRC_PATH/db_backup.bak
  fi 
}

function do_report(){
  tail -n20 $LOGFILE > /tmp/report-body.txt
  echo "" | mutt -F $MUTTRC -s "Nextcloud-Backup - $CUSTOMER" -a $LOGFILE -i /tmp/report-body.txt -- mau.fdez@macaitech.com

}



initialize
do_backup
do_report
do_cleanup
