#!/usr/bin/env bash
export DIR=$(pwd)
export PASS=PASS_DB
export FILE=database.dump
export FILE_ST=database_st.dump
export DATABASE=<database>
export USER=<user>
export HOST_REMOTE=<host>

echo '---------------> EXPORT DUMP Complete'
docker exec -it -e PGPASSWORD=${PASS} postgres pg_dump ${DATABASE} -Fc -f /tmp/${FILE} -U ${USER} -h ${HOST_REMOTE}

echo '---------------> EXPORT SQL Complete'
docker exec -it -e PGPASSWORD=${PASS} postgres pg_dump ${DATABASE} -f /tmp/${FILE}.sql -U ${USER} -h ${HOST_REMOTE}

echo '---------------> EXPORT DUMP Estructure'
docker exec -it -e PGPASSWORD=${PASS} postgres pg_dump ${DATABASE} -s -Fc -f /tmp/${FILE_ST} -U ${USER} -h ${HOST_REMOTE}

echo '---------------> EXPORT SQL Estructure'
docker exec -it -e PGPASSWORD=${PASS} postgres pg_dump ${DATABASE} -s -f /tmp/${FILE_ST}.sql -U ${USER} -h 127.0.0.1

docker cp postgres:/tmp/${FILE} app/bash/${FILE}
docker cp postgres:/tmp/${FILE}.sql app/bash/${FILE}.sql
docker cp postgres:/tmp/${FILE_ST} app/bash/${FILE_ST}
docker cp postgres:/tmp/${FILE_ST}.sql app/bash/${FILE_ST}.sql

echo '--------------> RESTORE DUMP'
docker exec -e PGPASSWORD=${PASS} -i postgres pg_restore -h 127.0.0.1 -U ${USER} --clean --no-acl --no-owner -d ${DATABASE}  < ./app/bash/${FILE}
docker exec -e PGPASSWORD=${PASS} -i postgres pg_restore -h 127.0.0.1 -U ${USER} --clean --no-acl --no-owner -d ${DATABASE}  < ./app/bash/${FILE}

echo '--------------> RESTORE SQL'
#docker exec -e PGPASSWORD=${PASS} -i postgres psql -h 127.0.0.1 -U franco -d ${DATABASE} < back_BASE_OK.sql