view scripts/backup-container.sh @ 275:92a5ca7de9f5 hotfix/v2.2.1

flow: Closed <hotfix> 'v2.2.1'.
author smith@nwoca.org
date Wed, 14 Nov 2018 22:05:33 +0000
parents 7723b18b112b
children 7710677f147c
line wrap: on
line source
#!/bin/bash
# Executes a database backup for the specifed database container.  
# The current directory is expected to contaion 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
backupFile=./backup/${container}.$(date +%Y-%m-%d-%H-%M-%S).backup

echo "preparing to backup ${container} on current project at ${projectDir}:"
docker-compose ps
echo " "

project=$(composeGetProject)

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

echo "starting backup of $container for $project"
docker-compose exec -T $container 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: $(tail ${backupFile})"
   exit 1
fi

gzip ${backupFile}


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