view scripts/remote-backup.sh @ 293:95d7d70f34a5 production v2.4.0

flow: Merged <release> 'v2.4.0' to <master> ('production').
author smith@nwoca.org
date Thu, 27 Dec 2018 00:06:14 +0000
parents 4cc087cde1d0
children d78b45c28205
line wrap: on
line source
#!/bin/bash
# Executes a database backup for the specified database container.
# The current directory is expected to contain a project configured
# as with SSDT conventions for an application database.
#
# When successful, the output file will be in ./backup with the
# container name and timestamp in the file.
# The file will also be placed on the specified remote target
# The format of the output is a compressed pg_dump (sql) format.
# along with the top level contents of the project directory
# Environment variables can be used for REMOTE_BACKUP_TARGET and REMOTE_USERNAME
#


container=${1?Must provide container name to backup}
remoteTarget=${2:-$REMOTE_BACKUP_TARGET}
userName=${3:-$REMOTE_USERNAME}
projectDir=${4:-$PWD}


cd $projectDir

source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh"

set -o pipefail
mkdir -p ./backup

project=$(composeGetProject)

echo "Project is $project"
echo "Container is $container"
echo "Remote target is $remoteTarget"
echo "Username is $userName"

backupFile=./backup/${project}-${container}.$(date +%Y-%m-%d-%H-%M-%S).backup
backupFile2=./backup/${project}-${container}.$(date +%Y-%m-%d-%H-%M-%S).directorycontents.tar.gz


if [ "$project" == "" ]; then
   echo "no project available"
   exit 1
fi

echo "starting backup of $container for $project"
docker-compose exec -T $container sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container ; (exit $?) " > ${backupFile}

if [[ $( grep --count "CREATE TABLE" ${backupFile} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile} ) -eq 0 ]]; then
   echo "ERROR: backup verification FAILED"
   echo "ERROR:  $(tail ${backupFile})"
   exit 1
fi

gzip ${backupFile}

echo "completed backup of $container for $project to ${backupFile}"

#backup of all files in current directory
tar -czf ${backupFile2} . --exclude=./backup

echo "completed backup of all files for $project to ${backupFile2}"


#
#
scp ${backupFile}.gz  ${backupFile2} $userName@$remoteTarget

echo " "

echo "completed sending ${backupFile}.gz and ${backupFile2} to ${remoteTarget} as user $userName"