comparison scripts/backup-container.sh @ 163:daf91a3bbee5 production v1.2.0

flow: Merged <release> 'v1.2.0' to <master> ('production').
author smith@nwoca.org
date Tue, 15 Aug 2017 21:59:23 +0100
parents 303259c08b7d
children 620ea742f1df
comparison
equal deleted inserted replaced
141:06611703da29 163:daf91a3bbee5
1 #!/bin/bash
2 # Executes a database backup for the specifed database container.
3 # The current directory is expected to contaion a project configured
4 # as with SSDT conventions for an application database.
5 #
6 # When successful, the output file will be in ./backup with the
7 # container name and timestamp in the file. The format of the output
8 # is a compressed pg_dump (sql) format.
9 #
10 container=${1?Must provide container name to backup}
11
12 source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh"
13
14 set -o pipefail
15 mkdir -p ./backup
16 backupFile=./backup/${container}.$(date +%Y-%m-%d-%H-%M-%S).backup
17
18 project=$(composeGetProject $container)
19
20 if [ "$project" == "" ]; then
21 echo "no project available"
22 exit 1
23 fi
24
25 echo "starting backup of $container for $project"
26 docker-compose exec $container sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile}
27
28 if [[ $( grep --count "CREATE TABLE" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then
29 echo "ERROR: backup verification FAILED"
30 echo "ERROR: $(tail ${backupFile})"
31 exit 1
32 fi
33
34 gzip ${backupFile}
35
36
37 echo "completed backup of $container for $project to ${backupFile}"
38