# Online backup script using pg_basebackup
##########################################

BACKUP_DIR=/backup/postgresql/pgdata
BACKUP_COMPRESSED_DIR=/backup/postgresql/daily

if [ -z "${BACKUP_DIR}" ]
then
    echo "\${BACKUP_DIR} variable is empty"
    exit 1
else
    rm -rf ${BACKUP_DIR}/*
fi

if [ -z "${BACKUP_COMPRESSED_DIR}" ]
then
    echo "\${BACKUP_COMPRESSED_DIR} variable is empty"
    exit 1
fi


pg_basebackup -h localhost -P -D ${BACKUP_DIR}
pg_verifybackup ${BACKUP_DIR}

if [ "$?" != "0" ]
then
    echo "Verify backup failed"
    exit 1
fi

NOW=$(date '+%Y-%m-%d__%H_%M_%S')
cd ${BACKUP_DIR}
tar -cvf - * | pigz > ${BACKUP_COMPRESSED_DIR}/${NOW}.tar.gz
##########################################


# https://public.dalibo.com/exports/formation/manuels/modules/i2/i2.handout.html