view scripts/backup-container-docker.sh @ 392:53c399aaa85d

INV-197 make workflow scripts executable
author Matt Calmes <calmes@ssdt-ohio.org>
date Mon, 25 Oct 2021 13:00:06 -0400
parents d75786228e6b
children 56498d910235
line wrap: on
line source
#!/bin/bash
# Executes a database backup for the specifed database container.  
# The current directory is expected to contain a project configured
# as with SSDT conventions for an application database.
#
# When successful, the output file will be in ./backup with the
# container name and timestamp in the file.  The format of the output
# is a compressed pg_dump (sql) format.
#
container=${1?Must provide container name to backup}
projectDir=${2:-$PWD}

cd $projectDir

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

set -o pipefail
mkdir -p ./backup

if [ "$container" = "invdb" ]; then
   function prop {
       grep "${1}" .env/ssdt-inventory-shared.properties|cut -d'=' -f2
   }
   entityId=$(prop 'entityId' | tr -d '"')
   containerName="$entityId-inventory-db"
   IRN=$(docker exec $containerName psql --username=postgres --dbname=$container -t -c 'select districtirn from inventoryconfiguration')
   ## if database is empty, it will put IRN 000000
   if [ "$IRN" == "" ]; then
      echo "no IRN set,using 000000"
      IRN=000000
   fi
   ##Take out the extra space from IRN returned from db.
   IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'`
   backupFile=./backup/${IRN}${entityId}-${container}.$(date +%Y-%m-%d-%H-%M-%S).backup
   echo "entityId is $entityId"
   echo "container name is $containerName"
fi

if [ "$container" = "workflowsdb" ]; then
   function prop {
       grep "${1}" .env/ssdt-workflows-shared.properties|cut -d'=' -f2
   }
   entityId=$(prop 'entityId' | tr -d '"')
   containerName="$entityId-workflows-db"
   backupFile=./backup/${entityId}-${container}.$(date +%Y-%m-%d-%H-%M-%S).backup
   echo "entityId is $entityId"
   echo "container name is $containerName"
fi

echo "Container is $container"
echo "IRN is $IRN"

echo "backup file ${backupFile}"
echo "preparing to backup ${container} on current project at ${projectDir}:"
echo " "

echo "starting backup of $container"
docker exec -t $containerName sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile}
#if [[ $( grep --count "CREATE TABLE" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then
#   echo "ERROR: backup verification FAILED"
#   echo "Error: empty database"
#   echo "ERROR: $(tail ${backupFile})"
  # We want the process to continue even if the db is blank
  # exit 1
#fi

gzip ${backupFile}

echo "completed backup of $container to ${backupFile}"