From eb4092c159ca61f87119c1fe8ab49bf99153e0e1 Mon Sep 17 00:00:00 2001 From: Jannis Gebauer Date: Tue, 8 Mar 2016 10:41:58 +0100 Subject: [PATCH] adds docker database controls --- .../compose/postgres/Dockerfile | 11 ++++ .../compose/postgres/backup.sh | 22 ++++++++ .../compose/postgres/list-backups.sh | 4 ++ .../compose/postgres/restore.sh | 56 +++++++++++++++++++ {{cookiecutter.repo_name}}/dev.yml | 3 +- {{cookiecutter.repo_name}}/docker-compose.yml | 1 + 6 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 {{cookiecutter.repo_name}}/compose/postgres/Dockerfile create mode 100644 {{cookiecutter.repo_name}}/compose/postgres/backup.sh create mode 100644 {{cookiecutter.repo_name}}/compose/postgres/list-backups.sh create mode 100644 {{cookiecutter.repo_name}}/compose/postgres/restore.sh diff --git a/{{cookiecutter.repo_name}}/compose/postgres/Dockerfile b/{{cookiecutter.repo_name}}/compose/postgres/Dockerfile new file mode 100644 index 00000000..33272364 --- /dev/null +++ b/{{cookiecutter.repo_name}}/compose/postgres/Dockerfile @@ -0,0 +1,11 @@ +FROM postgres:9.5 + +# add backup scripts +ADD backup.sh /usr/local/bin/backup +ADD restore.sh /usr/local/bin/restore +ADD list-backups.sh /usr/local/bin/list-backups + +# make them executable +RUN chmod +x /usr/local/bin/restore +RUN chmod +x /usr/local/bin/list-backups +RUN chmod +x /usr/local/bin/backup diff --git a/{{cookiecutter.repo_name}}/compose/postgres/backup.sh b/{{cookiecutter.repo_name}}/compose/postgres/backup.sh new file mode 100644 index 00000000..97c95e1d --- /dev/null +++ b/{{cookiecutter.repo_name}}/compose/postgres/backup.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# stop on errors +set -e + +# we might run into trouble when using the default `postgres` user, e.g. when dropping the postgres +# database in restore.sh. Check that something else is used here +if [ "$POSTGRES_USER" == "postgres" ] +then + echo "creating a backup as the postgres user is not supported, make sure to set the POSTGRES_USER environment variable" + exit 1 +fi + +# export the postgres password so that subsequent commands don't ask for it +export PGPASSWORD=$POSTGRES_PASSWORD + +echo "creating backup" +echo "---------------" + +FILENAME=backup_$(date +'%Y_%m_%dT%H_%M_%S').sql +pg_dump -h postgres -U $POSTGRES_USER >> /backups/$FILENAME + +echo "successfully created backup $FILENAME" diff --git a/{{cookiecutter.repo_name}}/compose/postgres/list-backups.sh b/{{cookiecutter.repo_name}}/compose/postgres/list-backups.sh new file mode 100644 index 00000000..75972b75 --- /dev/null +++ b/{{cookiecutter.repo_name}}/compose/postgres/list-backups.sh @@ -0,0 +1,4 @@ +#!/bin/bash +echo "listing available backups" +echo "-------------------------" +ls /backups/ diff --git a/{{cookiecutter.repo_name}}/compose/postgres/restore.sh b/{{cookiecutter.repo_name}}/compose/postgres/restore.sh new file mode 100644 index 00000000..75008280 --- /dev/null +++ b/{{cookiecutter.repo_name}}/compose/postgres/restore.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# stop on errors +set -e + +# we might run into trouble when using the default `postgres` user, e.g. when dropping the postgres +# database in restore.sh. Check that something else is used here +if [ "$POSTGRES_USER" == "postgres" ] +then + echo "restoring as the postgres user is not supported, make sure to set the POSTGRES_USER environment variable" + exit 1 +fi + +# export the postgres password so that subsequent commands don't ask for it +export PGPASSWORD=$POSTGRES_PASSWORD + +# check that we have an argument for a filename candidate +if [[ $# -eq 0 ]] ; then + echo 'usage:' + echo ' docker-compose run postgres restore ' + echo '' + echo 'to get a list of available backups, run:' + echo ' docker-compose run postgres list-backups' + exit 1 +fi + +# set the backupfile variable +BACKUPFILE=/backups/$1 + +# check that the file exists +if ! [ -f $BACKUPFILE ]; then + echo "backup file not found" + echo 'to get a list of available backups, run:' + echo ' docker-compose run postgres list-backups' + exit 1 +fi + +echo "beginning restore from $1" +echo "-------------------------" + +# delete the db +# deleting the db can fail. Spit out a comment if this happens but continue since the db +# is created in the next step +echo "deleting old database $POSTGRES_USER" +if dropdb -h postgres -U $POSTGRES_USER $POSTGRES_USER +then echo "deleted $POSTGRES_USER database" +else echo "database $POSTGRES_USER does not exist, continue" +fi + +# create a new database +echo "creating new database $POSTGRES_USER" +createdb -h postgres -U $POSTGRES_USER $POSTGRES_USER -O $POSTGRES_USER + +# restore the database +echo "restoring database $POSTGRES_USER" +psql -h postgres -U $POSTGRES_USER < $BACKUPFILE diff --git a/{{cookiecutter.repo_name}}/dev.yml b/{{cookiecutter.repo_name}}/dev.yml index 4b02e5b6..7672e785 100644 --- a/{{cookiecutter.repo_name}}/dev.yml +++ b/{{cookiecutter.repo_name}}/dev.yml @@ -1,11 +1,12 @@ version: '2' services: postgres: - image: postgres:9.5 + build: ./compose/postgres volumes: # If you are using boot2docker, postgres data has to live in the VM for now until #581 is fixed # for more info see here: https://github.com/boot2docker/boot2docker/issues/581 - /data/dev/{{cookiecutter.repo_name}}/postgres:/var/lib/postgresql/data + - /data/dev/{{cookiecutter.repo_name}}/postgres-backups:/backups environment: - POSTGRES_USER={{cookiecutter.repo_name}} diff --git a/{{cookiecutter.repo_name}}/docker-compose.yml b/{{cookiecutter.repo_name}}/docker-compose.yml index 2a523dd6..20fad0b7 100644 --- a/{{cookiecutter.repo_name}}/docker-compose.yml +++ b/{{cookiecutter.repo_name}}/docker-compose.yml @@ -4,6 +4,7 @@ services: image: postgres:9.5 volumes: - /data/{{cookiecutter.repo_name}}/postgres:/var/lib/postgresql/data + - /data/{{cookiecutter.repo_name}}/postgres-backups:/backups env_file: .env django: