Mercurial > public > ssdt-docker
annotate scripts/backup-container-docker.sh @ 605:46a67e7afd98 release/2.23.0 tip
flow: Created branch 'release/2.23.0'.
author | Marc Davis <marc.davis@mcoecn.org> |
---|---|
date | Wed, 18 Sep 2024 20:38:06 -0400 |
parents | fb25711eead7 |
children |
rev | line source |
---|---|
388
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
1 #!/bin/bash |
584
fb25711eead7
add irn to the ess backup
Marc Davis <marc.davis@mcoecn.org>
parents:
529
diff
changeset
|
2 # Executes a database backup for the specified database container. |
388
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
3 # The current directory is expected to contain a project configured |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
4 # as with SSDT conventions for an application database. |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
5 # |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
6 # When successful, the output file will be in ./backup with the |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
7 # container name and timestamp in the file. The format of the output |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
8 # is a compressed pg_dump (sql) format. |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
9 # |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
10 container=${1?Must provide container name to backup} |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
11 projectDir=${2:-$PWD} |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
12 |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
13 cd $projectDir |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
14 |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
15 source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh" |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
16 |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
17 set -o pipefail |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
18 mkdir -p ./backup |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
19 |
393
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
20 if [[ "$container" = "invdb" && -f ".env/ssdt-inventory-shared.properties" ]]; then |
388
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
21 function prop { |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
22 grep "${1}" .env/ssdt-inventory-shared.properties|cut -d'=' -f2 |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
23 } |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
24 entityId=$(prop 'entityId' | tr -d '"') |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
25 containerName="$entityId-inventory-db" |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
26 IRN=$(docker exec $containerName psql --username=postgres --dbname=$container -t -c 'select districtirn from inventoryconfiguration') |
390
d75786228e6b
INV-197 wire workflows backup into shared scripts
Matt Calmes <calmes@ssdt-ohio.org>
parents:
389
diff
changeset
|
27 ## if database is empty, it will put IRN 000000 |
d75786228e6b
INV-197 wire workflows backup into shared scripts
Matt Calmes <calmes@ssdt-ohio.org>
parents:
389
diff
changeset
|
28 if [ "$IRN" == "" ]; then |
d75786228e6b
INV-197 wire workflows backup into shared scripts
Matt Calmes <calmes@ssdt-ohio.org>
parents:
389
diff
changeset
|
29 echo "no IRN set,using 000000" |
d75786228e6b
INV-197 wire workflows backup into shared scripts
Matt Calmes <calmes@ssdt-ohio.org>
parents:
389
diff
changeset
|
30 IRN=000000 |
d75786228e6b
INV-197 wire workflows backup into shared scripts
Matt Calmes <calmes@ssdt-ohio.org>
parents:
389
diff
changeset
|
31 fi |
389
4e42c10e3c0d
INV-197 update db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
388
diff
changeset
|
32 ##Take out the extra space from IRN returned from db. |
4e42c10e3c0d
INV-197 update db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
388
diff
changeset
|
33 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'` |
395
b3909bd1a2b6
INV-198 fix output of backup-container-docker.sh and create restore-inventory.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
393
diff
changeset
|
34 backupFile=./backup/${IRN}${entityId}-inventory-db.$(date +%Y-%m-%d-%H-%M-%S).backup |
388
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
35 echo "entityId is $entityId" |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
36 echo "container name is $containerName" |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
37 fi |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
38 |
393
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
39 if [[ "$container" = "workflowsdb" && -f ".env/ssdt-workflows-shared.properties" ]]; then |
390
d75786228e6b
INV-197 wire workflows backup into shared scripts
Matt Calmes <calmes@ssdt-ohio.org>
parents:
389
diff
changeset
|
40 function prop { |
d75786228e6b
INV-197 wire workflows backup into shared scripts
Matt Calmes <calmes@ssdt-ohio.org>
parents:
389
diff
changeset
|
41 grep "${1}" .env/ssdt-workflows-shared.properties|cut -d'=' -f2 |
d75786228e6b
INV-197 wire workflows backup into shared scripts
Matt Calmes <calmes@ssdt-ohio.org>
parents:
389
diff
changeset
|
42 } |
d75786228e6b
INV-197 wire workflows backup into shared scripts
Matt Calmes <calmes@ssdt-ohio.org>
parents:
389
diff
changeset
|
43 entityId=$(prop 'entityId' | tr -d '"') |
d75786228e6b
INV-197 wire workflows backup into shared scripts
Matt Calmes <calmes@ssdt-ohio.org>
parents:
389
diff
changeset
|
44 containerName="$entityId-workflows-db" |
395
b3909bd1a2b6
INV-198 fix output of backup-container-docker.sh and create restore-inventory.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
393
diff
changeset
|
45 backupFile=./backup/${entityId}-workflows-db.$(date +%Y-%m-%d-%H-%M-%S).backup |
390
d75786228e6b
INV-197 wire workflows backup into shared scripts
Matt Calmes <calmes@ssdt-ohio.org>
parents:
389
diff
changeset
|
46 echo "entityId is $entityId" |
d75786228e6b
INV-197 wire workflows backup into shared scripts
Matt Calmes <calmes@ssdt-ohio.org>
parents:
389
diff
changeset
|
47 echo "container name is $containerName" |
388
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
48 fi |
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
49 |
521
8ddeb5a36678
add itcm backup conditionals
Marc Davis <marc.davis@mcoecn.org>
parents:
397
diff
changeset
|
50 if [[ "$container" = "itcmdb" && -f ".env/itcm-shared.properties" ]]; then |
8ddeb5a36678
add itcm backup conditionals
Marc Davis <marc.davis@mcoecn.org>
parents:
397
diff
changeset
|
51 function prop { |
8ddeb5a36678
add itcm backup conditionals
Marc Davis <marc.davis@mcoecn.org>
parents:
397
diff
changeset
|
52 grep "${1}" .env/itcm-shared.properties|cut -d'=' -f2 |
8ddeb5a36678
add itcm backup conditionals
Marc Davis <marc.davis@mcoecn.org>
parents:
397
diff
changeset
|
53 } |
8ddeb5a36678
add itcm backup conditionals
Marc Davis <marc.davis@mcoecn.org>
parents:
397
diff
changeset
|
54 entityId=$(prop 'entityId' | tr -d '"') |
8ddeb5a36678
add itcm backup conditionals
Marc Davis <marc.davis@mcoecn.org>
parents:
397
diff
changeset
|
55 containerName="$entityId-itcm-db" |
8ddeb5a36678
add itcm backup conditionals
Marc Davis <marc.davis@mcoecn.org>
parents:
397
diff
changeset
|
56 backupFile=./backup/${entityId}-itcm-db.$(date +%Y-%m-%d-%H-%M-%S).backup |
8ddeb5a36678
add itcm backup conditionals
Marc Davis <marc.davis@mcoecn.org>
parents:
397
diff
changeset
|
57 echo "entityId is $entityId" |
8ddeb5a36678
add itcm backup conditionals
Marc Davis <marc.davis@mcoecn.org>
parents:
397
diff
changeset
|
58 echo "container name is $containerName" |
8ddeb5a36678
add itcm backup conditionals
Marc Davis <marc.davis@mcoecn.org>
parents:
397
diff
changeset
|
59 fi |
8ddeb5a36678
add itcm backup conditionals
Marc Davis <marc.davis@mcoecn.org>
parents:
397
diff
changeset
|
60 |
529
579a61f2037d
add conditionals for ess db when backing up databases.
Marc Davis <marc.davis@mcoecn.org>
parents:
521
diff
changeset
|
61 if [[ "$container" = "essdb" && -f ".env/ess-shared.properties" ]]; then |
579a61f2037d
add conditionals for ess db when backing up databases.
Marc Davis <marc.davis@mcoecn.org>
parents:
521
diff
changeset
|
62 function prop { |
579a61f2037d
add conditionals for ess db when backing up databases.
Marc Davis <marc.davis@mcoecn.org>
parents:
521
diff
changeset
|
63 grep "${1}" .env/ess-shared.properties|cut -d'=' -f2 |
579a61f2037d
add conditionals for ess db when backing up databases.
Marc Davis <marc.davis@mcoecn.org>
parents:
521
diff
changeset
|
64 } |
579a61f2037d
add conditionals for ess db when backing up databases.
Marc Davis <marc.davis@mcoecn.org>
parents:
521
diff
changeset
|
65 entityId=$(prop 'entityId' | tr -d '"') |
579a61f2037d
add conditionals for ess db when backing up databases.
Marc Davis <marc.davis@mcoecn.org>
parents:
521
diff
changeset
|
66 containerName="$entityId-ess-db" |
584
fb25711eead7
add irn to the ess backup
Marc Davis <marc.davis@mcoecn.org>
parents:
529
diff
changeset
|
67 IRN=$(docker exec $containerName psql --username=postgres --dbname=$container -t -c 'select irn from organization') |
fb25711eead7
add irn to the ess backup
Marc Davis <marc.davis@mcoecn.org>
parents:
529
diff
changeset
|
68 ## if database is empty, it will put IRN 000000 |
fb25711eead7
add irn to the ess backup
Marc Davis <marc.davis@mcoecn.org>
parents:
529
diff
changeset
|
69 if [ "$IRN" == "" ]; then |
fb25711eead7
add irn to the ess backup
Marc Davis <marc.davis@mcoecn.org>
parents:
529
diff
changeset
|
70 echo "no IRN set, using 000000" |
fb25711eead7
add irn to the ess backup
Marc Davis <marc.davis@mcoecn.org>
parents:
529
diff
changeset
|
71 IRN=000000 |
fb25711eead7
add irn to the ess backup
Marc Davis <marc.davis@mcoecn.org>
parents:
529
diff
changeset
|
72 fi |
fb25711eead7
add irn to the ess backup
Marc Davis <marc.davis@mcoecn.org>
parents:
529
diff
changeset
|
73 ##Take out the extra space from IRN returned from db. |
fb25711eead7
add irn to the ess backup
Marc Davis <marc.davis@mcoecn.org>
parents:
529
diff
changeset
|
74 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'` |
fb25711eead7
add irn to the ess backup
Marc Davis <marc.davis@mcoecn.org>
parents:
529
diff
changeset
|
75 backupFile=./backup/${IRN}${entityId}-ess-db.$(date +%Y-%m-%d-%H-%M-%S).backup |
529
579a61f2037d
add conditionals for ess db when backing up databases.
Marc Davis <marc.davis@mcoecn.org>
parents:
521
diff
changeset
|
76 echo "entityId is $entityId" |
579a61f2037d
add conditionals for ess db when backing up databases.
Marc Davis <marc.davis@mcoecn.org>
parents:
521
diff
changeset
|
77 echo "container name is $containerName" |
579a61f2037d
add conditionals for ess db when backing up databases.
Marc Davis <marc.davis@mcoecn.org>
parents:
521
diff
changeset
|
78 fi |
579a61f2037d
add conditionals for ess db when backing up databases.
Marc Davis <marc.davis@mcoecn.org>
parents:
521
diff
changeset
|
79 |
393
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
80 if [ ! -z "$containerName" ]; then |
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
81 echo "Container is $container" |
397
886a7dc796b8
INV-197 make display of IRN conditional as workflowdb has no access to this property
Matt Calmes <calmes@ssdt-ohio.org>
parents:
395
diff
changeset
|
82 if [ "$container" = "invdb" ]; then |
886a7dc796b8
INV-197 make display of IRN conditional as workflowdb has no access to this property
Matt Calmes <calmes@ssdt-ohio.org>
parents:
395
diff
changeset
|
83 echo "IRN is $IRN" |
886a7dc796b8
INV-197 make display of IRN conditional as workflowdb has no access to this property
Matt Calmes <calmes@ssdt-ohio.org>
parents:
395
diff
changeset
|
84 fi |
393
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
85 echo "backup file ${backupFile}" |
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
86 echo "preparing to backup ${container} on current project at ${projectDir}:" |
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
87 echo " " |
388
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
88 |
393
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
89 echo "starting backup of $container" |
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
90 docker exec -t $containerName sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile} |
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
91 #if [[ $( grep --count "CREATE TABLE" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then |
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
92 # echo "ERROR: backup verification FAILED" |
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
93 # echo "Error: empty database" |
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
94 # echo "ERROR: $(tail ${backupFile})" |
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
95 # We want the process to continue even if the db is blank |
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
96 # exit 1 |
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
97 #fi |
388
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
98 |
393
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
99 gzip ${backupFile} |
388
23d40b345e1e
INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff
changeset
|
100 |
395
b3909bd1a2b6
INV-198 fix output of backup-container-docker.sh and create restore-inventory.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
393
diff
changeset
|
101 echo "completed backup of $container to ${backupFile}.gz" |
393
56498d910235
INV-197 refine backup-container-docker.sh script
Matt Calmes <calmes@ssdt-ohio.org>
parents:
390
diff
changeset
|
102 fi |