287
|
1 #!/bin/bash
|
|
2 # Executes a database backup for the specified database container.
|
|
3 # The current directory is expected to contain a project configured
|
|
4 # as with SSDT conventions for an application database.
|
|
5 #
|
|
6 # When successful, the output file will be in ./backup with the
|
|
7 # container name and timestamp in the file.
|
|
8 # The file will also be placed on the specified remote target
|
|
9 # The format of the output is a compressed pg_dump (sql) format.
|
|
10 #
|
|
11 #
|
|
12 # This will backup both usasdb and uspsdb,
|
|
13 # along with the top level contents of the project directory
|
|
14 #Environment variables can be used for REMOTE_BACKUP_TARGET and REMOTE_USERNAME
|
|
15
|
|
16 remoteTarget=${1:-$REMOTE_BACKUP_TARGET}
|
|
17 userName=${2:-$REMOTE_USERNAME}
|
|
18 projectDir=${3:-$PWD}
|
|
19
|
|
20
|
|
21 cd $projectDir
|
|
22
|
|
23 source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh"
|
|
24
|
|
25 set -o pipefail
|
|
26 mkdir -p ./backup
|
|
27
|
|
28 container1=usasdb
|
|
29 container2=uspsdb
|
|
30
|
|
31 project=$(composeGetProject)
|
|
32
|
|
33 echo "project is $project"
|
|
34 echo "Containers are $container1 and $container2"
|
|
35 echo "Remote target is $remoteTarget"
|
|
36 echo "Username is $userName"
|
|
37
|
|
38 if [ "$project" == "" ]; then
|
|
39 echo "no project available"
|
|
40 exit 1
|
|
41 fi
|
|
42
|
|
43
|
|
44 backupFile1=./backup/${project}-${container1}.$(date +%Y-%m-%d-%H-%M-%S).backup
|
|
45 backupFile2=./backup/${project}-${container2}.$(date +%Y-%m-%d-%H-%M-%S).backup
|
|
46 backupFile3=./backup/${project}-directory.$(date +%Y-%m-%d-%H-%M-%S).directorycontents.tar.gz
|
|
47
|
|
48 #usasdb
|
|
49 echo "starting backup of $container1 for $project"
|
|
50 docker-compose exec -T $container1 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container1 ; (exit $?) " > ${backupFile1}
|
|
51
|
|
52 if [[ $( grep --count "CREATE TABLE" ${backupFile1} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile1} ) -eq 0 ]]; then
|
|
53 echo "ERROR: backup verification FAILED"
|
|
54 echo "ERROR: $(tail ${backupFile1})"
|
|
55 else
|
|
56 gzip ${backupFile1}
|
|
57
|
|
58 echo "completed backup of $container1 for $project to ${backupFile1}"
|
|
59
|
|
60 fi
|
|
61
|
|
62
|
|
63 #####
|
|
64 #uspsdb is being used
|
|
65
|
|
66 echo "starting backup of $container2 for $project"
|
|
67 docker-compose exec -T $container2 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container2 ; (exit $?) " > ${backupFile2}
|
|
68
|
|
69 if [[ $( grep --count "CREATE TABLE" ${backupFile2} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile2} ) -eq 0 ]]; then
|
|
70 echo "ERROR: backup verification FAILED"
|
|
71 echo "ERROR: $(tail ${backupFile2})"
|
|
72 else
|
|
73 gzip ${backupFile2}
|
|
74
|
|
75 echo "completed backup of $container2 for $project to ${backupFile2}"
|
|
76
|
|
77 fi
|
|
78
|
|
79 #######
|
|
80
|
|
81 #backup of all files in current directory
|
|
82 echo "starting backup of current directory for $project"
|
|
83
|
|
84 tar -czf ${backupFile3} . --exclude=./backup
|
|
85
|
|
86 echo "completed backup of all files for $project to ${backupFile3}"
|
|
87
|
|
88
|
|
89 #
|
|
90 #
|
|
91 scp ${backupFile1}.gz ${backupFile2}.gz ${backupFile3} $userName@$remoteTarget
|
|
92
|
|
93 echo " "
|
|
94
|
|
95 echo "completed sending ${backupFile1}.gz, ${backupFile2}.gz, and ${backupFile3} to ${remoteTarget} as user $userName"
|
|
96
|
|
97
|