annotate scripts/backup-container-docker.sh @ 388:23d40b345e1e

INV-197 create db backup helper scripts for inventory and workflow
author Matt Calmes <calmes@ssdt-ohio.org>
date Mon, 18 Oct 2021 06:33:33 -0400
parents
children 4e42c10e3c0d
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
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
2 # Executes a database backup for the specifed database container.
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
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
12 projectDir=${2:-$PWD}
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
13
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
14 project=${3?Must provide project name to backup}
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
15
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
16 cd $projectDir
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
17
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
18 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
19
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
20 set -o pipefail
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
21 mkdir -p ./backup
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
22
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
23 if [ "$container" = "invdb" ]; then
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
24 function prop {
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
25 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
26 }
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
27 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
28 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
29 IRN=$(docker exec $containerName psql --username=postgres --dbname=$container -t -c 'select districtirn from inventoryconfiguration')
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
30 backupFile=./backup/${IRN}${entityId}-${container}.$(date +%Y-%m-%d-%H-%M-%S).backup
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
31 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
32 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
33 fi
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
34
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
35 #If the project is empty, we want to stop the process because this is being run from the wrong directory
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
36
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
37 #if [ "$project" == "" ]; then
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
38 # echo "no project available"
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
39 # exit 1
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
40 #fi
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
41 #
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
42 ##Take out the extra space from IRN returned from db.
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
43 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'`
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
44
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
45 ## if database is empty, it will put IRN 000000
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
46 if [ "$IRN" == "" ]; then
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
47 echo "no IRN set,using 000000"
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
48 IRN=000000
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
49 fi
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
50
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
51
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
52
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
53 echo "Project is $project"
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
54 echo "Container is $container"
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
55 echo "IRN is $IRN"
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
56
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
57 echo "backup file ${backupFile}"
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
58 echo "preparing to backup ${container} on current project at ${projectDir}:"
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
59 echo " "
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
60
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
61
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
62
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
63 echo "starting backup of $container for $project"
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
64 docker exec -t $containerName sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile}
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
65 #if [[ $( grep --count "CREATE TABLE" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
66 # echo "ERROR: backup verification FAILED"
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
67 # echo "Error: empty database"
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
68 # echo "ERROR: $(tail ${backupFile})"
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
69 # We want the process to continue even if the db is blank
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
70 # exit 1
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
71 #fi
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
72
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
73 gzip ${backupFile}
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
74
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
75
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
76 echo "completed backup of $container for $project to ${backupFile}"
23d40b345e1e INV-197 create db backup helper scripts for inventory and workflow
Matt Calmes <calmes@ssdt-ohio.org>
parents:
diff changeset
77