Skip to content

Commit 3ded330

Browse files
committed
first commit
0 parents  commit 3ded330

File tree

7 files changed

+440
-0
lines changed

7 files changed

+440
-0
lines changed

.github/workflows/build.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Create and push a Docker image
2+
3+
on:
4+
push:
5+
branches: ['main']
6+
7+
env:
8+
REGISTRY: ghcr.io
9+
IMAGE_NAME: ${{ github.repository }}
10+
VERSION: $GITHUB_SHA
11+
12+
jobs:
13+
build-and-push-image:
14+
runs-on: ubuntu-latest
15+
16+
permissions:
17+
contents: read
18+
packages: write
19+
20+
steps:
21+
- name: Checkout repository
22+
uses: actions/checkout@v4
23+
24+
- name: Create version tag
25+
id: version
26+
run: echo "tag=$(git show -s --format="%ct-%h" $GITHUB_SHA)" >> $GITHUB_OUTPUT
27+
28+
- name: Log in to the container registry
29+
uses: docker/login-action@v3
30+
with:
31+
registry: ${{ env.REGISTRY }}
32+
username: ${{ github.actor }}
33+
password: ${{ secrets.GITHUB_TOKEN }}
34+
35+
- name: Build and push Docker image
36+
uses: docker/build-push-action@v5
37+
with:
38+
pull: true
39+
push: true
40+
tags: |
41+
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
42+
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.tag }}
43+
build-args: VERSION=${{ steps.version.outputs.tag }}

Dockerfile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM ghcr.io/tweedegolf/debian:bookworm
2+
3+
# Install postgresql client
4+
ENV POSTGRESQL_VERSION 16
5+
RUN curl -s -L https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
6+
&& echo "deb http://apt.postgresql.org/pub/repos/apt/ bookworm-pgdg main" > /etc/apt/sources.list.d/pgdg.list \
7+
&& apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
8+
postgresql-client-$POSTGRESQL_VERSION \
9+
bzip2 \
10+
python3 \
11+
&& rm -rf /var/lib/apt/lists/*
12+
13+
# https://github.com/restic/restic/releases
14+
ENV RESTIC_VERSION 0.16.4
15+
# install restic, see https://restic.readthedocs.io/en/stable/020_installation.html#official-binaries
16+
RUN curl -sSLfo /usr/local/bin/restic.bz2 \
17+
"https://github.com/restic/restic/releases/download/v${RESTIC_VERSION}/restic_${RESTIC_VERSION}_linux_amd64.bz2" \
18+
&& bzip2 -d /usr/local/bin/restic.bz2 \
19+
&& chmod +x /usr/local/bin/restic
20+
21+
# Install backup scripts
22+
COPY bin/* /usr/local/bin/
23+
RUN ["/usr/local/bin/backup.sh"]

README.md

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# TGBS
2+
3+
Tweede golf backup service
4+
5+
This image allows you to backup data in a docker/kubernetes environment to a
6+
restic repository. This image is best suited to be run at scheduled times (e.g.
7+
as a cron job).
8+
9+
The most basic operation of this image would be to mount some image/disk into
10+
the container and create a backup from that mount. You can also mount S3 or
11+
GCS object storage buckets to backup their files. Note that this is not
12+
recommended for very large buckets.
13+
14+
This image also has an option to connect to a PostgreSQL database and create a
15+
backup file/directory, and upload that result to a restic repository.
16+
17+
Most of these modes of operation are controlled by environment variables.
18+
19+
## Backup settings
20+
The listing below contains a short overview of the environment variables
21+
supported by restic and which ones are required when backing up to a repository
22+
on Backblaze B2 storage. For details on the environment variables restic
23+
supports, see [their documentation](https://restic.readthedocs.io/en/stable/040_backup.html#environment-variables).
24+
25+
### RESTIC_REPOSITORY
26+
The repository url for the backup.
27+
28+
### RESTIC_PASSWORD
29+
The password to access the repository. In a kubernetes environment this should
30+
be made available via a secret and not directly in the kubernetes config.
31+
32+
### B2_ACCOUNT_ID
33+
The account id of the account that has write access to the backblaze repository.
34+
35+
### B2_ACCOUNT_KEY
36+
The secret account key of the account that has write access to the backblaze
37+
repository.
38+
39+
### TGBS_BACKUP_LOCK
40+
If this is set to `1`, the `--no-lock` flag will not be set.
41+
42+
### TGBS_BACKUP_TAGS
43+
If this is set, then the backup is tagged with the value of this environment
44+
variable. Different tags can be comma-separated. If the variable is not set,
45+
then the backup is not tagged.
46+
47+
### TGBS_BACKUP_PATH
48+
If this is specified, create a backup of the given path (either a directory or
49+
file).
50+
51+
## PostgreSQL database backup
52+
To create a PostgreSQL database backup, set the `TGBS_PSQL_BACKUP` to `1`.
53+
To configure the database connection, use the environment variables available
54+
to postgresql clients: https://www.postgresql.org/docs/current/libpq-envars.html
55+
56+
Generally you will want to set these environment variables for a simple database
57+
backup:
58+
59+
TGBS_PSQL_BACKUP=1
60+
PGHOST=somehost
61+
PGDATABASE=mydatabase
62+
PGUSER=myuser
63+
PGPASSWORD=password
64+
65+
Here is a full list of environment variable this image listens for:
66+
67+
### TGBS_PSQL_BACKUP
68+
Set this variable to `1` to enable backups of PostgreSQL.
69+
70+
### TGBS_PSQL_BACKUP_TAGS
71+
If this is set, this overrides the tags for the PostgreSQL specific part of the
72+
backup. This variable works the same as the `TGBS_BACKUP_TAGS` variable.
73+
74+
### TGBS_PSQL_BACKUP_JOBS
75+
Set the number of jobs to backup. By default this will be the number of cores
76+
available to the backup container.
77+
78+
### TGBS_PSQL_BACKUP_OWNER
79+
Set this variable to `1` to backup owner information. This is not done by
80+
default.
81+
82+
### TGBS_PSQL_BACKUP_PRIVILEGES
83+
Set this variable to `1` to backup privilege information (grants). This is not
84+
done by default.
85+
86+
### TGBS_PSQL_BACKUP_FORMAT
87+
Set this variable to `c` to change the backup format to the custom format, which
88+
will result in a single file instead of a directory. In most cases the
89+
directory format is more suited for backup using restic.
90+
91+
### TGBS_PSQL_BACKUP_COMPRESS
92+
Set the compression level to a number between `0` (no compression) and
93+
`9` (maximum compression).
94+
95+
### PGURL
96+
Instead of specifying the `PGHOST`, `PGPORT`, `PGDATABASE`, `PGUSER` and
97+
`PGPASSWORD` environment variables individually, you can also specify the
98+
`PGURL` variable as an (non-standard) alternative.

bin/backup.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
/usr/local/bin/docker-psql-backup.sh
6+
/usr/local/bin/docker-file-backup.sh

bin/docker-file-backup.sh

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
echoerr() { echo "$@" 1>&2; }
6+
7+
if [ ! -z "$TGBS_BACKUP_PATH" ]; then
8+
if [ -z "$RESTIC_REPOSITORY" ]; then
9+
echoerr "--- ERROR: No restic repository provided, you must provide one"
10+
exit 1
11+
fi
12+
echoerr "--- Creating file/directory backup"
13+
14+
backup_cmd=( restic backup )
15+
16+
# Add tags to the backup
17+
TGBS_BACKUP_TAGS_CLEAN=()
18+
if [ ! -z "$TGBS_BACKUP_TAGS" ]; then
19+
IFS=',' tag_list=("$TGBS_BACKUP_TAGS")
20+
for tag in ${tag_list[@]}; do
21+
tag="${tag#"${tag%%[![:space:]]*}"}"
22+
tag="${tag%"${tag##*[![:space:]]}"}"
23+
backup_cmd+=( --tag "'$tag'" )
24+
TGBS_BACKUP_TAGS_CLEAN+=( "$tag" )
25+
done
26+
fi
27+
28+
TGBS_BACKUP_LOCKFILE=true
29+
if [ -z "$TGBS_BACKUP_LOCK" ] || [ "$TGBS_BACKUP_LOCK" == "0" ] || [ "$TGBS_BACKUP_LOCK" == "false" ]; then
30+
backup_cmd+=( "--no-lock" )
31+
TGBS_BACKUP_LOCKFILE=false
32+
fi
33+
34+
backup_cmd+=( "$TGBS_BACKUP_PATH" )
35+
36+
if [ "${#TGBS_BACKUP_TAGS_CLEAN[@]}" -gt 0 ]; then
37+
IFS=',' echoerr "--- Using tags for restic snapshot: ${TGBS_BACKUP_TAGS_CLEAN[*]}"
38+
fi
39+
if [ "${TGBS_BACKUP_LOCKFILE}" = false ]; then
40+
echoerr "--- WARNING: Not using a lockfile"
41+
fi
42+
echoerr "--- Creating restic snapshot from $TGBS_BACKUP_PATH to repository $RESTIC_REPOSITORY"
43+
44+
# Run the restic command
45+
eval "${backup_cmd[@]}"
46+
else
47+
echoerr "--- Not creating file/directory backup"
48+
fi

0 commit comments

Comments
 (0)