54
|
1 #!/bin/bash
|
159
|
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
|
188
|
12 projectDir=${2:-$PWD}
|
|
13
|
|
14 cd $projectDir
|
|
15
|
159
|
16 source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh"
|
|
17
|
287
|
18 project=$(composeGetProject)
|
|
19
|
159
|
20 set -o pipefail
|
|
21 mkdir -p ./backup
|
314
|
22 ##
|
|
23 ##THis one puts a space before the IRN
|
|
24 IRN=$(docker-compose exec -T $container psql --username=postgres --dbname=$container -t -c 'select irn from organization')
|
|
25 ##Trim function in postgres didn't work - so take out the extra space this way.
|
|
26 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'`
|
159
|
27
|
314
|
28 backupFile=./backup/${IRN}${project}-${container}.$(date +%Y-%m-%d-%H-%M-%S).backup
|
|
29 #echo "IRNS${IRNS}"
|
|
30 echo "IRN${IRN}"
|
|
31
|
|
32 echo "backup file ${backupFile}"
|
274
|
33 echo "preparing to backup ${container} on current project at ${projectDir}:"
|
|
34 echo " "
|
|
35
|
287
|
36
|
159
|
37
|
|
38 if [ "$project" == "" ]; then
|
|
39 echo "no project available"
|
|
40 exit 1
|
|
41 fi
|
|
42
|
|
43 echo "starting backup of $container for $project"
|
188
|
44 docker-compose exec -T $container sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile}
|
159
|
45
|
|
46 if [[ $( grep --count "CREATE TABLE" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then
|
|
47 echo "ERROR: backup verification FAILED"
|
267
|
48 echo "ERROR: $(tail ${backupFile})"
|
159
|
49 exit 1
|
|
50 fi
|
|
51
|
|
52 gzip ${backupFile}
|
|
53
|
|
54
|
|
55 echo "completed backup of $container for $project to ${backupFile}"
|
|
56
|