rodrigobertin
8/6/2019 - 12:26 PM

Back Database Postgres

Backup database postgres with docker

#!/usr/bin/env bash
export BASE=$(pwd)
export PASS=<pass>
export FILE=database.dump
export FILE_ST=database_st.dump
export DATABASE=<datbase>
export USER=<postgres>
export HOST_REMOTE=<remote>

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 ${HOST_REMOTE}

echo '---------------> Copy Files'
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} --create --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