comparison scripts/backup-container-docker.sh @ 389:4e42c10e3c0d

INV-197 update db backup helper scripts for inventory and workflow
author Matt Calmes <calmes@ssdt-ohio.org>
date Mon, 18 Oct 2021 07:05:16 -0400
parents 23d40b345e1e
children d75786228e6b
comparison
equal deleted inserted replaced
388:23d40b345e1e 389:4e42c10e3c0d
6 # When successful, the output file will be in ./backup with the 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 7 # container name and timestamp in the file. The format of the output
8 # is a compressed pg_dump (sql) format. 8 # is a compressed pg_dump (sql) format.
9 # 9 #
10 container=${1?Must provide container name to backup} 10 container=${1?Must provide container name to backup}
11
12 projectDir=${2:-$PWD} 11 projectDir=${2:-$PWD}
13
14 project=${3?Must provide project name to backup}
15 12
16 cd $projectDir 13 cd $projectDir
17 14
18 source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh" 15 source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh"
19 16
25 grep "${1}" .env/ssdt-inventory-shared.properties|cut -d'=' -f2 22 grep "${1}" .env/ssdt-inventory-shared.properties|cut -d'=' -f2
26 } 23 }
27 entityId=$(prop 'entityId' | tr -d '"') 24 entityId=$(prop 'entityId' | tr -d '"')
28 containerName="$entityId-inventory-db" 25 containerName="$entityId-inventory-db"
29 IRN=$(docker exec $containerName psql --username=postgres --dbname=$container -t -c 'select districtirn from inventoryconfiguration') 26 IRN=$(docker exec $containerName psql --username=postgres --dbname=$container -t -c 'select districtirn from inventoryconfiguration')
27 ##Take out the extra space from IRN returned from db.
28 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'`
30 backupFile=./backup/${IRN}${entityId}-${container}.$(date +%Y-%m-%d-%H-%M-%S).backup 29 backupFile=./backup/${IRN}${entityId}-${container}.$(date +%Y-%m-%d-%H-%M-%S).backup
31 echo "entityId is $entityId" 30 echo "entityId is $entityId"
32 echo "container name is $containerName" 31 echo "container name is $containerName"
33 fi 32 fi
34
35 #If the project is empty, we want to stop the process because this is being run from the wrong directory
36
37 #if [ "$project" == "" ]; then
38 # echo "no project available"
39 # exit 1
40 #fi
41 #
42 ##Take out the extra space from IRN returned from db.
43 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'`
44 33
45 ## if database is empty, it will put IRN 000000 34 ## if database is empty, it will put IRN 000000
46 if [ "$IRN" == "" ]; then 35 if [ "$IRN" == "" ]; then
47 echo "no IRN set,using 000000" 36 echo "no IRN set,using 000000"
48 IRN=000000 37 IRN=000000
49 fi 38 fi
50 39
51
52
53 echo "Project is $project"
54 echo "Container is $container" 40 echo "Container is $container"
55 echo "IRN is $IRN" 41 echo "IRN is $IRN"
56 42
57 echo "backup file ${backupFile}" 43 echo "backup file ${backupFile}"
58 echo "preparing to backup ${container} on current project at ${projectDir}:" 44 echo "preparing to backup ${container} on current project at ${projectDir}:"
59 echo " " 45 echo " "
60 46
61 47 echo "starting backup of $container"
62
63 echo "starting backup of $container for $project"
64 docker exec -t $containerName sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile} 48 docker exec -t $containerName sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile}
65 #if [[ $( grep --count "CREATE TABLE" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then 49 #if [[ $( grep --count "CREATE TABLE" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then
66 # echo "ERROR: backup verification FAILED" 50 # echo "ERROR: backup verification FAILED"
67 # echo "Error: empty database" 51 # echo "Error: empty database"
68 # echo "ERROR: $(tail ${backupFile})" 52 # echo "ERROR: $(tail ${backupFile})"
70 # exit 1 54 # exit 1
71 #fi 55 #fi
72 56
73 gzip ${backupFile} 57 gzip ${backupFile}
74 58
59 echo "completed backup of $container to ${backupFile}"
75 60
76 echo "completed backup of $container for $project to ${backupFile}"
77