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
|
314
|
30 IRN=$(docker-compose exec -T $container psql --username=postgres --dbname=$container -t -c 'select irn from organization')
|
|
31 ##Trim function in postgres didn't work - so take out the extra space this way.
|
|
32 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'`
|
|
33
|
287
|
34 echo "Project is $project"
|
|
35 echo "Container is $container"
|
|
36 echo "Remote target is $remoteTarget"
|
|
37 echo "Username is $userName"
|
314
|
38 echo "IRN is $IRN"
|
287
|
39
|
314
|
40 backupFile=./backup/${IRN}${project}-${container}.$(date +%Y-%m-%d-%H-%M-%S).backup
|
|
41 backupFile2=./backup/${IRN}${project}-${container}.$(date +%Y-%m-%d-%H-%M-%S).directorycontents.tar.gz
|
287
|
42
|
|
43
|
|
44 if [ "$project" == "" ]; then
|
|
45 echo "no project available"
|
|
46 exit 1
|
|
47 fi
|
|
48
|
|
49 echo "starting backup of $container for $project"
|
|
50 docker-compose exec -T $container sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile}
|
|
51
|
|
52 if [[ $( grep --count "CREATE TABLE" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then
|
|
53 echo "ERROR: backup verification FAILED"
|
|
54 echo "ERROR: $(tail ${backupFile})"
|
|
55 exit 1
|
|
56 fi
|
|
57
|
|
58 gzip ${backupFile}
|
|
59
|
|
60 echo "completed backup of $container for $project to ${backupFile}"
|
|
61
|
|
62 #backup of all files in current directory
|
314
|
63 #tar -czf ${backupFile2} . --exclude=./backup
|
|
64 tar --exclude=./backup -czf ${backupFile2} .
|
287
|
65
|
|
66 echo "completed backup of all files for $project to ${backupFile2}"
|
|
67
|
|
68
|
|
69 #
|
|
70 #
|
|
71 scp ${backupFile}.gz ${backupFile2} $userName@$remoteTarget
|
|
72
|
|
73 echo " "
|
|
74
|
|
75 echo "completed sending ${backupFile}.gz and ${backupFile2} to ${remoteTarget} as user $userName"
|
|
76
|
|
77
|