comparison scripts/remote-backup.sh @ 287:4cc087cde1d0

backup script additions and changes
author aldrich@nwoca.org
date Tue, 11 Dec 2018 16:17:25 +0000
parents
children d78b45c28205
comparison
equal deleted inserted replaced
286:477609392d1b 287:4cc087cde1d0
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 # along with the top level contents of the project directory
11 # Environment variables can be used for REMOTE_BACKUP_TARGET and REMOTE_USERNAME
12 #
13
14
15 container=${1?Must provide container name to backup}
16 remoteTarget=${2:-$REMOTE_BACKUP_TARGET}
17 userName=${3:-$REMOTE_USERNAME}
18 projectDir=${4:-$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 project=$(composeGetProject)
29
30 echo "Project is $project"
31 echo "Container is $container"
32 echo "Remote target is $remoteTarget"
33 echo "Username is $userName"
34
35 backupFile=./backup/${project}-${container}.$(date +%Y-%m-%d-%H-%M-%S).backup
36 backupFile2=./backup/${project}-${container}.$(date +%Y-%m-%d-%H-%M-%S).directorycontents.tar.gz
37
38
39 if [ "$project" == "" ]; then
40 echo "no project available"
41 exit 1
42 fi
43
44 echo "starting backup of $container for $project"
45 docker-compose exec -T $container sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile}
46
47 if [[ $( grep --count "CREATE TABLE" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then
48 echo "ERROR: backup verification FAILED"
49 echo "ERROR: $(tail ${backupFile})"
50 exit 1
51 fi
52
53 gzip ${backupFile}
54
55 echo "completed backup of $container for $project to ${backupFile}"
56
57 #backup of all files in current directory
58 tar -czf ${backupFile2} . --exclude=./backup
59
60 echo "completed backup of all files for $project to ${backupFile2}"
61
62
63 #
64 #
65 scp ${backupFile}.gz ${backupFile2} $userName@$remoteTarget
66
67 echo " "
68
69 echo "completed sending ${backupFile}.gz and ${backupFile2} to ${remoteTarget} as user $userName"
70
71