#! /bin/bash

#checking cPanel is installed
if [[ -f /usr/local/cpanel/version ]] ; then

#Checking cPanel backup running or not
backup_ps_check=`ps aux | grep -w /usr/local/cpanel/bin/backup | grep -v "grep" | awk {'print$2'}`

if [[ $backup_ps_check -eq "" ]]; then

#checking the cPanel backup conf
BACKUPDIR=`cat /var/cpanel/backups/config | grep BACKUPDIR | cut -d ":" -f2 | tr -d "[:blank:]"`;
bk_check=`cat /var/cpanel/backups/config | grep BACKUPENABLE | cut -d "'"  -f2`;
bk_days=`cat /var/cpanel/backups/config | grep BACKUPDAYS | cut -d ":" -f2`;
#BACKUP_WEEKLY_ENABLE=`cat /var/cpanel/backups/config | grep BACKUP_WEEKLY_ENABLE | cut -d "'"  -f2`;
#BACKUP_WEEKLY_DAY=`cat /var/cpanel/backups/config | grep BACKUP_WEEKLY_DAY | cut -d ":"  -f2`;
#BACKUP_MONTHLY_ENABLE=`cat /var/cpanel/backups/config | grep BACKUP_MONTHLY_ENABLE | cut -d "'"  -f2`;
#BACKUP_MONTHLY_DATES=`cat /var/cpanel/backups/config | grep BACKUP_MONTHLY_DATES | cut -d ":"  -f2`;
backupcheck=`echo $bk_check $BACKUP_WEEKLY_ENABLE $BACKUP_MONTHLY_ENABLE | grep "yes"`;
backupdays=`echo $bk_days $BACKUP_WEEKLY_DAY $BACKUP_MONTHLY_DATES`;

#Checking backup is enabled or not
        if [[ $backupcheck != "" ]] ;  then
	
#checking current day is scheduled for backup
		bkp_check=$(echo $backupdays | grep "$(date '+%w')\|15" ); 
		
		
		if [[ $bkp_check != "" ]] ; then

	
		bk_days_chk=$(ls -l /usr/local/cpanel/logs/cpbackup/*.log | grep "$(date '+%b %e')");
#Checking backup complete staus

		
		#transporter_file=$(ls -l -t /qusr/local/cpanel/logs/cpbackup_transporter/*log | grep "$(date '+%b %e')" | head -n 1 |  awk {'print$9'});
		
		if [[ -f  $(ls -l -t /usr/local/cpanel/logs/cpbackup_transporter/*log 2> /dev/null| grep "$(date '+%b %e')" | head -n 1 |  awk {'print$9'}) ]]; then 
				
		backup_transporter_check=$(grep -i "failed\|Failure" $(ls -l -t /usr/local/cpanel/logs/cpbackup_transporter/*log | grep "$(date '+%b %e')" | head -n 1 |  awk {'print$9'}) | grep "$(date '+%F')" | grep -v "<<<");
		fi
#Checking current day backup log				
				       if [[ $bk_days_chk == "" ]] ; then
						echo "Backup not started yet";
					        exit 0
				       else
						
						if [[ -f $((ls -lt /usr/local/cpanel/logs/cpbackup/*.log 2> /dev/null | grep "$(date '+%b %e')" | awk {'print$9'}  | head -n 1 ) | cut -d : -f5 | awk {'print$1'}) ]]; then

						backup_status_check=$(grep "Final state is Backup" $(ls -lt /usr/local/cpanel/logs/cpbackup/*.log | grep "$(date '+%b %e')"| awk {'print$9'}  | head -n 1 ) | cut -d : -f5 | awk {'print$1'} ) ;	
		
						
						if [[ $backup_status_check == "Success" ]] ; then
							if [[ 	$backup_transporter_check == ""	]] ; then
								echo "Backup OK";
								exit 0
							else 
								echo "Backup Failure :: Transporter ERROR";
								exit 2
							fi
						else	
							echo "Backup Failure";
							exit 2
						fi
						fi 	
				       fi
			else echo "Backup not scheduled for today";
                	exit 0
                	fi
		
	else echo "Backup Not enabled";
        exit 2
	fi
else echo "Backup is running with PID $backup_ps_check :: SKIPPING CHECK";
exit 0	
fi

else echo "The script only for cPanel servers";
exit 1
fi
