annotate scripts/remote-backup-all.sh @ 316:83f30dc7e9fb

adjusting docker script to test notifications
author aldrich@ssdt-ohio.org
date Tue, 29 Oct 2019 16:45:31 +0100
parents d78b45c28205
children 146c0c263cf9
rev   line source
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
1 #!/bin/bash
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
2 # Executes a database backup for the specified database container.
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
3 # The current directory is expected to contain a project configured
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
4 # as with SSDT conventions for an application database.
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
5 #
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
6 # When successful, the output file will be in ./backup with the
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
7 # container name and timestamp in the file.
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
8 # The file will also be placed on the specified remote target
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
9 # The format of the output is a compressed pg_dump (sql) format.
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
10 #
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
11 #
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
12 # This will backup both usasdb and uspsdb,
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
13 # along with the top level contents of the project directory
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
14 #Environment variables can be used for REMOTE_BACKUP_TARGET and REMOTE_USERNAME
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
15 remoteTarget=${1:-$REMOTE_BACKUP_TARGET}
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
16 userName=${2:-$REMOTE_USERNAME}
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
17 projectDir=${3:-$PWD}
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
18
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
19
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
20 cd $projectDir
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
21
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
22 source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
23
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
24 set -o pipefail
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
25 mkdir -p ./backup
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
26
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
27 container1=usasdb
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
28 container2=uspsdb
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
29
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
30 project=$(composeGetProject)
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
31
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
32 if [ "$project" == "" ]; then
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
33 echo "no project available"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
34 exit 1
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
35 fi
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
36
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
37
314
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
38 IRN=$(docker-compose exec -T $container1 psql --username=postgres --dbname=$container1 -t -c 'select irn from organization')
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
39
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
40 if ["$IRN" == ""]; then
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
41 IRN=$(docker-compose exec -T $container2 psql --username=postgres --dbname=$container2 -t -c 'select irn from organization')
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
42 fi
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
43
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
44 ##Trim function in postgres didn't work - so take out the extra space this way.
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
45 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'`
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
46
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
47 echo "project is $project"
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
48 echo "Containers are $container1 and $container2"
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
49 echo "Remote target is $remoteTarget"
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
50 echo "Username is $userName"
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
51 echo "IRN is $IRN"
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
52
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
53
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
54 backupFile1=./backup/${IRN}${project}-${container1}.$(date +%Y-%m-%d-%H-%M-%S).backup
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
55 backupFile2=./backup/${IRN}${project}-${container2}.$(date +%Y-%m-%d-%H-%M-%S).backup
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
56 backupFile3=./backup/${IRN}${project}-directory.$(date +%Y-%m-%d-%H-%M-%S).directorycontents.tar.gz
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
57
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
58 #usasdb
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
59 echo "starting backup of $container1 for $project"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
60 docker-compose exec -T $container1 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container1 ; (exit $?) " > ${backupFile1}
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
61
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
62 if [[ $( grep --count "CREATE TABLE" ${backupFile1} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile1} ) -eq 0 ]]; then
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
63 echo "ERROR: backup verification FAILED"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
64 echo "ERROR: $(tail ${backupFile1})"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
65 else
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
66 gzip ${backupFile1}
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
67
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
68 echo "completed backup of $container1 for $project to ${backupFile1}"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
69
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
70 fi
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
71
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
72
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
73 #####
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
74 #uspsdb is being used
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
75
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
76 echo "starting backup of $container2 for $project"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
77 docker-compose exec -T $container2 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container2 ; (exit $?) " > ${backupFile2}
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
78
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
79 if [[ $( grep --count "CREATE TABLE" ${backupFile2} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile2} ) -eq 0 ]]; then
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
80 echo "ERROR: backup verification FAILED"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
81 echo "ERROR: $(tail ${backupFile2})"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
82 else
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
83 gzip ${backupFile2}
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
84
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
85 echo "completed backup of $container2 for $project to ${backupFile2}"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
86
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
87 fi
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
88
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
89 #######
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
90
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
91 #backup of all files in current directory
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
92 echo "starting backup of current directory for $project"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
93
314
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
94 #tar -czf ${backupFile3} . --exclude=./backup
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
95 tar --exclude=./backup -czf ${backupFile3} . --exclude=./backup
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
96
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
97 echo "completed backup of all files for $project to ${backupFile3}"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
98
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
99
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
100 #
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
101 #
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
102 scp ${backupFile1}.gz ${backupFile2}.gz ${backupFile3} $userName@$remoteTarget
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
103
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
104 echo " "
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
105
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
106 echo "completed sending ${backupFile1}.gz, ${backupFile2}.gz, and ${backupFile3} to ${remoteTarget} as user $userName"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
107
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
108