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
|
|
18 set -o pipefail
|
|
19 mkdir -p ./backup
|
|
20 backupFile=./backup/${container}.$(date +%Y-%m-%d-%H-%M-%S).backup
|
|
21
|
267
|
22 project=$(composeGetProject)
|
159
|
23
|
|
24 if [ "$project" == "" ]; then
|
|
25 echo "no project available"
|
|
26 exit 1
|
|
27 fi
|
|
28
|
|
29 echo "starting backup of $container for $project"
|
188
|
30 docker-compose exec -T $container sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile}
|
159
|
31
|
|
32 if [[ $( grep --count "CREATE TABLE" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then
|
|
33 echo "ERROR: backup verification FAILED"
|
267
|
34 echo "ERROR: $(tail ${backupFile})"
|
159
|
35 exit 1
|
|
36 fi
|
|
37
|
|
38 gzip ${backupFile}
|
|
39
|
|
40
|
|
41 echo "completed backup of $container for $project to ${backupFile}"
|
|
42
|