Mercurial > public > ssdt-docker
changeset 287:4cc087cde1d0
backup script additions and changes
author | aldrich@nwoca.org |
---|---|
date | Tue, 11 Dec 2018 16:17:25 +0000 |
parents | 477609392d1b |
children | ad3790b4e819 |
files | scripts/backup-container.sh scripts/remote-backup-all.sh scripts/remote-backup.sh |
diffstat | 3 files changed, 172 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/backup-container.sh Thu Dec 06 17:23:30 2018 +0000 +++ b/scripts/backup-container.sh Tue Dec 11 16:17:25 2018 +0000 @@ -15,14 +15,16 @@ source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh" +project=$(composeGetProject) + set -o pipefail mkdir -p ./backup -backupFile=./backup/${container}.$(date +%Y-%m-%d-%H-%M-%S).backup +backupFile=./backup/${project}-${container}.$(date +%Y-%m-%d-%H-%M-%S).backup echo "preparing to backup ${container} on current project at ${projectDir}:" echo " " -project=$(composeGetProject) + if [ "$project" == "" ]; then echo "no project available"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/remote-backup-all.sh Tue Dec 11 16:17:25 2018 +0000 @@ -0,0 +1,97 @@ +#!/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. +# +# +# This will backup both usasdb and uspsdb, +# along with the top level contents of the project directory +#Environment variables can be used for REMOTE_BACKUP_TARGET and REMOTE_USERNAME + +remoteTarget=${1:-$REMOTE_BACKUP_TARGET} +userName=${2:-$REMOTE_USERNAME} +projectDir=${3:-$PWD} + + +cd $projectDir + +source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh" + +set -o pipefail +mkdir -p ./backup + +container1=usasdb +container2=uspsdb + +project=$(composeGetProject) + +echo "project is $project" +echo "Containers are $container1 and $container2" +echo "Remote target is $remoteTarget" +echo "Username is $userName" + +if [ "$project" == "" ]; then + echo "no project available" + exit 1 +fi + + +backupFile1=./backup/${project}-${container1}.$(date +%Y-%m-%d-%H-%M-%S).backup +backupFile2=./backup/${project}-${container2}.$(date +%Y-%m-%d-%H-%M-%S).backup +backupFile3=./backup/${project}-directory.$(date +%Y-%m-%d-%H-%M-%S).directorycontents.tar.gz + +#usasdb +echo "starting backup of $container1 for $project" +docker-compose exec -T $container1 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container1 ; (exit $?) " > ${backupFile1} + + if [[ $( grep --count "CREATE TABLE" ${backupFile1} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile1} ) -eq 0 ]]; then + echo "ERROR: backup verification FAILED" + echo "ERROR: $(tail ${backupFile1})" + else + gzip ${backupFile1} + + echo "completed backup of $container1 for $project to ${backupFile1}" + + fi + + +##### +#uspsdb is being used + +echo "starting backup of $container2 for $project" +docker-compose exec -T $container2 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container2 ; (exit $?) " > ${backupFile2} + + if [[ $( grep --count "CREATE TABLE" ${backupFile2} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile2} ) -eq 0 ]]; then + echo "ERROR: backup verification FAILED" + echo "ERROR: $(tail ${backupFile2})" + else + gzip ${backupFile2} + + echo "completed backup of $container2 for $project to ${backupFile2}" + + fi + +####### + +#backup of all files in current directory +echo "starting backup of current directory for $project" + +tar -czf ${backupFile3} . --exclude=./backup + +echo "completed backup of all files for $project to ${backupFile3}" + + +# +# +scp ${backupFile1}.gz ${backupFile2}.gz ${backupFile3} $userName@$remoteTarget + +echo " " + +echo "completed sending ${backupFile1}.gz, ${backupFile2}.gz, and ${backupFile3} to ${remoteTarget} as user $userName" + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/remote-backup.sh Tue Dec 11 16:17:25 2018 +0000 @@ -0,0 +1,71 @@ +#!/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" + +