# Script to check for available cPanel backups
#
# CHANGELOG
# Version 1.0 - Bill McLaughlin
#             - Initial Version
# Version 5.05 03/04/2014
#
clear
############### Set Initial Defines ##############
LEGACY_A="/backup/cpbackup"
LEGACY_D="0"
LEGACY_W="0"
LEGACY_M="0"

NEW_D="0"
NEW_W="0"
NEW_M="0"
#################################################


if [ -z $1 ]; then

                echo -e "$(tput bold) "
    read -p  "Check Available Backups for which cPanel Username? " user; echo -e "$(tput sgr 0)"
else
    user=$1
fi

HOMEDIR=$(grep ^$user: /etc/passwd | cut -d: -f6)

if [ ! -f /var/cpanel/users/$user ]; then
    echo "$(tput bold)$(tput setaf 7)$user's $(tput setaf 1)$(tput bold)user file missing. This is likely an invalid user. $(tput setaf 3)$(tput bold)Checking for backups anyway...$(tput sgr 0)"

elif [ "$HOMEDIR" == "" ];then
    echo "$(tput setaf 1)$(tput bold)Couldn't determine home directory for $(tput sgr 0)$(tput setaf 7)$user$(tput sgr 0)"

else

THE_DOMAIN=$(grep $user /etc/trueuserdomains |  awk '{ print $1 }')
echo -e "$(tput setaf 3)$(tput bold)Checking For Backups for the domain $(tput setaf 7)$(tput bold)$THE_DOMAIN$(tput sgr 0)"
echo -e "$(tput setaf 3)$(tput bold)Checking For $(tput sgr 0)$(tput setaf 7)$(tput bold)Legacy$(tput sgr 0)$(tput setaf 3)$(tput bold) Backups...$(tput sgr 0)"
echo ""

fi
#################################### Check and set Legacy variables #######################################
if [ ! -d $LEGACY_A ]; then
        echo -e "$(tput setaf 1)$(tput bold)Sorry, there does not appear to be any Legacy Backups at all!$(tput sgr 0)"
        echo ""
		LEGACY_A="0"
fi
if [ ! -d /backup/cpbackup/daily/$user ]; then
	LEGACY_D="0"
else
	LEGACY_D="/backup/cpbackup/daily/$user"
fi
if [ ! -d /backup/cpbackup/weekly/$user ]; then
	LEGACY_W="0"
else
	LEGACY_W="/backup/cpbackup/weekly/$user"
fi
if [ ! -d /backup/cpbackup/monthly/$user ]; then
	LEGACY_M="0"
else
	LEGACY_M="/backup/cpbackup/monthly/$user"
fi

################################# Checking for Legacy Backups ######################################

if [ ! $LEGACY_D == "0" ]; then
		echo -e "$(tput setaf 2)$(tput bold)Great News! It looks like there are Daily Legacy Backups Available!$(tput sgr 0)"
        echo -e "$(tput bold)========================[ Legacy Daily Backups ]=============================$(tput sgr 0)"

		ls -o /backup/cpbackup/daily/$user/version | cut -c 23-

		echo -e "$(tput bold)=============================================================================$(tput sgr 0)"

		echo ""		
else
		echo ""
		echo -e "$(tput setaf 1)$(tput bold)Sorry, there does not appear to be any $(tput sgr 0)$(tput setaf 7)$(tput bold)Daily$(tput sgr 0)$(tput setaf 1)$(tput bold) Legacy Backups at all for the user $(tput sgr 0)$(tput setaf 7)$(tput bold)$user$(tput sgr 0)$(tput setaf 1)$(tput bold)! Checking for Legacy Weekly Backups Now...$(tput sgr 0)"
fi		
if [ ! $LEGACY_W == "0" ]; then
		echo -e "$(tput setaf 2)$(tput bold)Great News! It looks like there are Weekly Legacy Backups Available!$(tput sgr 0)"
        echo -e "$(tput bold)========================[ Legacy Weekly Backups ]============================$(tput sgr 0)"

		ls -o /backup/cpbackup/weekly/$user/version | cut -c 23-

		echo -e "$(tput bold)=============================================================================$(tput sgr 0)"

		echo ""
else
		echo -e "$(tput setaf 1)$(tput bold)Sorry, there does not appear to be any $(tput sgr 0)$(tput setaf 7)$(tput bold)Weekly$(tput sgr 0)$(tput setaf 1)$(tput bold) Legacy Backups at all for the user $(tput sgr 0)$(tput setaf 7)$(tput bold)$user$(tput sgr 0)$(tput setaf 1)$(tput bold)! Checking for Legacy Monthly Backups Now...$(tput sgr 0)"
fi
if [ ! $LEGACY_M == "0" ]; then
		echo -e "$(tput setaf 2)$(tput bold)Great News! It looks like there are Monthly Legacy Backups Available!$(tput sgr 0)"
        echo -e "$(tput bold)========================[ Legacy Monthly Backups ]===========================$(tput sgr 0)"

		ls -o /backup/cpbackup/monthly/$user/version | cut -c 23-

		echo -e "$(tput bold)=============================================================================$(tput sgr 0)"
		echo ""
else
		echo -e "$(tput setaf 1)$(tput bold)Sorry, there does not appear to be any $(tput sgr 0)$(tput setaf 7)$(tput bold)Monthly$(tput sgr 0)$(tput setaf 1)$(tput bold) Legacy Backups at all for the user $(tput sgr 0)$(tput setaf 7)$(tput bold)$user$(tput sgr 0)$(tput setaf 1)$(tput bold)! Done Checking Legacy Backups...$(tput sgr 0)"
fi
###################################### End Legacy #########################################


echo ""
echo -e "$(tput setaf 3)$(tput bold)Checking For $(tput sgr 0)$(tput setaf 7)$(tput bold)New Style$(tput sgr 0)$(tput setaf 3)$(tput bold) Backups...$(tput sgr 0)"
echo ""

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

########################### Checking for New Style Backups ################################

NEW_D=$(find /backup/20* -name $user.tar.gz 2> /dev/null | wc -l)
if [ $NEW_D -lt "1" ]; then
		echo -e "$(tput setaf 1)$(tput bold)There Does not appear to be any New Style $(tput sgr 0)$(tput setaf 7)$(tput bold)Daily$(tput sgr 0)$(tput setaf 1)$(tput bold) backups!$(tput sgr 0)"
	else
		echo -e "$(tput setaf 2)$(tput bold)Great News! It looks like there are $(tput setaf 7)$(tput bold)Daily$(tput setaf 2) New Style Backups Available!$(tput sgr 0)"
        echo -e "$(tput bold)========================[ New Style Daily Backups ]==========================$(tput sgr 0)"

		find /backup/20* -name $user.tar.gz

		echo -e "$(tput bold)=============================================================================$(tput sgr 0)"
fi
NEW_W=$(find /backup/weekly/20* -name $user.tar.gz 2> /dev/null | wc -l)
if [ $NEW_W -lt "1" ]; then
		echo -e "$(tput setaf 1)$(tput bold)There Does not appear to be any New Style $(tput sgr 0)$(tput setaf 7)$(tput bold)Weekly$(tput sgr 0)$(tput setaf 1)$(tput bold) backups!$(tput sgr 0)"
	else

		echo -e "$(tput setaf 2)$(tput bold)Great News! It looks like there are $(tput setaf 7)$(tput bold)Weekly$(tput setaf 2) New Style Backups Available!$(tput sgr 0)"
        echo -e "$(tput bold)========================[ New Style Weekly Backups ]=========================$(tput sgr 0)"
        echo ""
		find /backup/weekly/20* -name $user.tar.gz

		echo -e "$(tput bold)=============================================================================$(tput sgr 0)"		
fi
NEW_M=$(find /backup/monthly/20* -name $user.tar.gz 2> /dev/null | wc -l)
if [ $NEW_M -lt "1" ]; then
		echo -e "$(tput setaf 1)$(tput bold)There Does not appear to be any New Style $(tput sgr 0)$(tput setaf 7)$(tput bold)Monthly$(tput sgr 0)$(tput setaf 1)$(tput bold) backups!$(tput sgr 0)"
	else
		echo ""
		echo -e "$(tput setaf 2)$(tput bold)Great News! It looks like there are $(tput setaf 7)$(tput bold)Monthly$(tput setaf 2) New Style Backups Available!$(tput sgr 0)"
        echo -e "$(tput bold)========================[ New Style Monthly Backups ]========================$(tput sgr 0)"

		find /backup/monthly/20* -name $user.tar.gz

		echo -e "$(tput bold)=============================================================================$(tput sgr 0)"     
fi
#################################### End New Style ########################################

echo ""
echo -e "$(tput setaf 3)$(tput bold)Checking For $(tput sgr 0)$(tput setaf 7)$(tput bold)'Other'$(tput sgr 0)$(tput setaf 3)$(tput bold) Backups...$(tput sgr 0)"
echo ""
########################### Checking for 'Other' Backups ################################

OTHER_1=$(find /home -maxdepth 1 -name *$user*.tar.gz 2> /dev/null | wc -l)
if [ $OTHER_1 -lt "1" ]; then
		echo -e "$(tput setaf 1)$(tput bold)There Does not appear to be any $(tput sgr 0)$(tput setaf 7)$(tput bold)'Other'$(tput sgr 0)$(tput setaf 1)$(tput bold) backups in $(tput setaf 7)$(tput bold)[ /home ]$(tput setaf 1)!$(tput sgr 0)"
	else
		echo -e "$(tput setaf 2)$(tput bold)Great News! It looks like there are $(tput setaf 7)$(tput bold)'Other'$(tput sgr 0) $(tput setaf 2)$(tput bold)Backups Available in $(tput setaf 7)$(tput bold)[ /home ]$(tput setaf 2)$(tput bold)!$(tput sgr 0)"
        echo -e "$(tput bold)=======================[ 'Other' Backups in /home ]===========================$(tput sgr 0)"

		find /home -maxdepth 1 -name *$user*.tar.gz

		echo -e "$(tput bold)==============================================================================$(tput sgr 0)"
fi
OTHER_2=$(find /home/$user -maxdepth 1 -name *$user*.tar.gz 2> /dev/null | wc -l)
if [ $OTHER_2 -lt "1" ]; then
		echo -e "$(tput setaf 1)$(tput bold)There Does not appear to be any $(tput sgr 0)$(tput setaf 7)$(tput bold)'Other'$(tput sgr 0)$(tput setaf 1)$(tput bold) backups in $(tput setaf 7)$(tput bold)[ /home/$user ]$(tput setaf 1)!$(tput sgr 0)"
	else
		echo ""
		echo -e "$(tput setaf 2)$(tput bold)Great News! It looks like there are $(tput setaf 7)$(tput bold)'Other'$(tput sgr 0) $(tput setaf 2)$(tput bold)Backups Available in $(tput setaf 7)$(tput bold)[ /home/$user ]$(tput setaf 2)$(tput bold)!$(tput sgr 0)"
        echo -e "$(tput bold)====================[ 'Other' Backups in /home/$user ]=====================$(tput sgr 0)"

		find /home/$user -maxdepth 1 -name *$user*.tar.gz

		echo -e "$(tput bold)==============================================================================$(tput sgr 0)"		
fi
OTHER_3=$(find /root -maxdepth 1 -name *$user*.tar.gz 2> /dev/null | wc -l)
if [ $OTHER_3 -lt "1" ]; then
		echo -e "$(tput setaf 1)$(tput bold)There Does not appear to be any $(tput sgr 0)$(tput setaf 7)$(tput bold)'Other'$(tput sgr 0)$(tput setaf 1)$(tput bold) backups in $(tput setaf 7)$(tput bold)[ /root ]$(tput setaf 1)!$(tput sgr 0)"
	else
		echo ""
		echo -e "$(tput setaf 2)$(tput bold)Great News! It looks like there are $(tput setaf 7)$(tput bold)'Other'$(tput sgr 0) $(tput setaf 2)$(tput bold)Backups Available in $(tput setaf 7)$(tput bold)[ /root ]$(tput setaf 2)$(tput bold)!$(tput sgr 0)"
        echo -e "$(tput bold)=======================[ 'Other' Backups in /root ]==========================$(tput sgr 0)"

		find /root -maxdepth 1 -name *$user*.tar.gz

		echo -e "$(tput bold)=============================================================================$(tput sgr 0)"     
fi
#################################### End 'Other' ########################################
