# HG changeset patch # User smith@nwoca.org # Date 1501706692 -3600 # Node ID 303259c08b7dc3701d47d65ddf6ae8f4776c3587 # Parent 4bc3485f84601ba884b7da7042884c0cab07c5e7 DEP-12: update backup scripts diff -r 4bc3485f8460 -r 303259c08b7d scripts/backup-container.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/backup-container.sh Wed Aug 02 21:44:52 2017 +0100 @@ -0,0 +1,38 @@ +#!/bin/bash +# Executes a database backup for the specifed database container. +# The current directory is expected to contaion 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 format of the output +# is a compressed pg_dump (sql) format. +# +container=${1?Must provide container name to backup} + +source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh" + +set -o pipefail +mkdir -p ./backup +backupFile=./backup/${container}.$(date +%Y-%m-%d-%H-%M-%S).backup + +project=$(composeGetProject $container) + +if [ "$project" == "" ]; then + echo "no project available" + exit 1 +fi + +echo "starting backup of $container for $project" +docker-compose exec $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}" + diff -r 4bc3485f8460 -r 303259c08b7d scripts/backup-usas.sh --- a/scripts/backup-usas.sh Mon Jul 31 20:50:14 2017 +0100 +++ b/scripts/backup-usas.sh Wed Aug 02 21:44:52 2017 +0100 @@ -1,6 +1,5 @@ #!/bin/bash - docker-compose run --rm usasdb sh -c \ - "pg_dump -Cc --if-exists --host=usasdb \ - --dbname=\$USASDB_ENV_DB_NAME \ - --username=\$USASDB_ENV_DB_USER | gzip \ - > /backup/\$USASDB_ENV_DB_NAME.$(date +%Y-%m-%d-%H-%M-%S).backup.gz" +source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh" + +${SSDT_SCRIPTS}/backup-container.sh usasdb + diff -r 4bc3485f8460 -r 303259c08b7d scripts/backup-usps.sh --- a/scripts/backup-usps.sh Mon Jul 31 20:50:14 2017 +0100 +++ b/scripts/backup-usps.sh Wed Aug 02 21:44:52 2017 +0100 @@ -1,6 +1,6 @@ #!/bin/bash - docker-compose run --rm uspsdb sh -c \ - "pg_dump -Cc --if-exists --host=uspsdb \ - --dbname=\$USPSDB_ENV_DB_NAME \ - --username=\$USPSDB_ENV_DB_USER | gzip \ - > /backup/\$USPSDB_ENV_DB_NAME.$(date +%Y-%m-%d-%H-%M-%S).backup.gz" + +source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh" + +${SSDT_SCRIPTS}/backup-container.sh uspsdb + diff -r 4bc3485f8460 -r 303259c08b7d scripts/capture.sh --- a/scripts/capture.sh Mon Jul 31 20:50:14 2017 +0100 +++ b/scripts/capture.sh Wed Aug 02 21:44:52 2017 +0100 @@ -1,3 +1,4 @@ #!/bin/bash -# writes tail of current logs to standard out from containers in compose project. -docker-compose logs --no-color $1 \ No newline at end of file +# current logs to standard out from container (all containers by default) in current project +docker-compose logs --no-color $1 +