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
|
274
|
22 echo "preparing to backup ${container} on current project at ${projectDir}:"
|
|
23 echo " "
|
|
24
|
267
|
25 project=$(composeGetProject)
|
159
|
26
|
|
27 if [ "$project" == "" ]; then
|
|
28 echo "no project available"
|
|
29 exit 1
|
|
30 fi
|
|
31
|
|
32 echo "starting backup of $container for $project"
|
188
|
33 docker-compose exec -T $container sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile}
|
159
|
34
|
|
35 if [[ $( grep --count "CREATE TABLE" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then
|
|
36 echo "ERROR: backup verification FAILED"
|
267
|
37 echo "ERROR: $(tail ${backupFile})"
|
159
|
38 exit 1
|
|
39 fi
|
|
40
|
|
41 gzip ${backupFile}
|
|
42
|
|
43
|
|
44 echo "completed backup of $container for $project to ${backupFile}"
|
|
45
|