annotate scripts/backup-container.sh @ 320:58c49a386a11

making changes to remote backup scripts to create directory
author aldrich@ssdt-ohio.org
date Thu, 31 Oct 2019 14:34:53 +0100
parents d78b45c28205
children 2cb1093f9aef
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
188
2f5f41d237e7 DEP-12: fix backup script.
smith@nwoca.org
parents: 186
diff changeset
12 projectDir=${2:-$PWD}
2f5f41d237e7 DEP-12: fix backup script.
smith@nwoca.org
parents: 186
diff changeset
13
2f5f41d237e7 DEP-12: fix backup script.
smith@nwoca.org
parents: 186
diff changeset
14 cd $projectDir
2f5f41d237e7 DEP-12: fix backup script.
smith@nwoca.org
parents: 186
diff changeset
15
159
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
16 source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh"
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
17
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents: 280
diff changeset
18 project=$(composeGetProject)
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents: 280
diff changeset
19
159
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
20 set -o pipefail
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
21 mkdir -p ./backup
314
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
22 ##
320
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 314
diff changeset
23 ##This one puts a space before the IRN
314
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
24 IRN=$(docker-compose exec -T $container psql --username=postgres --dbname=$container -t -c 'select irn from organization')
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
25 ##Trim function in postgres didn't work - so take out the extra space this way.
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
26 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'`
159
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
27
320
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 314
diff changeset
28 ## if database is empty, it will put IRN 000000
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 314
diff changeset
29 if [ "$IRN" == "" ]; then
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 314
diff changeset
30 echo "no IRN set,using 000000"
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 314
diff changeset
31 IRN=000000
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 314
diff changeset
32 fi
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 314
diff changeset
33
314
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
34 backupFile=./backup/${IRN}${project}-${container}.$(date +%Y-%m-%d-%H-%M-%S).backup
320
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 314
diff changeset
35
314
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
36 echo "IRN${IRN}"
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
37
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
38 echo "backup file ${backupFile}"
274
7723b18b112b add debuging logic for omeresa
smith@nwoca.org
parents: 267
diff changeset
39 echo "preparing to backup ${container} on current project at ${projectDir}:"
7723b18b112b add debuging logic for omeresa
smith@nwoca.org
parents: 267
diff changeset
40 echo " "
7723b18b112b add debuging logic for omeresa
smith@nwoca.org
parents: 267
diff changeset
41
320
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 314
diff changeset
42 #If the project is empty, we want to stop the process because this is being run from the wrong directory
159
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
43
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
44 if [ "$project" == "" ]; then
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
45 echo "no project available"
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
46 exit 1
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
47 fi
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
48
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
49 echo "starting backup of $container for $project"
188
2f5f41d237e7 DEP-12: fix backup script.
smith@nwoca.org
parents: 186
diff changeset
50 docker-compose exec -T $container sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile}
159
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
51
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
52 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
53 echo "ERROR: backup verification FAILED"
320
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 314
diff changeset
54 echo "Error: empty database"
267
fcee81af7e71 simplify method of getting project name
smith@nwoca.org
parents: 188
diff changeset
55 echo "ERROR: $(tail ${backupFile})"
320
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 314
diff changeset
56 # We want the process to continue even if the db is blank
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 314
diff changeset
57 # exit 1
159
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
58 fi
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
59
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
60 gzip ${backupFile}
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
61
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
62
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
63 echo "completed backup of $container for $project to ${backupFile}"
303259c08b7d DEP-12: update backup scripts
smith@nwoca.org
parents: 54
diff changeset
64