annotate scripts/remote-backup.sh @ 302:15d0cffd9980 production v2.5.0

flow: Merged <release> 'v2.5.0' to <master> ('production').
author smith@nwoca.org
date Wed, 01 May 2019 15:33:00 +0100
parents 4cc087cde1d0
children d78b45c28205
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
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
30 echo "Project is $project"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
31 echo "Container is $container"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
32 echo "Remote target is $remoteTarget"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
33 echo "Username is $userName"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
34
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
35 backupFile=./backup/${project}-${container}.$(date +%Y-%m-%d-%H-%M-%S).backup
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
36 backupFile2=./backup/${project}-${container}.$(date +%Y-%m-%d-%H-%M-%S).directorycontents.tar.gz
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
37
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
38
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
39 if [ "$project" == "" ]; then
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
40 echo "no project available"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
41 exit 1
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
42 fi
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 echo "starting backup of $container for $project"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
45 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
46
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
47 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
48 echo "ERROR: backup verification FAILED"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
49 echo "ERROR: $(tail ${backupFile})"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
50 exit 1
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
51 fi
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
52
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
53 gzip ${backupFile}
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
54
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
55 echo "completed backup of $container for $project to ${backupFile}"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
56
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
57 #backup of all files in current directory
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
58 tar -czf ${backupFile2} . --exclude=./backup
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 all files for $project to ${backupFile2}"
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
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
63 #
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
64 #
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
65 scp ${backupFile}.gz ${backupFile2} $userName@$remoteTarget
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
66
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
67 echo " "
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 echo "completed sending ${backupFile}.gz and ${backupFile2} to ${remoteTarget} as user $userName"
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