#!/bin/bash

# Use pgpass for passwords https://www.postgresql.org/docs/current/libpq-pgpass.html

set -e

DIRNAME=$(dirname "$0")
TIMESTAMP=$(date +%Y-%m-%d-%H-%M-%S)

SOURCE_DB="h_edu_sro1252" && [[ ! -z "$1" ]] && SOURCE_DB="$1"
SOURCE_HOST=localhost
SOURCE_PORT=5433
BACKUP_PATH=./"$SOURCE_DB"_$TIMESTAMP.backup
TARGET_DB="bo-dev" && [[ ! -z "$2" ]] && TARGET_DB="$2"
TARGET_HOST=localhost
TARGET_USER=bo
TARGET_PORT=5432
echo "Copying from '$SOURCE_DB@$SOURCE_HOST:$SOURCE_PORT' to  '$TARGET_DB@$TARGET_HOST:$TARGET_PORT'"

set -xu
export VERBOSITY=verbose
pg_dump --host $SOURCE_HOST --username $SOURCE_DB --port $SOURCE_PORT --file $BACKUP_PATH --clean --create --format custom --verbose --no-privileges --no-owner --no-password --schema $SOURCE_DB $SOURCE_DB
./$DIRNAME/restore-backup.sh $BACKUP_PATH $SOURCE_DB $TARGET_DB
