annotate scripts/remote-backup-all.sh @ 287:4cc087cde1d0

backup script additions and changes
author aldrich@nwoca.org
date Tue, 11 Dec 2018 16:17:25 +0000
parents
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 #
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
11 #
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
12 # This will backup both usasdb and uspsdb,
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
13 # along with the top level contents of the project directory
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
14 #Environment variables can be used for REMOTE_BACKUP_TARGET and REMOTE_USERNAME
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
15
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
16 remoteTarget=${1:-$REMOTE_BACKUP_TARGET}
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
17 userName=${2:-$REMOTE_USERNAME}
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
18 projectDir=${3:-$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 container1=usasdb
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
29 container2=uspsdb
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
30
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
31 project=$(composeGetProject)
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
32
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
33 echo "project is $project"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
34 echo "Containers are $container1 and $container2"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
35 echo "Remote target is $remoteTarget"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
36 echo "Username is $userName"
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 if [ "$project" == "" ]; then
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
39 echo "no project available"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
40 exit 1
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
41 fi
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 backupFile1=./backup/${project}-${container1}.$(date +%Y-%m-%d-%H-%M-%S).backup
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
45 backupFile2=./backup/${project}-${container2}.$(date +%Y-%m-%d-%H-%M-%S).backup
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
46 backupFile3=./backup/${project}-directory.$(date +%Y-%m-%d-%H-%M-%S).directorycontents.tar.gz
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
47
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
48 #usasdb
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
49 echo "starting backup of $container1 for $project"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
50 docker-compose exec -T $container1 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container1 ; (exit $?) " > ${backupFile1}
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" ${backupFile1} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile1} ) -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 ${backupFile1})"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
55 else
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
56 gzip ${backupFile1}
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 echo "completed backup of $container1 for $project to ${backupFile1}"
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 fi
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 #uspsdb is being used
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 "starting backup of $container2 for $project"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
67 docker-compose exec -T $container2 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container2 ; (exit $?) " > ${backupFile2}
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 if [[ $( grep --count "CREATE TABLE" ${backupFile2} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile2} ) -eq 0 ]]; then
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
70 echo "ERROR: backup verification FAILED"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
71 echo "ERROR: $(tail ${backupFile2})"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
72 else
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
73 gzip ${backupFile2}
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 backup of $container2 for $project to ${backupFile2}"
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 fi
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
78
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
79 #######
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
80
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
81 #backup of all files in current directory
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
82 echo "starting backup of current directory for $project"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
83
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
84 tar -czf ${backupFile3} . --exclude=./backup
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
85
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
86 echo "completed backup of all files for $project to ${backupFile3}"
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
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
89 #
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
90 #
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
91 scp ${backupFile1}.gz ${backupFile2}.gz ${backupFile3} $userName@$remoteTarget
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
92
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
93 echo " "
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
94
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
95 echo "completed sending ${backupFile1}.gz, ${backupFile2}.gz, and ${backupFile3} to ${remoteTarget} as user $userName"
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
96
4cc087cde1d0 backup script additions and changes
aldrich@nwoca.org
parents:
diff changeset
97