Forked and modified from: https://github.com/nullpixel/postgres-docker-gcs-backup
This docker image allows for scheduled backups of a postgres docker container to a Google Cloud Storage bucket.
docker build -t postgres-docker-gcs-backup-dev .
docker run \
-e POSTGRES_HOST="1.2.3.4" \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_DATABASE=postgres \
-e GCLOUD_KEYFILE_BASE64="`base64 -i my-keyfile.json`" \
-e GCLOUD_PROJECT_ID="TODO" \
-e GCS_BACKUP_BUCKET="gs://my-bucket" \
-e SCHEDULE="0500" \
--rm -it postgres-docker-gcs-backup-dev
## Build on a Ubuntu x86-64 droplet
First run scp -r /path/to/postgres-docker-gcs-backup root@IP:~/
apt update && apt install -y docker.io
cd postgres-docker-gcs-backup
docker build -t drpancake/postgres-docker-gcs-backup:2022.06.23 .
docker login
docker push drpancake/postgres-docker-gcs-backup:2022.06.23
Variable | Description |
---|---|
POSTGRES_DATABASE |
The name of the database to backup. |
POSTGRES_HOST |
The host of the database to backup. |
POSTGRES_PORT |
The port of the database to backup. Default: 5432 |
POSTGRES_USER |
The username of the backup user. |
POSTGRES_PASSWORD |
The password of the backup user. |
POSTGRES_EXTRA_OPTS |
Any additional options you wish to pass to pg_dump . Default: '' |
GCLOUD_KEYFILE_BASE64 |
The GCP service account's credential file, in base64. See below for recommendations regarding this. |
GCLOUD_PROJECT_ID |
The Project ID which the bucket you wish to backup to is in. |
GCS_BACKUP_BUCKET |
The gs:// path to the storage bucket you wish to backup to. |
SCHEDULE |
Time to run the backup e.g. "0700" for 7am (UTC) |
We recommend creating a new, write-only service account to the storage bucket you wish to backup to (with the storage.objects.list
and storage.objects.create
permissions).
Below is a sample Docker Compose service.
db_backups:
image: drpancake/postgres-docker-gcs-backup:2022-06-10
depends_on:
- db
environment:
SCHEDULE: "0500"
POSTGRES_HOST: "db"
POSTGRES_DATABASE: "SomeDatabase"
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
GCLOUD_KEYFILE_BASE64: ${GCLOUD_KEYFILE_BASE64}
GCLOUD_PROJECT_ID: "hello-world"
GCS_BACKUP_BUCKET: "gs://my-backup-bucket-name"
You can also optionally mount a volume at /db-backups
to store the backups
there while they are uploading.
To get the service account key in the right format:
GCLOUD_KEYFILE_BASE64="`base64 -i my-key.json`"