#!/bin/bash
set -euo pipefail

if [ $# -ne 2 ]
then
    echo "Usage:: $0 dev|test file_name"
    exit 1
fi

env_name=$1
file_name=$2
db_name='none'
server_name='none'

case $env_name in
    dev)
        db_name='marvel_dev'
        server_name='marvel2-dev.cchrzodxlhy6.us-west-2.rds.amazonaws.com'
        ;;
    test)
        db_name='marvel_test'
        server_name='marvel2-test.cchrzodxlhy6.us-west-2.rds.amazonaws.com'
        ;;
    *)
        echo "Usage:: $0 dev|test"
        exit 1
        ;;
esac

if [ -f $file_name ]
then
    echo "File $file_name already exists"
    exit 1
fi

echo "======================="
echo "Starting data dump from server:$server_name db:$db_name to $file_name"
echo

pg_dump --host $server_name --username postgres -W --dbname $db_name --schema public --data-only > $file_name

echo
echo "Finished data dump from server:$server_name db:$db_name to $file_name"

