Skip to content

Commit 0f572ef

Browse files
authored
Minor Refactoring and Documentation Update (#2)
* Check for static errors in shell scripts with `shellcheck` * Documentation update to reflect latest changes
1 parent ec41eff commit 0f572ef

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

.github/workflows/lint.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Lint
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
8+
jobs:
9+
lint:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v4
14+
15+
- name: Lint
16+
run: make lint

Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ENV S3_PATH 'backup'
1919
ENV S3_ENDPOINT **None**
2020
ENV S3_S3V4 no
2121
ENV SCHEDULE **None**
22+
ENV SUCCESS_WEBHOOK **None**
2223

2324
ADD entrypoint.sh .
2425
ADD backup.sh .

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SHELL_FILES := $(wildcard *.sh)
2+
3+
lint:
4+
@shellcheck --enable=require-variable-braces $(SHELL_FILES) && echo "ShellCheck passed"

README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This is a fork of [karser/postgres-backup-s3](https://github.com/karser/docker-i
99

1010
Docker:
1111
```sh
12-
$ docker run -e S3_ACCESS_KEY_ID=key -e S3_SECRET_ACCESS_KEY=secret -e S3_BUCKET=my-bucket -e S3_PREFIX=backup -e POSTGRES_DATABASE=dbname -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -e POSTGRES_HOST=localhost f213/postgres-backup-s3
12+
$ docker run -e S3_ACCESS_KEY_ID=key -e S3_SECRET_ACCESS_KEY=secret -e S3_BUCKET=my-bucket -e S3_PREFIX=backup -e POSTGRES_DATABASE=dbname -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -e POSTGRES_HOST=localhost -e SCHEDULE="@daily" f213/postgres-backup-s3
1313
```
1414

1515
Docker Compose:
@@ -28,7 +28,7 @@ postgres-backup:
2828
test: curl http://localhost:1880
2929

3030
environment:
31-
SCHEDULE: 0 30 */2 * * * # every 2 hours at HH:30
31+
SCHEDULE: 0 30 */2 * * * * # every 2 hours at HH:30
3232
S3_REGION: region
3333
S3_ACCESS_KEY_ID: key
3434
S3_SECRET_ACCESS_KEY: secret
@@ -42,8 +42,6 @@ postgres-backup:
4242
SUCCESS_WEBHOOK: https://sb-ping.ru/8pp9RGwDDPzTL2R8MRb8Ae
4343
```
4444
45-
### Automatic Periodic Backups
45+
### Crontab format
4646
47-
You can additionally set the `SCHEDULE` environment variable like `-e SCHEDULE="@daily"` to run the backup automatically.
48-
49-
More information about the scheduling can be found [here](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules).
47+
Schedule format with years support. More information about the scheduling can be found [here](https://github.com/aptible/supercronic/tree/master?tab=readme-ov-file#crontab-format)

backup.sh

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#! /bin/sh
22

3+
# shellcheck disable=SC3040 # expecting 'pipefail' derrictive is availabe in the shell
4+
# shellcheck disable=SC2086 # POSTGRES_HOST_OPTS and AWS_ARGS should be splitted by spaces intentionally
5+
36
set -e
47
set -o pipefail
58

@@ -25,8 +28,8 @@ fi
2528

2629
if [ "${POSTGRES_HOST}" = "**None**" ]; then
2730
if [ -n "${POSTGRES_PORT_5432_TCP_ADDR}" ]; then
28-
POSTGRES_HOST=$POSTGRES_PORT_5432_TCP_ADDR
29-
POSTGRES_PORT=$POSTGRES_PORT_5432_TCP_PORT
31+
POSTGRES_HOST="${POSTGRES_PORT_5432_TCP_ADDR}"
32+
POSTGRES_PORT="${POSTGRES_PORT_5432_TCP_PORT}"
3033
else
3134
echo "You need to set the POSTGRES_HOST environment variable."
3235
exit 1
@@ -43,33 +46,33 @@ if [ "${POSTGRES_PASSWORD}" = "**None**" ]; then
4346
exit 1
4447
fi
4548

46-
if [ "${S3_ENDPOINT}" == "**None**" ]; then
49+
if [ "${S3_ENDPOINT}" = "**None**" ]; then
4750
AWS_ARGS=""
4851
else
4952
AWS_ARGS="--endpoint-url ${S3_ENDPOINT}"
5053
fi
5154

5255
# env vars needed for aws tools
53-
export AWS_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID
54-
export AWS_SECRET_ACCESS_KEY=$S3_SECRET_ACCESS_KEY
55-
export AWS_DEFAULT_REGION=$S3_REGION
56+
export AWS_ACCESS_KEY_ID="${S3_ACCESS_KEY_ID}"
57+
export AWS_SECRET_ACCESS_KEY="${S3_SECRET_ACCESS_KEY}"
58+
export AWS_DEFAULT_REGION="${S3_REGION}"
5659

57-
export PGPASSWORD=$POSTGRES_PASSWORD
58-
POSTGRES_HOST_OPTS="-h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER $POSTGRES_EXTRA_OPTS"
60+
export PGPASSWORD="${POSTGRES_PASSWORD}"
61+
POSTGRES_HOST_OPTS="-h ${POSTGRES_HOST} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} ${POSTGRES_EXTRA_OPTS}"
5962

6063
echo "Creating dump of ${POSTGRES_DATABASE} database from ${POSTGRES_HOST}..."
6164

62-
pg_dump -Fc $POSTGRES_HOST_OPTS $POSTGRES_DATABASE > db.dump
65+
pg_dump -Fc ${POSTGRES_HOST_OPTS} "${POSTGRES_DATABASE}" > db.dump
6366

64-
echo "Uploading dump to $S3_BUCKET"
67+
echo "Uploading dump to ${S3_BUCKET}"
6568

66-
cat db.dump | aws $AWS_ARGS s3 cp - s3://$S3_BUCKET/$S3_PREFIX/${POSTGRES_DATABASE}_$(date +"%Y-%m-%dT%H:%M:%SZ").dump || exit 2
69+
aws ${AWS_ARGS} s3 cp db.dump "s3://${S3_BUCKET}/${S3_PREFIX}/${POSTGRES_DATABASE}_$(date +"%Y-%m-%dT%H:%M:%SZ").dump" || exit 2
6770

6871
echo "DB backup uploaded successfully"
6972

7073
rm db.dump
7174

72-
if [ -n $SUCCESS_WEBHOOK ]; then
73-
echo "Notifying $SUCCESS_WEBHOOK"
74-
curl -m 10 --retry 5 $SUCCESS_WEBHOOK
75+
if [ ! "${SUCCESS_WEBHOOK}" = "**None**" ]; then
76+
echo "Notifying ${SUCCESS_WEBHOOK}"
77+
curl -m 10 --retry 5 "${SUCCESS_WEBHOOK}"
7578
fi

0 commit comments

Comments
 (0)