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
|
|
32 if [ "$project" == "" ]; then
|
|
33 echo "no project available"
|
|
34 exit 1
|
|
35 fi
|
|
36
|
|
37
|
314
|
38 IRN=$(docker-compose exec -T $container1 psql --username=postgres --dbname=$container1 -t -c 'select irn from organization')
|
|
39
|
|
40 if ["$IRN" == ""]; then
|
|
41 IRN=$(docker-compose exec -T $container2 psql --username=postgres --dbname=$container2 -t -c 'select irn from organization')
|
|
42 fi
|
|
43
|
|
44 ##Trim function in postgres didn't work - so take out the extra space this way.
|
|
45 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'`
|
|
46
|
|
47 echo "project is $project"
|
|
48 echo "Containers are $container1 and $container2"
|
|
49 echo "Remote target is $remoteTarget"
|
|
50 echo "Username is $userName"
|
|
51 echo "IRN is $IRN"
|
|
52
|
|
53
|
|
54 backupFile1=./backup/${IRN}${project}-${container1}.$(date +%Y-%m-%d-%H-%M-%S).backup
|
|
55 backupFile2=./backup/${IRN}${project}-${container2}.$(date +%Y-%m-%d-%H-%M-%S).backup
|
|
56 backupFile3=./backup/${IRN}${project}-directory.$(date +%Y-%m-%d-%H-%M-%S).directorycontents.tar.gz
|
287
|
57
|
|
58 #usasdb
|
|
59 echo "starting backup of $container1 for $project"
|
|
60 docker-compose exec -T $container1 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container1 ; (exit $?) " > ${backupFile1}
|
|
61
|
|
62 if [[ $( grep --count "CREATE TABLE" ${backupFile1} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile1} ) -eq 0 ]]; then
|
|
63 echo "ERROR: backup verification FAILED"
|
|
64 echo "ERROR: $(tail ${backupFile1})"
|
|
65 else
|
|
66 gzip ${backupFile1}
|
|
67
|
|
68 echo "completed backup of $container1 for $project to ${backupFile1}"
|
|
69
|
|
70 fi
|
|
71
|
|
72
|
|
73 #####
|
|
74 #uspsdb is being used
|
|
75
|
|
76 echo "starting backup of $container2 for $project"
|
|
77 docker-compose exec -T $container2 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container2 ; (exit $?) " > ${backupFile2}
|
|
78
|
|
79 if [[ $( grep --count "CREATE TABLE" ${backupFile2} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile2} ) -eq 0 ]]; then
|
|
80 echo "ERROR: backup verification FAILED"
|
|
81 echo "ERROR: $(tail ${backupFile2})"
|
|
82 else
|
|
83 gzip ${backupFile2}
|
|
84
|
|
85 echo "completed backup of $container2 for $project to ${backupFile2}"
|
|
86
|
|
87 fi
|
|
88
|
|
89 #######
|
|
90
|
|
91 #backup of all files in current directory
|
|
92 echo "starting backup of current directory for $project"
|
|
93
|
314
|
94 #tar -czf ${backupFile3} . --exclude=./backup
|
|
95 tar --exclude=./backup -czf ${backupFile3} . --exclude=./backup
|
287
|
96
|
|
97 echo "completed backup of all files for $project to ${backupFile3}"
|
|
98
|
|
99
|
|
100 #
|
|
101 #
|
|
102 scp ${backupFile1}.gz ${backupFile2}.gz ${backupFile3} $userName@$remoteTarget
|
|
103
|
|
104 echo " "
|
|
105
|
|
106 echo "completed sending ${backupFile1}.gz, ${backupFile2}.gz, and ${backupFile3} to ${remoteTarget} as user $userName"
|
|
107
|
|
108
|