#!/bin/bash
#
# This creates a snapshot of the current database state
# and places the dumps in an encrypted archive.
#

# exit on first error
set -e

filename=$1
dumps_dir="/tmp/dumps"
tmp_filename="/tmp/dump.tar.xz"

if [ -z "$filename" ]; then
    echo "Set the filename of the snapshot as the only argument" && exit 1
fi

docker-compose down
docker-compose up -d metadb

docker exec dev_metadb_1 bash -c '\
pg_dump hub -h localhost -U user -O --inserts --column-inserts >  hub.sql; \
pg_dump datastore -h localhost -U user -O --inserts --column-inserts >  datastore.sql; \
pg_dump simpleidp -h localhost -U user -O --inserts --column-inserts >  idp.sql; \
pg_dump pantheon -h localhost -U user -O --inserts --column-inserts >  pantheon.sql; \
'

mkdir -p $dumps_dir
rm -rf "$dumps_dir/*"

docker cp dev_metadb_1:/hub.sql $dumps_dir
docker cp dev_metadb_1:/datastore.sql $dumps_dir
docker cp dev_metadb_1:/idp.sql $dumps_dir
docker cp dev_metadb_1:/pantheon.sql $dumps_dir

tar cfvJ $filename -C $dumps_dir .
rm -rf $dumps_dir

exit 0
