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