54
|
1 #!/bin/bash
|
159
|
2 # Executes a database backup for the specifed database container.
|
|
3 # The current directory is expected to contaion 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. The format of the output
|
|
8 # is a compressed pg_dump (sql) format.
|
|
9 #
|
|
10 container=${1?Must provide container name to backup}
|
|
11
|
188
|
12 projectDir=${2:-$PWD}
|
|
13
|
|
14 cd $projectDir
|
|
15
|
159
|
16 source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh"
|
|
17
|
287
|
18 project=$(composeGetProject)
|
|
19
|
159
|
20 set -o pipefail
|
|
21 mkdir -p ./backup
|
287
|
22 backupFile=./backup/${project}-${container}.$(date +%Y-%m-%d-%H-%M-%S).backup
|
159
|
23
|
274
|
24 echo "preparing to backup ${container} on current project at ${projectDir}:"
|
|
25 echo " "
|
|
26
|
287
|
27
|
159
|
28
|
|
29 if [ "$project" == "" ]; then
|
|
30 echo "no project available"
|
|
31 exit 1
|
|
32 fi
|
|
33
|
|
34 echo "starting backup of $container for $project"
|
188
|
35 docker-compose exec -T $container sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile}
|
159
|
36
|
|
37 if [[ $( grep --count "CREATE TABLE" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then
|
|
38 echo "ERROR: backup verification FAILED"
|
267
|
39 echo "ERROR: $(tail ${backupFile})"
|
159
|
40 exit 1
|
|
41 fi
|
|
42
|
|
43 gzip ${backupFile}
|
|
44
|
|
45
|
|
46 echo "completed backup of $container for $project to ${backupFile}"
|
|
47
|