Skip to content

Commit

Permalink
Backport from 9.6
Browse files Browse the repository at this point in the history
Add ARCHIVE_FILENAME parameters to specify fixed filename (kartoza#11)

* Add ARCHIVE_FILENAME parameters to specify fixed filename
* Update travis.yml for latest changes
* Edit README.md to reflect the changes
* Edit docker-compose.yml for latest update
* Add restore script
  • Loading branch information
lucernae committed Feb 14, 2019
1 parent 39f7a71 commit 4818a94
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 17 deletions.
1 change: 1 addition & 0 deletions .dockerignore
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
backups
docker-compose.override.yml
31 changes: 21 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
install:
- sudo apt-get install -y linux-image-virtual kernel linux-image-extra-virtual
- curl -sSL https://get.docker.com/ | sudo sh
- sudo pip install docker-compose
sudo: required

services:
- docker

env:
- COMPOSE_FILE=docker-compose.yml:docker-compose.override.travis.yml

script:
- ls -lah
- docker-compose up -d dbbackup
- docker-compose up -d --build dbbackup
- docker-compose ps
- docker exec dockerpgbackups_dbbackup_1 ls
- docker exec dockerpgbackups_dbbackup_1 date --set="2012-6-29 10:59.59 PM"
- wait 4
- ls -lah backups/*
- if [[ ! -f "backups/2012/June/foo.dmp" ]]; then exit 1; fi
- docker-compose exec dbbackup /bin/bash -c "until psql -l; do echo 'db unavailable. sleeping'; sleep 1; done"
- docker-compose exec dbbackup ls
# Test backup
- docker-compose exec dbbackup /backups.sh
- export ARCHIVE_DUMP="backups/$(date +%Y)/$(date +%B)/watchkeeper_gis.$(date +%d-%B-%Y).dmp"
- ls -lah ${ARCHIVE_DUMP}
# Test restore
- docker-compose exec dbbackup cp /${ARCHIVE_DUMP} /backups/target_archive.dmp
- docker-compose exec dbbackup psql -l gis
- docker-compose exec dbbackup dropdb gis
- docker-compose exec dbbackup psql -l
- docker-compose exec dbbackup /restore.sh
- docker-compose exec dbbackup psql -l gis

notifications:
irc:
channels:
Expand Down
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
FROM kartoza/postgis:10.0-2.4
MAINTAINER [email protected]

RUN apt-get install -y postgresql-client
RUN apt-get -y update; apt-get install -y postgresql-client
ADD backups-cron /etc/cron.d/backups-cron
RUN touch /var/log/cron.log
ADD backups.sh /backups.sh
ADD restore.sh /restore.sh
ADD start.sh /start.sh


ENTRYPOINT []
CMD ["/start.sh"]
49 changes: 46 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ docker run --name="backups"\
In this example I used a volume into which the actual backups will be
stored.

# Specifying environment
## Specifying environment


You can also use the following environment variables to pass a
Expand All @@ -74,6 +74,7 @@ user name and password etc for the database connection.
* POSTGRES_PORT if not set, defaults to : 5432
* POSTGRES_HOST if not set, defaults to : db
* POSTGRES_DBNAME if not set, defaults to : gis
* ARCHIVE_FILENAME you can use your specified filename format here, default to empty, which means it will use default filename format.

Example usage:

Expand Down Expand Up @@ -106,7 +107,7 @@ db:
- USERNAME=docker
- PASS=docker
dbbackups:
dbbackup:
image: kartoza/pg-backup:9.4
hostname: pg-backups
volumes:
Expand All @@ -127,9 +128,51 @@ dbbackups:
Then run using:

```
docker-compose up -d dbbackups
docker-compose up -d dbbackup
```

## Filename format

The default backup archive generated will be stored in this directory (inside the container):

```
/backups/$(date +%Y)/$(date +%B)/${DUMPPREFIX}_${DB}.$(date +%d-%B-%Y).dmp
```

As a concrete example, with `DUMPPREFIX=PG` and if your postgis has DB name `gis`.
The backup archive would be something like:

```
/backups/2019/February/PG_gis.13-February-2019.dmp
```

If you specify `ARCHIVE_FILENAME` instead (default value is empty). The
filename will be fixed according to this prefix.
Let's assume `ARCHIVE_FILENAME=/backups/latest`
The backup archive would be something like

```
/backups/latest.gis.dmp
```

## Restoring

A simple restore script is provided.
You need to specify some environment variables first:

* TARGET_DB: the db name to restore
* WITH_POSTGIS: Kartoza specific, to generate POSTGIS extension along with the restore process
* TARGET_ARCHIVE: the full path of the archive to restore

The restore script will delete the `TARGET_DB`, so make sure you know what you are doing.
Then it will create a new one and restore the content from `TARGET_ARCHIVE`

If you specify these environment variable using docker-compose.yml file,
then you can execute a restore process like this:

```
docker-compose exec dbbackup /restore.sh
```

## Credits

Expand Down
6 changes: 5 additions & 1 deletion backups.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ DBLIST=`psql -l | awk '{print $1}' | grep -v "+" | grep -v "Name" | grep -v "Lis
for DB in ${DBLIST}
do
echo "Backing up $DB" >> /var/log/cron.log
FILENAME=${MYBACKUPDIR}/${DUMPPREFIX}_${DB}.${MYDATE}.dmp
if [ -z "${ARCHIVE_FILENAME:-}" ]; then
FILENAME=${MYBACKUPDIR}/${DUMPPREFIX}_${DB}.${MYDATE}.dmp
else
FILENAME="${ARCHIVE_FILENAME}.${DB}.dmp"
fi
pg_dump -Fc -f ${FILENAME} -x -O ${DB}
done
12 changes: 12 additions & 0 deletions docker-compose.override.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This is for testing on travis only
# For production we recommend to rather use
# image: kartoza/pg-backup
# in the dbbackup service
dbbackup:
environment:
# Postgres environment variable
# Can be set to execute PG tools directly
- PGHOST=db
- PGUSER=docker
- PGPASSWORD=docker
- PGPORT=5432
7 changes: 6 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@
db:
image: kartoza/postgis
environment:
- ALLOW_IP_RANGE=0.0.0.0/0
- POSTGRES_USER=docker
- POSTGRES_PASS=docker

dbbackup:
build: .
volumes:
- ./backup:/backup
- ./backups:/backups
environment:
- DUMPPREFIX=watchkeeper
- POSTGRES_HOST=db
- POSTGRES_DBNAME=gis
- POSTGRES_USER=docker
- POSTGRES_PASS=docker
- POSTGRES_PORT=5432
# For restore script
- TARGET_DB=gis
- WITH_POSTGIS=1
- TARGET_ARCHIVE=/backups/target_archive.dmp
links:
- db:db
31 changes: 31 additions & 0 deletions restore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

source /pgenv.sh

echo "TARGET_DB: ${TARGET_DB}"
echo "WITH_POSTGIS: ${WITH_POSTGIS}"
echo "TARGET_ARCHIVE: ${TARGET_ARCHIVE}"

if [ -z "${TARGET_ARCHIVE:-}" ] || [ ! -f "${TARGET_ARCHIVE:-}" ]; then
echo "TARGET_ARCHIVE needed."
exit 1
fi

if [ -z "${TARGET_DB:-}" ]; then
echo "TARGET_DB needed."
exit 1
fi

echo "Dropping target DB"
dropdb ${TARGET_DB}

if [ -z "${WITH_POSTGIS:-}" ]; then
echo "Recreate target DB without POSTGIS"
createdb -O ${PGUSER} ${TARGET_DB}
else
echo "Recreate target DB with POSTGIS"
createdb -O ${PGUSER} -T template_postgis ${TARGET_DB}
fi

echo "Restoring dump file"
pg_restore ${TARGET_ARCHIVE} | psql -d ${TARGET_DB}
1 change: 1 addition & 0 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export PGPORT=$POSTGRES_PORT
export PGHOST=$POSTGRES_HOST
export PGDATABASE=$POSTGRES_DBNAME
export DUMPPREFIX=$DUMPPREFIX
export ARCHIVE_FILENAME="${ARCHIVE_FILENAME}"
" > /pgenv.sh

echo "Start script running with these environment options"
Expand Down

0 comments on commit 4818a94

Please sign in to comment.