mirror of
https://github.com/cookiecutter/cookiecutter-django.git
synced 2025-01-24 08:14:13 +03:00
adds docker database controls
This commit is contained in:
parent
93c65590ee
commit
eb4092c159
11
{{cookiecutter.repo_name}}/compose/postgres/Dockerfile
Normal file
11
{{cookiecutter.repo_name}}/compose/postgres/Dockerfile
Normal file
|
@ -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
|
22
{{cookiecutter.repo_name}}/compose/postgres/backup.sh
Normal file
22
{{cookiecutter.repo_name}}/compose/postgres/backup.sh
Normal file
|
@ -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"
|
|
@ -0,0 +1,4 @@
|
||||||
|
#!/bin/bash
|
||||||
|
echo "listing available backups"
|
||||||
|
echo "-------------------------"
|
||||||
|
ls /backups/
|
56
{{cookiecutter.repo_name}}/compose/postgres/restore.sh
Normal file
56
{{cookiecutter.repo_name}}/compose/postgres/restore.sh
Normal file
|
@ -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 <backup-file>'
|
||||||
|
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
|
|
@ -1,11 +1,12 @@
|
||||||
version: '2'
|
version: '2'
|
||||||
services:
|
services:
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:9.5
|
build: ./compose/postgres
|
||||||
volumes:
|
volumes:
|
||||||
# If you are using boot2docker, postgres data has to live in the VM for now until #581 is fixed
|
# 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
|
# 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:/var/lib/postgresql/data
|
||||||
|
- /data/dev/{{cookiecutter.repo_name}}/postgres-backups:/backups
|
||||||
environment:
|
environment:
|
||||||
- POSTGRES_USER={{cookiecutter.repo_name}}
|
- POSTGRES_USER={{cookiecutter.repo_name}}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ services:
|
||||||
image: postgres:9.5
|
image: postgres:9.5
|
||||||
volumes:
|
volumes:
|
||||||
- /data/{{cookiecutter.repo_name}}/postgres:/var/lib/postgresql/data
|
- /data/{{cookiecutter.repo_name}}/postgres:/var/lib/postgresql/data
|
||||||
|
- /data/{{cookiecutter.repo_name}}/postgres-backups:/backups
|
||||||
env_file: .env
|
env_file: .env
|
||||||
|
|
||||||
django:
|
django:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user