annotate scripts/remote-backup.sh @ 320:58c49a386a11

making changes to remote backup scripts to create directory
author aldrich@ssdt-ohio.org
date Thu, 31 Oct 2019 14:34:53 +0100
parents 7c460a921709
children 2cb1093f9aef
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
320
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 319
diff changeset
34 ## 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
35 if [ "$IRN" == "" ]; then
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 319
diff changeset
36 echo "no IRN set, using 000000"
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 319
diff changeset
37 IRN=000000
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 319
diff changeset
38 fi
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 319
diff changeset
39
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
40 echo "Project is $project"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
41 echo "Container is $container"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
42 echo "Remote target is $remoteTarget"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
43 echo "Username is $userName"
314
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
44 echo "IRN is $IRN"
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
45
314
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
46 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
47 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
48
320
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 319
diff changeset
49 #If the project is empty, we want to stop the process because this is being run from the wrong directory
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
50 if [ "$project" == "" ]; then
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
51 echo "no project available"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
52 exit 1
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
53 fi
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 "starting backup of $container for $project"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
56 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
57
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
58 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
59 echo "ERROR: backup verification FAILED"
320
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 319
diff changeset
60 echo "Error: empty database"
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
61 echo "ERROR: $(tail ${backupFile})"
320
58c49a386a11 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 319
diff changeset
62 # 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
63 # exit 1
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
64 fi
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 gzip ${backupFile}
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 echo "completed backup of $container for $project to ${backupFile}"
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 #backup of all files in current directory
314
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
71 #tar -czf ${backupFile2} . --exclude=./backup
d78b45c28205 backup script adding IRN to name
aldrich@ssdt-ohio.org
parents: 287
diff changeset
72 tar --exclude=./backup -czf ${backupFile2} .
287
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 echo "completed backup of all files for $project to ${backupFile2}"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
75
319
7c460a921709 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 318
diff changeset
76 #Create remote directory before scp
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
77
319
7c460a921709 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 318
diff changeset
78 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
79
7c460a921709 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 318
diff changeset
80 target=`echo $remoteTarget | sed -e 's/.*://'`
7c460a921709 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 318
diff changeset
81
7c460a921709 making changes to remote backup scripts to create directory
aldrich@ssdt-ohio.org
parents: 318
diff changeset
82 ssh $host mkdir -p $target/$project
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 #Create remote directory
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
85 #
317
146c0c263cf9 adding project to remote backup scripts target
aldrich@ssdt-ohio.org
parents: 314
diff changeset
86 scp ${backupFile}.gz ${backupFile2} $userName@$remoteTarget/$project
287
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
87
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
88 echo " "
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
89
318
c61524abdfd6 correcting adding project to remote backup scripts target
aldrich@ssdt-ohio.org
parents: 317
diff changeset
90 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
91
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
92