#!/bin/bash

# PostgreSQL Backup and Restore Script
# Zafer Çelenk (zafercelenk@gmail.com)
#
# Son Duzenleme: 04/11/2016
#

HOST="localhost"
DATABASE=$2
PORT="5432"
USER="postgres"
PASS="test"
BACKUP_FILE=$3

KLASOR="/home/zafer/pg_yedek"

function backup {
    DOSYA=$KLASOR"/"$DATABASE"_yedek_"$(date +"%Y%m%d")".backup"

    pg_dump --dbname=postgresql://$USER:$PASS@$HOST:$PORT/$DATABASE \
            --format=custom --blobs --verbose --file=$DOSYA

    echo "==== YEDEKLEME ISLEMİ TAMAMLANDI. ADRESI:$DOSYA ====="
    #pg_dump --host=$HOST --dbname=$DATABASE --username=$USER -b --verbose --file=$KLASOR/$DATABASE_yedek.backup
}

function restore {
    pg_restore --dbname=postgresql://$USER:$PASS@$HOST:$PORT/$DATABASE \
                --verbose $BACKUP_FILE

    echo "==== YEDEKDEN YUKLEME ISLEMİ TAMAMLANDI. VERITABANI:$DATABASE ====="
}


case $1 in
    "backup")
        backup
        ;;
    "restore")
        restore
        ;;
    *)
        echo "Program icin ornek kullanim soyledir:"
        echo -e "$ pgYedek backup dbname"
        echo -e "$ pgYedek restore dbname backup_file"
        ;;
esac
