#!/bin/bash
################################################################################
# pg_rman_cron
#	Wrapper on pg_rman for cron jobs. This wrapper does two things:
#	- write logs of pg_rman to a dedicated, default folder to keep a track on
#	backup activity
#	- check existence of the flag enforcing full backup and enforce backup as
#	such. Flag is removed once backup has correctly finished.
#
#	Copyright (c) 2013-2016 VMware, Inc. All rights reserved.
################################################################################

THIS_DIR=`dirname $0`

# Ensure that global variables are loaded.
source /etc/profile

if [ -z $VMWARE_POSTGRES_BASE ]; then
   echo "VMWARE_POSTGRES_BASE is not set."
   echo "Check your installation."
   exit 1
fi
SANITY_FILE=$VMWARE_POSTGRES_BASE/scripts/vpostgres_sanity_checks
if [ -f $SANITY_FILE ]; then
   source $SANITY_FILE
else
   echo "Sanity check file for environment variables of VMware Postgres"
   echo "is not available. Check your installation."
   exit 1
fi

# Load parameters
PGVM_CONF=${VMWARE_POSTGRES_ETC}/pgvm.conf
if [ -f $PGVM_CONF ]
then
   . $PGVM_CONF
else
   echo "Could not find $PGVM_CONF"
   exit 1
fi

# Help message
show_help()
{
   ERROR_NUM=$1
   echo "Usage: `basename $0` [ full | incremental | archive ]"
   exit $ERROR_NUM
}

# Safety checks
# Check number of arguments
if [ $# -ne 1 ]
then
   echo "Incorrect number of arguments"
   show_help 0
fi

# Check value of given argument
BACKUP_MODE=$1
if [ $BACKUP_MODE != "incremental" -a \
     $BACKUP_MODE != "full" -a \
     $BACKUP_MODE != "archive" ]
then
   echo "Incorrect argument: $BACKUP_MODE"
   show_help 1
fi

# Now we are ready to go...
# Check the presence of the flag to enforce a full backup
if [ -f $PG_BACKUP_ENFORCE_FULL ]
then
   # Enforce backup mode to full
   BACKUP_MODE="full"
fi

# Define log file name
BACKUP_TIMESTAMP=$(date +"%Y%m%d_%H%M")
BACKUP_LOG_NAME=pg_rman_${BACKUP_MODE}_${BACKUP_TIMESTAMP}.log
BACKUP_LOG=$VMWARE_POSTGRES_LOG/$BACKUP_LOG_NAME

# If there is already more than 80% of disk space used by backup and
# recovery facilities, avoid running any new backups until user has
# freed up some space on this partition, increased the size of the
# partition to suite the needs of the system.
total_space=`df -k $VMWARE_POSTGRES_ARCHIVE | tail -n 1 | tr -s ' ' | cut -d' ' -f 2`
free_space=`df -k $VMWARE_POSTGRES_ARCHIVE | tail -n 1 | tr -s ' ' | cut -d' ' -f 4`

# Process needs to check if there is more than 20% of disk available
# or not to run the next backup. It may happen that the next backup
# indeed takes more space than that though...
ALLOWED_PERCENT=20
allowed_space=`expr $total_space \* $ALLOWED_PERCENT / 100`
if [ "$free_space" -lt "$allowed_space" ]
then
	echo "Not enough space left to take backup:" >> $BACKUP_LOG
	echo "- Free: $free_space kB" >> $BACKUP_LOG
	echo "- Total: $total_space kB" >> $BACKUP_LOG
	echo "At least $ALLOWED_PERCENT % of free space is needed to take a backup." >> $BACKUP_LOG
	exit 1
fi

# And run pg_rman
$VMWARE_POSTGRES_BIN/pg_rman backup -b $BACKUP_MODE \
	-B $VMWARE_POSTGRES_BACKUP -D $VMWARE_POSTGRES_DATA -v > $BACKUP_LOG
ERROR_NUM=$?
if [ $ERROR_NUM != 0 ]
then
	echo "Error while proceeding pg_rman, failed with error $ERROR_NUM" >> $BACKUP_LOG
	exit $ERROR_NUM
fi

# Validate the backup as well for immediate use
$VMWARE_POSTGRES_BIN/pg_rman validate \
	-B $VMWARE_POSTGRES_BACKUP -D $VMWARE_POSTGRES_DATA -v >> $BACKUP_LOG
ERROR_NUM=$?
if [ $ERROR_NUM != 0 ]
then
	echo "Error while validating pg_rman backup, failed with error $ERROR_NUM" >> $BACKUP_LOG
	exit $ERROR_NUM
fi

# Remove flag enforcing full backup if any
if [ -f $PG_BACKUP_ENFORCE_FULL ]
then
   rm -f $PG_BACKUP_ENFORCE_FULL
fi
