Skip to content

Commit 21ce3fd

Browse files
committed
Initial commit
0 parents  commit 21ce3fd

File tree

7 files changed

+217
-0
lines changed

7 files changed

+217
-0
lines changed

.github/workflows/build.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Build
2+
on:
3+
push:
4+
branches:
5+
- 'master'
6+
7+
jobs:
8+
build:
9+
name: Build
10+
runs-on: ubuntu-latest
11+
env:
12+
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
13+
DOCKER_REGISTRY: ghcr.io
14+
DOCKER_IMAGE_NAME: ${{ github.repository }}
15+
steps:
16+
- name: Check out the repo
17+
uses: actions/checkout@v2
18+
19+
- name: Set up Docker Buildx
20+
uses: docker/setup-buildx-action@v1
21+
22+
- name: Log in to the registry
23+
uses: docker/login-action@v1
24+
with:
25+
registry: ${{ env.DOCKER_REGISTRY }}
26+
username: ${{ github.actor }}
27+
password: ${{ secrets.GITHUB_TOKEN }}
28+
29+
- name: Image tags & labels
30+
id: meta
31+
uses: docker/metadata-action@v3
32+
with:
33+
images: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }}
34+
35+
- name: Image build & push
36+
uses: docker/build-push-action@v2
37+
with:
38+
context: .
39+
file: Dockerfile
40+
push: true
41+
cache-from: type=gha
42+
cache-to: type=gha,mode=max
43+
tags: ${{ steps.meta.outputs.tags }}
44+
labels: ${{ steps.meta.outputs.labels }}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
.env
3+
.vscode

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM alpine:3.13
2+
3+
RUN apk update \
4+
&& apk --no-cache add dumb-init postgresql-client curl aws-cli
5+
6+
RUN curl -L https://github.com/odise/go-cron/releases/download/v0.0.7/go-cron-linux.gz | zcat > /usr/local/bin/go-cron && chmod +x /usr/local/bin/go-cron
7+
8+
COPY entrypoint.sh .
9+
COPY backup.sh .
10+
11+
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
12+
CMD ["sh", "entrypoint.sh"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Johannes Schickling
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# postgres-s3-backup
2+
3+
Yet another variation, supporting DO Spaces, hearbeats, and freshness checks.
4+
5+
## Usage
6+
7+
```yml
8+
backuper:
9+
image: ghcr.io/dipdup-net/postgres-s3-backup:master
10+
environment:
11+
- S3_ENDPOINT=${S3_ENDPOINT}
12+
- S3_ACCESS_KEY_ID=${S3_ACCESS_KEY_ID}
13+
- S3_SECRET_ACCESS_KEY=${S3_SECRET_ACCESS_KEY}
14+
- S3_BUCKET=${S3_BUCKET}
15+
- S3_PATH=${S3_PATH}
16+
- S3_FILENAME=${S3_FILENAME}
17+
- POSTGRES_USER=${POSTGRES_USER:-dipdup}
18+
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-changeme}
19+
- POSTGRES_DB=${POSTGRES_DB:-dipdup}
20+
- POSTGRES_HOST=${POSTGRES_HOST:-db}
21+
- POSTGRES_EXTRA_OPTS=${POSTGRES_EXTRA_OPTS}
22+
- HEARTBEAT_URI=${HEARTBEAT_URI}
23+
- SCHEDULE=${SCHEDULE}
24+
```
25+
26+
### Digital Ocean
27+
28+
`S3_ENDPOINT` is your space endpoint (not space address)
29+
`S3_BUCKET` is space name
30+
31+
### Schedule
32+
33+
Use CRON expression [generator](https://crontab.guru/).
34+
If `SCHEDULE` is empty, a one-time snapshot will be made.
35+
36+
### Heartbeat
37+
38+
`HEARTBEAT_URI` is optional

backup.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#! /bin/sh
2+
3+
set -eo pipefail
4+
set -o pipefail
5+
6+
if [ -z "${POSTGRES_HOST}" ]; then
7+
POSTGRES_HOST="db"
8+
fi
9+
10+
if [ -z "$POSTGRES_PORT" ]; then
11+
POSTGRES_PORT="5432"
12+
fi
13+
14+
if [ -z "${S3_ENDPOINT}" ]; then
15+
AWS_ARGS=""
16+
else
17+
AWS_ARGS="--endpoint-url ${S3_ENDPOINT}"
18+
fi
19+
20+
# env vars needed for aws tools
21+
export AWS_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID
22+
export AWS_SECRET_ACCESS_KEY=$S3_SECRET_ACCESS_KEY
23+
export AWS_DEFAULT_REGION=$S3_REGION
24+
25+
# env vars needed for pg_dump
26+
export PGPASSWORD=$POSTGRES_PASSWORD
27+
POSTGRES_HOST_OPTS="-h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER $POSTGRES_EXTRA_OPTS"
28+
29+
# TODO: check if database is fresh
30+
31+
echo "Snapshotting $POSTGRES_DB database"
32+
pg_dump -Fc $POSTGRES_HOST_OPTS $POSTGRES_DB > dump.backup
33+
34+
echo "Rotating old snapshot"
35+
aws $AWS_ARGS s3 cp s3://$S3_BUCKET/$S3_PATH/$S3_FILENAME.backup s3://$S3_BUCKET/$S3_PATH/$S3_FILENAME.old.backup --acl public-read || true
36+
37+
echo "Uploading fresh snapshot to $S3_BUCKET/$S3_PATH/$S3_FILENAME"
38+
cat dump.backup | aws $AWS_ARGS s3 cp - s3://$S3_BUCKET/$S3_PATH/$S3_FILENAME.backup --acl public-read || exit 2
39+
40+
echo "Snapshot uploaded successfully, removing local file"
41+
rm dump.backup
42+
43+
if [ ! -z "$HEARTBEAT_URI" ]; then
44+
echo "Sending heartbeat signal"
45+
curl -m 10 --retry 5 $HEARTBEAT_URI
46+
fi

entrypoint.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#! /bin/sh
2+
3+
set -eo pipefail
4+
5+
if [ -z "${POSTGRES_USER}" ]; then
6+
echo "Please set POSTGRES_USER"
7+
exit 1
8+
fi
9+
10+
if [ -z "${POSTGRES_PASSWORD}" ]; then
11+
echo "Please set POSTGRES_PASSWORD"
12+
exit 1
13+
fi
14+
15+
if [ -z "${POSTGRES_DB}" ]; then
16+
echo "Please set POSTGRES_DB"
17+
exit 1
18+
fi
19+
20+
if [ -z "${S3_ACCESS_KEY_ID}" ]; then
21+
echo "Please set S3_ACCESS_KEY_ID"
22+
exit 1
23+
fi
24+
25+
if [ -z "${S3_SECRET_ACCESS_KEY}" ]; then
26+
echo "Please set S3_SECRET_ACCESS_KEY"
27+
exit 1
28+
fi
29+
30+
if [ -z "${S3_BUCKET}" ]; then
31+
echo "Please set S3_BUCKET"
32+
exit 1
33+
fi
34+
35+
if [ -z "${S3_PATH}" ]; then
36+
echo "Please set S3_PATH"
37+
exit 1
38+
fi
39+
40+
if [ -z "${S3_FILENAME}" ]; then
41+
echo "Please set S3_FILENAME"
42+
exit 1
43+
fi
44+
45+
if [ "${S3_S3V4}" = "yes" ]; then
46+
aws configure set default.s3.signature_version s3v4
47+
fi
48+
49+
if [ -z "${SCHEDULE}" ]; then
50+
sh backup.sh
51+
else
52+
exec go-cron -s "$SCHEDULE" -p 1880 -- /bin/sh ./backup.sh
53+
fi

0 commit comments

Comments
 (0)