#!/bin/bash

#
# zalohovaci skript WinStrom
#

DIR=$1

if [ x"$DIR" = x"" ]; then
    echo "$0 [backup_dir]" 2> /dev/stderr
    exit 1
fi

if [ x"$USER" != x"root" ]; then
    echo "This script can be run only as a root user" 2> /dev/stderr
    exit 2
fi

DBUSER=postgres
DT=`date "+%Y%m%d"`

# Volba pro zpetnou kompatibilitu zaloh z PostgreSQL 9.0 nebo novejsi obnovovanych na starsi verzi
PG_PRE_V9=`pg_dump -V | grep -E "\(PostgreSQL\) [0-8]\.?" &> /dev/null ; echo $?`
if [ $PG_PRE_V9 -eq 0 ]; then
    PGOPTIONS=''
else
    PGOPTIONS='-c bytea_output=escape'
fi

BACKUPDIR=/tmp/flexibeee-pgsql-backup/

rm -rf $BACKUPDIR
mkdir -p $DIR
mkdir -p $BACKUPDIR
chown postgres $BACKUPDIR
cd $BACKUPDIR


for i in `su $DBUSER -c "psql -l -A -t" | cut -d\| -f 1 | grep -v template0 | grep -v template1 | grep -v = | grep -v postgres | grep -v centralServer | sed "y/\//-/"`; do
    DBNAME=$i
    MY_FILE=$BACKUPDIR$DBNAME-$DT
    if [ -f $MY_FILE.flexibee-backup ]; then
        MY_FILE="$MY_FILE-2"
    fi
    su $DBUSER -c "/usr/bin/pg_dump -U $DBUSER $DBNAME -b -Fc --file=$MY_FILE.flexibee-backup"
    mv $MY_FILE.flexibee-backup $DIR/ > /dev/null
    if [ -f $MY_FILE.flexibee-backup ]; then
        rm $MY_FILE.flexibee-backup
    fi
done

su $DBUSER -c "pg_dumpall -U $DBUSER --globals-only > $BACKUPDIR/globals-$DT.sql"
mv $BACKUPDIR/globals-$DT.sql $DIR/ > /dev/null

exit 0
