annotate scripts/backup-container.sh @ 165:37d03f440b4b hotfix/v1.2.1

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