Skip to content

Commit aea37ae

Browse files
committed
add postgres 17 backup
1 parent 533e900 commit aea37ae

File tree

20 files changed

+553
-9
lines changed

20 files changed

+553
-9
lines changed

.github/workflows/main.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
pg14: ${{ steps.filter.outputs.pg14 }}
2626
pg15: ${{ steps.filter.outputs.pg15 }}
2727
pg16: ${{ steps.filter.outputs.pg16 }}
28+
pg17: ${{ steps.filter.outputs.pg17 }}
2829

2930
steps:
3031
- name: Checkout 📦
@@ -51,6 +52,8 @@ jobs:
5152
- '15/**'
5253
pg16:
5354
- '16/**'
55+
pg17:
56+
- '17/**'
5457
5558
# Job for Docker Build and Push
5659
build:
@@ -74,6 +77,7 @@ jobs:
7477
pg14=${{ needs.changes.outputs.pg14 }}
7578
pg15=${{ needs.changes.outputs.pg15 }}
7679
pg16=${{ needs.changes.outputs.pg16 }}
80+
pg17=${{ needs.changes.outputs.pg17 }}
7781
7882
if [[ $pg9 == true ]]; then
7983
VERSION="9.6"
@@ -89,8 +93,10 @@ jobs:
8993
VERSION="14"
9094
elif [[ $pg15 == true ]]; then
9195
VERSION="15"
92-
elif [[ $pg15 == true ]]; then
96+
elif [[ $pg16 == true ]]; then
9397
VERSION="16"
98+
elif [[ $pg17 == true ]]; then
99+
VERSION="17"
94100
fi
95101
TAGS="${DOCKER_IMAGE}:${VERSION}"
96102
echo ::set-output name=subdir::${VERSION}

17/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM postgres:17-alpine
2+
3+
LABEL maintainer="Just van den Broecke <justb4@gmail.com>"
4+
5+
RUN apk add --no-cache --update gettext python3 py3-click py3-docker-py && mkdir /pgbackup
6+
7+
ENV PGB_SCHEDULE 0 23 * * *
8+
9+
ADD docker/* /pgbackup/
10+
11+
CMD ["/pgbackup/entrypoint.sh"]

17/README.md

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# Docker PG Backup
2+
3+
Inspired by: https://github.com/kartoza/docker-pg-backup
4+
5+
A Docker container that runs automated scheduled PostgreSQL/PostGIS backups for all
6+
PostgreSQL-based Docker Containers in its network that have the Label `"pgbackup.enable=true"`.
7+
8+
It should work with
9+
the following PostgreSQL/PostGIS Docker images:
10+
11+
* [mdillon/postgis](https://hub.docker.com/r/mdillon/postgis/)
12+
* [Kartoza docker postgis](https://github.com/kartoza//docker-postgis)
13+
* [Standard PostgreSQL Docker image](https://hub.docker.com/_/postgres/)
14+
15+
Any other PostgreSQL/PostGIS image may work as long as its Container has the `POSTGRES_` environment
16+
variables set (see below).
17+
18+
By default it will create a backup once per night (at 23:00) in a
19+
nicely ordered directory by container-name/year/month, but you can specify your own schedule.
20+
21+
* Docker hub at: https://registry.hub.docker.com/u/justb4/pgbackup/
22+
* Github at: https://github.com/justb4/docker-pgbackup
23+
24+
## Getting the image
25+
26+
There are various ways to get the image onto your system:
27+
28+
The preferred way (but using most bandwidth for the initial image) is to
29+
get our docker trusted build like this:
30+
31+
32+
```
33+
docker pull justb4/pgbackup:17
34+
35+
```
36+
37+
We highly suggest that you use a tagged image as
38+
latest may change and may not successfully back up your database. Use the same or
39+
greater version of postgis as the database you are backing up.
40+
To build the image yourself:
41+
42+
```
43+
docker build -t justb4/pgbackup .
44+
```
45+
46+
If you do not wish to do a local checkout first then build directly from github.
47+
48+
```
49+
git clone git://github.com/justb4/docker-pgbackup
50+
```
51+
52+
## Environment Variables
53+
54+
* `PGB_SCHEDULE`, crontab schedule line, if not set, defaults to : `0 23 * * *`
55+
56+
## Run Backups
57+
58+
To create a running container do:
59+
60+
```
61+
docker run --name="pgbackup"\
62+
-v backup:/backup -v /var/run/docker.sock:/var/run/docker.sock \
63+
-i -d justb4/pgbackup:17
64+
```
65+
66+
In this example a local dir (`./backup`) is mounted inti which the actual backups will be
67+
stored.
68+
69+
Best is to use docker-compose, below the as used
70+
for testing, with a schedule that backs up once a minute.
71+
72+
73+
```
74+
# Test for pgbackup with sample db
75+
version: "3"
76+
77+
services:
78+
db:
79+
image: mdillon/postgis:17-alpine
80+
container_name: pg_db_17
81+
labels:
82+
- "pgbackup.enable=true"
83+
environment:
84+
- POSTGRES_DB=testdb
85+
- POSTGRES_USER=testuser
86+
- POSTGRES_PASSWORD=testpass
87+
88+
dbbackup:
89+
image: justb4/pgbackup:17
90+
container_name: pg_backup_17
91+
volumes:
92+
- /var/run/docker.sock:/var/run/docker.sock
93+
- ./backup:/backup
94+
environment:
95+
- PGB_SCHEDULE=*/1 * * * *
96+
97+
```
98+
99+
Then run using:
100+
101+
```
102+
docker-compose up -d
103+
```
104+
105+
It is advised to use explicit DB-container-naming, as backups will be stored in
106+
subdirectories (`year/month/<DB-container-name>-ymd-hm.sql.gz`).
107+
108+
## Explicit Backups
109+
110+
You can also run backups (and restores) explicitly, by calling `exec` on the `justb4/pgbackup`
111+
container, assuming `pgbackup` here.
112+
113+
Backup all DBs containers:
114+
115+
```
116+
docker exec -it pgbackup /pgbackup/backup-all.sh
117+
118+
```
119+
120+
Or you can backup a single DB container:
121+
122+
```
123+
docker exec -it pgbackup /pgbackup/backup.sh <DB container-name> <backup file.sql.gz>
124+
125+
126+
# example
127+
docker exec -it pgbackup /pgbackup/backup.sh pgdb /backup/mybackup.sql.gz
128+
129+
```
130+
131+
## List Backups
132+
133+
You can list all backups available in the container:
134+
135+
```
136+
docker exec -it pgbackup /pgbackup/list-backups.sh
137+
138+
```
139+
140+
## Restoring Backups
141+
142+
This Docker Image also provides restore utilities.
143+
144+
You can `bash` into the `justb4/pgbackup` container and run `restore.sh` or other commands
145+
from there. The following steps are needed:
146+
147+
* if not already present copy your backup file, assuming `/backup/mybackup.sql.gz` here, into the `pgbackup` container mounted volume
148+
* figure out the name of your `justb4/pgbackup` container, assuming `pgbackup` here
149+
* figure out the name of your target DB container, assuming `pgdb` here
150+
* `bash` into the container: `docker exec -it pgbackup /bin/bash`
151+
* execute restore: `/pgbackup/restore.sh /backup/mybackup.sql.gz pgdb`
152+
153+
You could also `exec` directly. Below an example:
154+
155+
```
156+
docker exec -it pgbackup /pgbackup/restore.sh pgdb /backup/2018/10/pgdb-181013-1050.sql.gz
157+
158+
```
159+
160+
## Design and diffs with kartoza/pg-backup
161+
162+
Main difference is that `justb4/pgbackup` uses the Docker API to search within its Docker Network for
163+
Containers that have the Label `"pgbackup.enable=true"`. Using Labels in conjunction with the Docker API
164+
is found in many modern Docker-based services, like e.g. Traefik and Kubernetes.
165+
166+
Each Container to be backed up is then further inspected to get the PostgreSQL credentials
167+
needed to connect with PG tools like `psql`. The Container name will be the PG Hostname
168+
(TODO: figure out IP address via Docker API,
169+
such that single backup/restores can be run commandline).
170+
171+
This has the following advantages:
172+
173+
* loose coupling, easy to setup
174+
* one `pgbackup` Container can backup multiple PostgreSQL Containers
175+
* no need to configure `pgbackup` with all PG credentials
176+
177+
Further changes:
178+
179+
* works with multiple Docker images for both PostgreSQL and PostGIS (mdillon and kartoza)
180+
* using smaller `postgres:<version>-alpine` as base image (i.s.o. `kartoza/postgis`)
181+
* schedule via env var `PGB_SCHEDULE`
182+
* dumps in SQL gzip format (more portable among PG versions) but may become option in futre
183+
* includes restore command to restore backup file in a named container
184+
185+
## Credits
186+
187+
* Tim Sutton (tim@kartoza.com) for https://github.com/kartoza/docker-pg-backup - Consulted Oct 2018
188+
* Just van den Broecke (https://justobjects.nl) - this version - 2018

17/build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
docker build -t justb4/pgbackup:17 .

17/docker/backup-all.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
echo "START Backup"
4+
5+
pushd /pgbackup
6+
python3 pgbackup.py --backupdir /backup backup-all
7+
popd
8+
9+
echo "END Backup"

17/docker/backup.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
echo "START Backup single DB"
4+
5+
# stop on errors
6+
set -e
7+
8+
# check that we have right number of arguments
9+
if [[ ! $# -eq 2 ]]
10+
then
11+
echo 'usage:'
12+
echo ' /pgbackup/backup.sh <container_name> <backup-file>'
13+
echo ''
14+
echo 'to get a list of available backups, run:'
15+
echo ' /pgbackup/list-backups.sh'
16+
exit 1
17+
fi
18+
19+
# set the container name variable
20+
CONTAINERNAME=$1
21+
22+
# set the backupfile variable
23+
BACKUPFILE=$2
24+
25+
echo "Backing up ${CONTAINERNAME} to ${BACKUPFILE}"
26+
27+
pushd /pgbackup
28+
python3 pgbackup.py --filepath ${BACKUPFILE} --containername ${CONTAINERNAME} backup
29+
popd
30+
31+
echo "END Backup"

17/docker/cronfile-template

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Schedule supplied via env var
2+
${PGB_SCHEDULE} /pgbackup/backup-all.sh 2>&1
3+
4+
# We need a blank line here for it to be a valid cron file

17/docker/entrypoint.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
PGB_SCHEDULE=${PGB_SCHEDULE:='0 23 * * *'}
4+
5+
pushd /pgbackup
6+
7+
envsubst < cronfile-template > backup-cron
8+
9+
# Now launch cron in then foreground.
10+
echo "Launching /usr/sbin/crond in foregound with schedule:"
11+
cat backup-cron
12+
crontab backup-cron
13+
/usr/sbin/crond -f -S -l 0

17/docker/list-backups.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
echo "listing available backups"
3+
echo "-------------------------"
4+
find /backup -type f -name '*.sql.gz'

0 commit comments

Comments
 (0)