annotate scripts/remote-backup.sh @ 314:d78b45c28205

backup script adding IRN to name
author aldrich@ssdt-ohio.org
date Mon, 28 Oct 2019 19:43:53 +0100
parents 4cc087cde1d0
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 # 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
314
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
30 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
31 ##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
32 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'`
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
33
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
34 echo "Project is $project"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
35 echo "Container is $container"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
36 echo "Remote target is $remoteTarget"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
37 echo "Username is $userName"
314
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
38 echo "IRN is $IRN"
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
39
314
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
40 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
41 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
42
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
43
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
44 if [ "$project" == "" ]; then
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
45 echo "no project available"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
46 exit 1
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
47 fi
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
48
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
49 echo "starting backup of $container for $project"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
50 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
51
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
52 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
53 echo "ERROR: backup verification FAILED"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
54 echo "ERROR: $(tail ${backupFile})"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
55 exit 1
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
56 fi
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 gzip ${backupFile}
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
59
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
60 echo "completed backup of $container for $project to ${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 #backup of all files in current directory
314
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
63 #tar -czf ${backupFile2} . --exclude=./backup
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
64 tar --exclude=./backup -czf ${backupFile2} .
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
65
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
66 echo "completed backup of all files for $project to ${backupFile2}"
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
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 #
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
71 scp ${backupFile}.gz ${backupFile2} $userName@$remoteTarget
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 echo " "
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
74
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
75 echo "completed sending ${backupFile}.gz and ${backupFile2} to ${remoteTarget} as user $userName"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
76
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
77