annotate scripts/remote-backup.sh @ 344:731a1492c8a0

testbench updates
author aldrich@ssdt-ohio.org
date Wed, 20 May 2020 15:48:31 +0100
parents 4285b8a2762e
children
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 # along with the top level contents of the project directory
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
11 # Environment variables can be used for REMOTE_BACKUP_TARGET and REMOTE_USERNAME
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
12 #
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
13
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
14
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
15 container=${1?Must provide container name to backup}
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
16 remoteTarget=${2:-$REMOTE_BACKUP_TARGET}
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
17 userName=${3:-$REMOTE_USERNAME}
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
18 projectDir=${4:-$PWD}
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
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
21 cd $projectDir
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
22
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
23 source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
24
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
25 set -o pipefail
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
26 mkdir -p ./backup
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
27
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
28 project=$(composeGetProject)
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
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
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
37 IRN=$(docker-compose exec -T $container psql --username=postgres --dbname=$container -t -c 'select irn from organization')
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
38 ##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
39 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'`
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
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
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
47 echo "Project is $project"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
48 echo "Container is $container"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
49 echo "Remote target is $remoteTarget"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
50 echo "Username is $userName"
314
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
51 echo "IRN is $IRN"
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
52
321
2cb1093f9aef making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents: 320
diff changeset
53
314
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
54 backupFile=./backup/${IRN}${project}-${container}.$(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}-${container}.$(date +%Y-%m-%d-%H-%M-%S).directorycontents.tar.gz
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
56
321
2cb1093f9aef making changes to remote backup scripts to account for no IRN
aldrich@ssdt-ohio.org
parents: 320
diff changeset
57
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
58
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
59 echo "starting backup of $container for $project"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
60 docker-compose exec -T $container sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile}
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" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
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
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
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
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
68 fi
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 gzip ${backupFile}
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 echo "completed backup of $container for $project to ${backupFile}"
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 #backup of all files in current directory
314
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
75 #tar -czf ${backupFile2} . --exclude=./backup
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
76 tar --exclude=./backup -czf ${backupFile2} .
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
77
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
78 echo "completed backup of all files for $project to ${backupFile2}"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
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
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
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
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
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
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
91
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
92 echo " "
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
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
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
95
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
96