Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kus committed Feb 4, 2022
0 parents commit 21ce3fd
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Build
on:
push:
branches:
- 'master'

jobs:
build:
name: Build
runs-on: ubuntu-latest
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
DOCKER_REGISTRY: ghcr.io
DOCKER_IMAGE_NAME: ${{ github.repository }}
steps:
- name: Check out the repo
uses: actions/checkout@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Log in to the registry
uses: docker/login-action@v1
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Image tags & labels
id: meta
uses: docker/metadata-action@v3
with:
images: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }}

- name: Image build & push
uses: docker/build-push-action@v2
with:
context: .
file: Dockerfile
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
.env
.vscode
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM alpine:3.13

RUN apk update \
&& apk --no-cache add dumb-init postgresql-client curl aws-cli

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

COPY entrypoint.sh .
COPY backup.sh .

ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["sh", "entrypoint.sh"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Johannes Schickling

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# postgres-s3-backup

Yet another variation, supporting DO Spaces, hearbeats, and freshness checks.

## Usage

```yml
backuper:
image: ghcr.io/dipdup-net/postgres-s3-backup:master
environment:
- S3_ENDPOINT=${S3_ENDPOINT}
- S3_ACCESS_KEY_ID=${S3_ACCESS_KEY_ID}
- S3_SECRET_ACCESS_KEY=${S3_SECRET_ACCESS_KEY}
- S3_BUCKET=${S3_BUCKET}
- S3_PATH=${S3_PATH}
- S3_FILENAME=${S3_FILENAME}
- POSTGRES_USER=${POSTGRES_USER:-dipdup}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-changeme}
- POSTGRES_DB=${POSTGRES_DB:-dipdup}
- POSTGRES_HOST=${POSTGRES_HOST:-db}
- POSTGRES_EXTRA_OPTS=${POSTGRES_EXTRA_OPTS}
- HEARTBEAT_URI=${HEARTBEAT_URI}
- SCHEDULE=${SCHEDULE}
```
### Digital Ocean
`S3_ENDPOINT` is your space endpoint (not space address)
`S3_BUCKET` is space name

### Schedule

Use CRON expression [generator](https://crontab.guru/).
If `SCHEDULE` is empty, a one-time snapshot will be made.

### Heartbeat

`HEARTBEAT_URI` is optional
46 changes: 46 additions & 0 deletions backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#! /bin/sh

set -eo pipefail
set -o pipefail

if [ -z "${POSTGRES_HOST}" ]; then
POSTGRES_HOST="db"
fi

if [ -z "$POSTGRES_PORT" ]; then
POSTGRES_PORT="5432"
fi

if [ -z "${S3_ENDPOINT}" ]; then
AWS_ARGS=""
else
AWS_ARGS="--endpoint-url ${S3_ENDPOINT}"
fi

# env vars needed for aws tools
export AWS_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=$S3_SECRET_ACCESS_KEY
export AWS_DEFAULT_REGION=$S3_REGION

# env vars needed for pg_dump
export PGPASSWORD=$POSTGRES_PASSWORD
POSTGRES_HOST_OPTS="-h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER $POSTGRES_EXTRA_OPTS"

# TODO: check if database is fresh

echo "Snapshotting $POSTGRES_DB database"
pg_dump -Fc $POSTGRES_HOST_OPTS $POSTGRES_DB > dump.backup

echo "Rotating old snapshot"
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

echo "Uploading fresh snapshot to $S3_BUCKET/$S3_PATH/$S3_FILENAME"
cat dump.backup | aws $AWS_ARGS s3 cp - s3://$S3_BUCKET/$S3_PATH/$S3_FILENAME.backup --acl public-read || exit 2

echo "Snapshot uploaded successfully, removing local file"
rm dump.backup

if [ ! -z "$HEARTBEAT_URI" ]; then
echo "Sending heartbeat signal"
curl -m 10 --retry 5 $HEARTBEAT_URI
fi
53 changes: 53 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#! /bin/sh

set -eo pipefail

if [ -z "${POSTGRES_USER}" ]; then
echo "Please set POSTGRES_USER"
exit 1
fi

if [ -z "${POSTGRES_PASSWORD}" ]; then
echo "Please set POSTGRES_PASSWORD"
exit 1
fi

if [ -z "${POSTGRES_DB}" ]; then
echo "Please set POSTGRES_DB"
exit 1
fi

if [ -z "${S3_ACCESS_KEY_ID}" ]; then
echo "Please set S3_ACCESS_KEY_ID"
exit 1
fi

if [ -z "${S3_SECRET_ACCESS_KEY}" ]; then
echo "Please set S3_SECRET_ACCESS_KEY"
exit 1
fi

if [ -z "${S3_BUCKET}" ]; then
echo "Please set S3_BUCKET"
exit 1
fi

if [ -z "${S3_PATH}" ]; then
echo "Please set S3_PATH"
exit 1
fi

if [ -z "${S3_FILENAME}" ]; then
echo "Please set S3_FILENAME"
exit 1
fi

if [ "${S3_S3V4}" = "yes" ]; then
aws configure set default.s3.signature_version s3v4
fi

if [ -z "${SCHEDULE}" ]; then
sh backup.sh
else
exec go-cron -s "$SCHEDULE" -p 1880 -- /bin/sh ./backup.sh
fi

0 comments on commit 21ce3fd

Please sign in to comment.