comparison scripts/backup-container-docker.sh @ 399:bfed402ab6b0 production

INV-197 merge release/v2.13.0 into production
author Matt Calmes <calmes@ssdt-ohio.org>
date Thu, 28 Oct 2021 07:55:48 -0400
parents 886a7dc796b8
children 8ddeb5a36678
comparison
equal deleted inserted replaced
387:d6f2270566e9 399:bfed402ab6b0
1 #!/bin/bash
2 # Executes a database backup for the specifed database container.
3 # The current directory is expected to contain 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 projectDir=${2:-$PWD}
12
13 cd $projectDir
14
15 source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh"
16
17 set -o pipefail
18 mkdir -p ./backup
19
20 if [[ "$container" = "invdb" && -f ".env/ssdt-inventory-shared.properties" ]]; then
21 function prop {
22 grep "${1}" .env/ssdt-inventory-shared.properties|cut -d'=' -f2
23 }
24 entityId=$(prop 'entityId' | tr -d '"')
25 containerName="$entityId-inventory-db"
26 IRN=$(docker exec $containerName psql --username=postgres --dbname=$container -t -c 'select districtirn from inventoryconfiguration')
27 ## if database is empty, it will put IRN 000000
28 if [ "$IRN" == "" ]; then
29 echo "no IRN set,using 000000"
30 IRN=000000
31 fi
32 ##Take out the extra space from IRN returned from db.
33 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'`
34 backupFile=./backup/${IRN}${entityId}-inventory-db.$(date +%Y-%m-%d-%H-%M-%S).backup
35 echo "entityId is $entityId"
36 echo "container name is $containerName"
37 fi
38
39 if [[ "$container" = "workflowsdb" && -f ".env/ssdt-workflows-shared.properties" ]]; then
40 function prop {
41 grep "${1}" .env/ssdt-workflows-shared.properties|cut -d'=' -f2
42 }
43 entityId=$(prop 'entityId' | tr -d '"')
44 containerName="$entityId-workflows-db"
45 backupFile=./backup/${entityId}-workflows-db.$(date +%Y-%m-%d-%H-%M-%S).backup
46 echo "entityId is $entityId"
47 echo "container name is $containerName"
48 fi
49
50 if [ ! -z "$containerName" ]; then
51 echo "Container is $container"
52 if [ "$container" = "invdb" ]; then
53 echo "IRN is $IRN"
54 fi
55 echo "backup file ${backupFile}"
56 echo "preparing to backup ${container} on current project at ${projectDir}:"
57 echo " "
58
59 echo "starting backup of $container"
60 docker exec -t $containerName sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile}
61 #if [[ $( grep --count "CREATE TABLE" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then
62 # echo "ERROR: backup verification FAILED"
63 # echo "Error: empty database"
64 # echo "ERROR: $(tail ${backupFile})"
65 # We want the process to continue even if the db is blank
66 # exit 1
67 #fi
68
69 gzip ${backupFile}
70
71 echo "completed backup of $container to ${backupFile}.gz"
72 fi