#!/bin/sh

PGDATA="$1"
PGBACKUP="$2"

if [ -z "$PGDATA" ]
then
    echo "Usage: $0 database-path backup-sql"
    exit 1
fi

if [ -z "$PGBACKUP" ]
then
    echo "Usage: $0 database-path backup-sql"
    echo "Usage example:$0  /srv/pgsql/data /srv/pgsql/yedek/yedek-9.6.3"
    exit 1
fi

if [ ! -f "$PGBACKUP" ]
then
    echo "$PGBACKUP not found"
    echo "Usage example:$0  /srv/pgsql/data /srv/pgsql/yedek/yedek-9.6.3"
    exit 1
fi

# servis durdurulur.
servis postgresql stop

# eski yedeğe alınır.
_ustdizin=$(dirname $PGDATA)
mv ${_ustdizin}/data ${_ustdizin}/eskidata

# yeni dizin ve izinleri atanır.
mkdir ${_ustdizin}/data 
chown postgres:postgres ${_ustdizin}/data 

# yeni vt oluşturulur.
su - postgres -c '/usr/bin/initdb -E UTF8 -D /srv/pgsql/data'

servis postgresql start
sleep 3

su - postgres -c "psql -f $PGBACKUP"

