Mercurial > public > ssdt-docker
annotate scripts/remote-backup-all.sh @ 406:3508fd2d323a production 2.14.0
flow: Merged <release> '2.14.0' to <master> ('production').
author | Jason Klinger <klinger@nwoca.org> |
---|---|
date | Fri, 20 May 2022 20:14:22 +0100 |
parents | 4285b8a2762e |
children |
rev | line source |
---|---|
287 | 1 #!/bin/bash |
2 # Executes a database backup for the specified 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. | |
8 # The file will also be placed on the specified remote target | |
9 # The format of the output is a compressed pg_dump (sql) format. | |
10 # | |
11 # | |
12 # This will backup both usasdb and uspsdb, | |
13 # along with the top level contents of the project directory | |
14 #Environment variables can be used for REMOTE_BACKUP_TARGET and REMOTE_USERNAME | |
15 remoteTarget=${1:-$REMOTE_BACKUP_TARGET} | |
16 userName=${2:-$REMOTE_USERNAME} | |
17 projectDir=${3:-$PWD} | |
18 | |
19 | |
20 cd $projectDir | |
21 | |
22 source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh" | |
23 | |
24 set -o pipefail | |
25 mkdir -p ./backup | |
26 | |
27 container1=usasdb | |
28 container2=uspsdb | |
29 | |
30 project=$(composeGetProject) | |
31 | |
320
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
32 #If the project is empty, we want to stop the process because this is being run from the wrong directory |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
33 |
287 | 34 if [ "$project" == "" ]; then |
35 echo "no project available" | |
36 exit 1 | |
37 fi | |
38 | |
320
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
39 ## first will try to get IRNfrom usasdb; if that is empty it will try to get it from uspsdb |
287 | 40 |
314 | 41 IRN=$(docker-compose exec -T $container1 psql --username=postgres --dbname=$container1 -t -c 'select irn from organization') |
42 | |
318
c61524abdfd6
correcting adding project to remote backup scripts target
aldrich@ssdt-ohio.org
parents:
317
diff
changeset
|
43 if [ "$IRN" == "" ]; then |
314 | 44 IRN=$(docker-compose exec -T $container2 psql --username=postgres --dbname=$container2 -t -c 'select irn from organization') |
45 fi | |
46 | |
47 ##Trim function in postgres didn't work - so take out the extra space this way. | |
48 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'` | |
49 | |
320
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
50 ## if both database are empty, it will put IRN 000000 |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
51 if [ "$IRN" == "" ]; then |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
52 echo "no IRN set, using 000000" |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
53 IRN=000000 |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
54 fi |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
55 |
314 | 56 echo "project is $project" |
57 echo "Containers are $container1 and $container2" | |
58 echo "Remote target is $remoteTarget" | |
59 echo "Username is $userName" | |
60 echo "IRN is $IRN" | |
61 | |
62 | |
63 backupFile1=./backup/${IRN}${project}-${container1}.$(date +%Y-%m-%d-%H-%M-%S).backup | |
64 backupFile2=./backup/${IRN}${project}-${container2}.$(date +%Y-%m-%d-%H-%M-%S).backup | |
65 backupFile3=./backup/${IRN}${project}-directory.$(date +%Y-%m-%d-%H-%M-%S).directorycontents.tar.gz | |
287 | 66 |
67 #usasdb | |
68 echo "starting backup of $container1 for $project" | |
69 docker-compose exec -T $container1 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container1 ; (exit $?) " > ${backupFile1} | |
70 | |
71 if [[ $( grep --count "CREATE TABLE" ${backupFile1} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile1} ) -eq 0 ]]; then | |
72 echo "ERROR: backup verification FAILED" | |
73 echo "ERROR: $(tail ${backupFile1})" | |
320
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
74 echo "Error: empty database" |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
75 #else We still gzip the backup file |
322
4285b8a2762e
making changes to remote backup scripts to account for IRN - tweaks
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
76 fi |
287 | 77 gzip ${backupFile1} |
78 | |
79 echo "completed backup of $container1 for $project to ${backupFile1}" | |
80 | |
81 | |
82 | |
83 ##### | |
322
4285b8a2762e
making changes to remote backup scripts to account for IRN - tweaks
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
84 #uspsdb |
287 | 85 |
86 echo "starting backup of $container2 for $project" | |
87 docker-compose exec -T $container2 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container2 ; (exit $?) " > ${backupFile2} | |
88 | |
89 if [[ $( grep --count "CREATE TABLE" ${backupFile2} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile2} ) -eq 0 ]]; then | |
90 echo "ERROR: backup verification FAILED" | |
91 echo "ERROR: $(tail ${backupFile2})" | |
320
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
92 echo "Error: empty database" |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
93 #else We still gzip the backup file |
322
4285b8a2762e
making changes to remote backup scripts to account for IRN - tweaks
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
94 fi |
287 | 95 gzip ${backupFile2} |
96 | |
97 echo "completed backup of $container2 for $project to ${backupFile2}" | |
98 | |
322
4285b8a2762e
making changes to remote backup scripts to account for IRN - tweaks
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
99 |
287 | 100 |
101 ####### | |
102 | |
103 #backup of all files in current directory | |
104 echo "starting backup of current directory for $project" | |
105 | |
314 | 106 #tar -czf ${backupFile3} . --exclude=./backup |
317
146c0c263cf9
adding project to remote backup scripts target
aldrich@ssdt-ohio.org
parents:
314
diff
changeset
|
107 tar --exclude=./backup -czf ${backupFile3} . |
287 | 108 |
109 echo "completed backup of all files for $project to ${backupFile3}" | |
110 | |
319
7c460a921709
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
318
diff
changeset
|
111 #Create remote directory before scp |
7c460a921709
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
318
diff
changeset
|
112 host=`echo $userName@$remoteTarget | sed -e 's/:.*//'` |
287 | 113 |
319
7c460a921709
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
318
diff
changeset
|
114 target=`echo $remoteTarget | sed -e 's/.*://'` |
7c460a921709
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
318
diff
changeset
|
115 |
322
4285b8a2762e
making changes to remote backup scripts to account for IRN - tweaks
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
116 |
319
7c460a921709
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
318
diff
changeset
|
117 ssh $host mkdir -p $target/$project |
7c460a921709
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
318
diff
changeset
|
118 |
322
4285b8a2762e
making changes to remote backup scripts to account for IRN - tweaks
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
119 #SCP files |
287 | 120 # |
317
146c0c263cf9
adding project to remote backup scripts target
aldrich@ssdt-ohio.org
parents:
314
diff
changeset
|
121 scp ${backupFile1}.gz ${backupFile2}.gz ${backupFile3} $userName@$remoteTarget/$project |
287 | 122 |
123 echo " " | |
124 | |
318
c61524abdfd6
correcting adding project to remote backup scripts target
aldrich@ssdt-ohio.org
parents:
317
diff
changeset
|
125 echo "completed sending ${backupFile1}.gz, ${backupFile2}.gz, and ${backupFile3} to ${remoteTarget}/${project} as user $userName" |
287 | 126 |
127 |