Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to backup to BackBlaze #10

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
start: restore backup

restore: db
docker-compose up -d restore
docker compose up -d restore

backup: db
docker-compose up -d backup
docker compose up -d backup

db:
docker-compose up -d db
docker compose up -d db

clean:
docker-compose kill
docker compose kill
docker system prune -f
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@ Service to backup and/or restore a PostgreSQL database to/from S3

`S3_BUCKET` e.g., _s3://database-backups_ **NOTE: no trailing slash**

>**It's recommended that your S3 bucket have versioning turned on.**
>**It's recommended that your S3 bucket have versioning turned on.** Each backup creates a file of the form _DB_NAME_.sql.gz. If versioning is not turned on, the previous backup file will be replaced with the new one, resulting in a single level of backups.

`B2_BUCKET` (optional) Name of the Backblaze B2 bucket, e.g., _database-backups_. When `B2_BUCKET` is defined, the backup file is copied to the B2 bucket in addition to the S3 bucket.

>**It's recommended that your B2 bucket have versioning and encryption turned on.** Each backup creates a file of the form _DB_NAME_.sql.gz. If versioning is not turned on, the previous backup file will be replaced with the new one, resulting in a single level of backups. Encryption may offer an additional level of protection from attackers. It also has the side effect of preventing downloads of the file via the Backblaze GUI (you'll have to use the `b2` command or the Backblaze API).

`B2_APPLICATION_KEY_ID` (optional; required if `B2_BUCKET` is defined) Backblaze application key ID

`B2_APPLICATION_KEY` (optional; required if `B2_BUCKET` is defined) Backblaze application key secret

`B2_HOST` (optional; required if `B2_BUCKET` is defined) Backblaze B2 bucket's `Endpoint`
## Docker Hub
This image is built automatically on Docker Hub as [silintl/postgresql-backup-restore](https://hub.docker.com/r/silintl/postgresql-backup-restore/)

Expand Down
18 changes: 18 additions & 0 deletions application/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,22 @@ else
echo "${MYNAME}: Copy backup to ${S3_BUCKET} of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
fi

if [ "${B2_BUCKET}" != "" ]; then
start=$(date +%s)
s3cmd \
--access_key=${B2_APPLICATION_KEY_ID} \
--secret_key=${B2_APPLICATION_KEY} \
--host=${B2_HOST} \
--host-bucket='%(bucket)s.'"${B2_HOST}" \
put /tmp/${DB_NAME}.sql.gz s3://${B2_BUCKET}/${DB_NAME}.sql.gz
STATUS=$?
end=$(date +%s)
if [ $STATUS -ne 0 ]; then
echo "${MYNAME}: FATAL: Copy backup to Backblaze B2 bucket ${B2_BUCKET} of ${DB_NAME} returned non-zero status ($STATUS) in $(expr ${end} - ${start}) seconds."
exit $STATUS
else
echo "${MYNAME}: Copy backup to Backblaze B2 bucket ${B2_BUCKET} of ${DB_NAME} completed in $(expr ${end} - ${start}) seconds."
fi
fi

echo "${MYNAME}: backup: Completed"
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '2'
services:
data:
image: silintl/data-volume:latest
Expand Down
6 changes: 6 additions & 0 deletions local.env.dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
AWS_ACCESS_KEY=
AWS_SECRET_KEY=
S3_BUCKET=

# BackBlaze variables
B2_BUCKET=
B2_APPLICATION_KEY_ID=
B2_APPLICATION_KEY=
B2_HOST=