view scripts/restore-container.sh @ 241:fa943a14123d production v2.0.0

flow: Merged <release> 'v2.0.0' to <master> ('production').
author smith@nwoca.org
date Wed, 10 Jan 2018 01:45:28 +0000
parents 2e0d3c70bba2
children 5be78ce3b33c
line wrap: on
line source
#!/bin/bash

source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh"

sourceFile=${1?must provide backup file to be restored}

if [ -z "$1" ]
then
  echo "Usage: `basename $0` {backup file to restore}"
  echo " must specify the name of the file to restore.  Assumed to be in format:"
  echo "     {service}.{timestamp}.backup.gz  "
  echo "       where {service} is the usasdb or uspsdb container to be restored."
  echo " The script will restore the backup to the appropriate container based "
  echo " on the filename."
  exit $E_NOARGS
fi

target=$(basename $1)
target=${target%%.*}

project=$(composeGetProject $target)

if [ "$project" == "" ]; then
   echo "no project available"
   exit 1
fi

echo 
echo "Preparing to restore"
echo "--------------------"
echo "   file: $sourceFile"
echo "   to    ${project}/${target} database: $target"
echo " "
echo "WARNING:  This operation will DELETE and replace any exising database"
echo " "

read -e -p "Continue? <y/N> " answer
case $answer in
     y | Y | yes | YES ) answer="y";;
     n | N | no | NO ) answer="n";;
     *) answer="n"
esac

if [ "$answer" == "y" ] 
then
  appService=${target/db/app}
  echo "stopping application service $appService"
  docker-compose stop $appService

  dbContainer=$(composeGetContainer $target)
 
  echo "copy backup file to database container"
  docker cp ${sourceFile} ${dbContainer}:/tmp/restore.backup.gz

  echo "starting database restore"
  docker-compose exec -T -u postgres $target sh -c "gunzip -f /tmp/restore.backup.gz -c | psql"

fi