Mercurial > public > ssdt-docker
annotate scripts/backup-container.sh @ 481:0701c3e10254
USASR-5154 replace apt with yum
author | belknapSSDT <zach.belknap@mcoecn.org> |
---|---|
date | Thu, 28 Sep 2023 16:21:52 -0400 |
parents | 2cb1093f9aef |
children |
rev | line source |
---|---|
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 | |
321
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
20 #If the project is empty, we want to stop the process because this is being run from the wrong directory |
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
21 |
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
22 if [ "$project" == "" ]; then |
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
23 echo "no project available" |
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
24 exit 1 |
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
25 fi |
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
26 |
159 | 27 set -o pipefail |
28 mkdir -p ./backup | |
314 | 29 ## |
320
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
314
diff
changeset
|
30 ##This one puts a space before the IRN |
314 | 31 IRN=$(docker-compose exec -T $container psql --username=postgres --dbname=$container -t -c 'select irn from organization') |
32 ##Trim function in postgres didn't work - so take out the extra space this way. | |
33 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'` | |
159 | 34 |
320
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
314
diff
changeset
|
35 ## if database is empty, it will put IRN 000000 |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
314
diff
changeset
|
36 if [ "$IRN" == "" ]; then |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
314
diff
changeset
|
37 echo "no IRN set,using 000000" |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
314
diff
changeset
|
38 IRN=000000 |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
314
diff
changeset
|
39 fi |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
314
diff
changeset
|
40 |
314 | 41 backupFile=./backup/${IRN}${project}-${container}.$(date +%Y-%m-%d-%H-%M-%S).backup |
320
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
314
diff
changeset
|
42 |
321
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
43 echo "Project is $project" |
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
44 echo "Container is $container" |
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
45 echo "IRN is $IRN" |
314 | 46 |
47 echo "backup file ${backupFile}" | |
274 | 48 echo "preparing to backup ${container} on current project at ${projectDir}:" |
49 echo " " | |
50 | |
159 | 51 |
52 | |
53 echo "starting backup of $container for $project" | |
188 | 54 docker-compose exec -T $container sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile} |
159 | 55 |
56 if [[ $( grep --count "CREATE TABLE" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then | |
57 echo "ERROR: backup verification FAILED" | |
320
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
314
diff
changeset
|
58 echo "Error: empty database" |
267 | 59 echo "ERROR: $(tail ${backupFile})" |
320
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
314
diff
changeset
|
60 # We want the process to continue even if the db is blank |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
314
diff
changeset
|
61 # exit 1 |
159 | 62 fi |
63 | |
64 gzip ${backupFile} | |
65 | |
66 | |
67 echo "completed backup of $container for $project to ${backupFile}" | |
68 |