2019-11-29 22:17:47 -07:00
# Introduction
2019-12-01 21:49:48 -07:00
This project provides Docker images to periodically back up a PostgreSQL database to AWS S3, and to restore from the backup as needed.
2019-11-27 20:57:21 -07:00
# Usage
## Backup
```yaml
2022-07-03 13:47:02 -04:00
services:
postgres:
2023-12-21 22:47:10 +01:00
image: postgres:16
2022-07-03 13:47:02 -04:00
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
2019-11-27 20:57:21 -07:00
2022-07-03 13:47:02 -04:00
backup:
2023-12-21 22:47:10 +01:00
image: eeshugerman/postgres-backup-s3:16
2022-07-03 13:47:02 -04:00
environment:
SCHEDULE: '@weekly ' # optional
BACKUP_KEEP_DAYS: 7 # optional
PASSPHRASE: passphrase # optional
S3_REGION: region
S3_ACCESS_KEY_ID: key
S3_SECRET_ACCESS_KEY: secret
S3_BUCKET: my-bucket
S3_PREFIX: backup
POSTGRES_HOST: postgres
POSTGRES_DATABASE: dbname
POSTGRES_USER: user
POSTGRES_PASSWORD: password
2019-11-27 20:57:21 -07:00
```
2022-04-12 21:39:51 +03:00
2023-12-21 22:47:10 +01:00
- Images are tagged by the major PostgreSQL version supported: `12` , `13` , `14` , `15` or `16` .
2021-12-07 22:15:47 -05:00
- The `SCHEDULE` variable determines backup frequency. See go-cron schedules documentation [here ](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules ). Omit to run the backup immediately and then exit.
2019-12-01 18:47:18 -07:00
- If `PASSPHRASE` is provided, the backup will be encrypted using GPG.
2023-01-29 12:23:42 -05:00
- Run `docker exec <container name> sh backup.sh` to trigger a backup ad-hoc.
- If `BACKUP_KEEP_DAYS` is set, backups older than this many days will be deleted from S3.
- Set `S3_ENDPOINT` if you're using a non-AWS S3-compatible storage provider.
2019-11-27 20:57:21 -07:00
## Restore
2024-01-27 11:33:49 -05:00
> [!CAUTION]
> DATA LOSS! All database objects will be dropped and re-created.
2019-12-01 18:47:18 -07:00
### ... from latest backup
2019-11-27 20:57:21 -07:00
```sh
docker exec < container name > sh restore.sh
```
2024-01-27 11:33:49 -05:00
> [!NOTE]
> If your bucket has more than a 1000 files, the latest may not be restored -- only one S3 `ls` command is used
2019-12-01 18:47:18 -07:00
### ... from specific backup
2019-11-29 22:17:47 -07:00
```sh
docker exec < container name > sh restore.sh < timestamp >
```
2019-12-01 21:49:48 -07:00
2022-07-03 12:23:22 -04:00
# Development
## Build the image locally
`ALPINE_VERSION` determines Postgres version compatibility. See [`build-and-push-images.yml` ](.github/workflows/build-and-push-images.yml ) for the latest mapping.
```sh
2022-07-14 06:16:28 +01:00
DOCKER_BUILDKIT=1 docker build --build-arg ALPINE_VERSION=3.14 .
2022-07-03 12:23:22 -04:00
```
2022-07-03 13:43:54 -04:00
## Run a simple test environment with Docker Compose
```sh
cp template.env .env
# fill out your secrets/params in .env
docker compose up -d
```
2022-07-03 12:23:22 -04:00
2021-06-22 08:32:05 -07:00
# Acknowledgements
This project is a fork and re-structuring of @schickling 's [postgres-backup-s3 ](https://github.com/schickling/dockerfiles/tree/master/postgres-backup-s3 ) and [postgres-restore-s3 ](https://github.com/schickling/dockerfiles/tree/master/postgres-restore-s3 ).
2019-12-01 21:49:48 -07:00
## Fork goals
2022-07-26 09:11:27 -04:00
These changes would have been difficult or impossible merge into @schickling 's repo or similarly-structured forks.
2023-01-29 12:23:42 -05:00
- dedicated repository
- automated builds
- support multiple PostgreSQL versions
- backup and restore with one image
2019-12-01 21:49:48 -07:00
2022-07-26 09:11:27 -04:00
## Other changes and features
- some environment variables renamed or removed
2019-12-01 21:49:48 -07:00
- uses `pg_dump` 's `custom` format (see [docs ](https://www.postgresql.org/docs/10/app-pgdump.html ))
- drop and re-create all database objects on restore
2022-07-26 09:11:27 -04:00
- backup blobs and all schemas by default
- no Python 2 dependencies
2019-12-01 21:49:48 -07:00
- filter backups on S3 by database name
2022-07-26 09:11:27 -04:00
- support encrypted (password-protected) backups
- support for restoring from a specific backup by timestamp
- support for auto-removal of old backups