Mercurial > public > ssdt-docker
annotate scripts/remote-backup.sh @ 452:a927ce7a5864
USASR-5154 try stretch
author | belknapSSDT <zach.belknap@mcoecn.org> |
---|---|
date | Wed, 27 Sep 2023 17:03:11 -0400 |
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 # along with the top level contents of the project directory | |
11 # Environment variables can be used for REMOTE_BACKUP_TARGET and REMOTE_USERNAME | |
12 # | |
13 | |
14 | |
15 container=${1?Must provide container name to backup} | |
16 remoteTarget=${2:-$REMOTE_BACKUP_TARGET} | |
17 userName=${3:-$REMOTE_USERNAME} | |
18 projectDir=${4:-$PWD} | |
19 | |
20 | |
21 cd $projectDir | |
22 | |
23 source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh" | |
24 | |
25 set -o pipefail | |
26 mkdir -p ./backup | |
27 | |
28 project=$(composeGetProject) | |
29 | |
321
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
30 #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
|
31 if [ "$project" == "" ]; then |
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
32 echo "no project available" |
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
33 exit 1 |
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
34 fi |
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
35 |
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
36 |
314 | 37 IRN=$(docker-compose exec -T $container psql --username=postgres --dbname=$container -t -c 'select irn from organization') |
38 ##Trim function in postgres didn't work - so take out the extra space this way. | |
39 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'` | |
40 | |
320
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
41 ## if database is empty, it will put IRN 000000 |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
42 if [ "$IRN" == "" ]; then |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
43 echo "no IRN set, using 000000" |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
44 IRN=000000 |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
45 fi |
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
46 |
287 | 47 echo "Project is $project" |
48 echo "Container is $container" | |
49 echo "Remote target is $remoteTarget" | |
50 echo "Username is $userName" | |
314 | 51 echo "IRN is $IRN" |
287 | 52 |
321
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
53 |
314 | 54 backupFile=./backup/${IRN}${project}-${container}.$(date +%Y-%m-%d-%H-%M-%S).backup |
55 backupFile2=./backup/${IRN}${project}-${container}.$(date +%Y-%m-%d-%H-%M-%S).directorycontents.tar.gz | |
287 | 56 |
321
2cb1093f9aef
making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents:
320
diff
changeset
|
57 |
287 | 58 |
59 echo "starting backup of $container for $project" | |
60 docker-compose exec -T $container sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile} | |
61 | |
62 if [[ $( grep --count "CREATE TABLE" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then | |
63 echo "ERROR: backup verification FAILED" | |
320
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
64 echo "Error: empty database" |
287 | 65 echo "ERROR: $(tail ${backupFile})" |
320
58c49a386a11
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
319
diff
changeset
|
66 # 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:
319
diff
changeset
|
67 # exit 1 |
287 | 68 fi |
69 | |
70 gzip ${backupFile} | |
71 | |
72 echo "completed backup of $container for $project to ${backupFile}" | |
73 | |
74 #backup of all files in current directory | |
314 | 75 #tar -czf ${backupFile2} . --exclude=./backup |
76 tar --exclude=./backup -czf ${backupFile2} . | |
287 | 77 |
78 echo "completed backup of all files for $project to ${backupFile2}" | |
79 | |
319
7c460a921709
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
318
diff
changeset
|
80 #Create remote directory before scp |
287 | 81 |
319
7c460a921709
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
318
diff
changeset
|
82 host=`echo $userName@$remoteTarget | sed -e 's/:.*//'` |
7c460a921709
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
318
diff
changeset
|
83 |
7c460a921709
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
318
diff
changeset
|
84 target=`echo $remoteTarget | sed -e 's/.*://'` |
7c460a921709
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
318
diff
changeset
|
85 |
7c460a921709
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
318
diff
changeset
|
86 ssh $host mkdir -p $target/$project |
7c460a921709
making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents:
318
diff
changeset
|
87 |
322
4285b8a2762e
making changes to remote backup scripts to account for IRN - tweaks
aldrich@ssdt-ohio.org
parents:
321
diff
changeset
|
88 #scp file |
287 | 89 # |
317
146c0c263cf9
adding project to remote backup scripts target
aldrich@ssdt-ohio.org
parents:
314
diff
changeset
|
90 scp ${backupFile}.gz ${backupFile2} $userName@$remoteTarget/$project |
287 | 91 |
92 echo " " | |
93 | |
318
c61524abdfd6
correcting adding project to remote backup scripts target
aldrich@ssdt-ohio.org
parents:
317
diff
changeset
|
94 echo "completed sending ${backupFile}.gz and ${backupFile2} to ${remoteTarget}/${project} as user $userName" |
287 | 95 |
96 |