### Set up Local Development Environment Source: https://github.com/solectrus/postgres-s3-backup/blob/master/README.md Copy the template environment file and fill in your specific secrets and parameters. Then, start the Docker Compose services in detached mode. ```bash cp template.env .env # fill out your secrets/params in .env docker compose up -d ``` -------------------------------- ### Docker Compose for Scheduled PostgreSQL Backups Source: https://context7.com/solectrus/postgres-s3-backup/llms.txt Example docker-compose.yaml configuration for setting up a scheduled weekly backup with GPG encryption and 7-day retention. Ensure the PostgreSQL service is defined and accessible. ```yaml # docker-compose.yaml — scheduled weekly backup with 7-day retention and encryption services: postgres: image: postgres:18-alpine environment: POSTGRES_USER: myuser POSTGRES_PASSWORD: secret POSTGRES_DB: myapp backup: image: ghcr.io/solectrus/postgres-s3-backup:18 depends_on: - postgres environment: SCHEDULE: "@weekly" # also accepts: @daily, @hourly, "0 3 * * *" BACKUP_KEEP_DAYS: 7 PASSPHRASE: "my-gpg-secret" S3_REGION: "us-east-1" S3_ACCESS_KEY_ID: "AKIAIOSFODNN7EXAMPLE" S3_SECRET_ACCESS_KEY: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" S3_BUCKET: "my-backup-bucket" S3_PREFIX: "postgres" POSTGRES_HOST: postgres POSTGRES_DATABASE: myapp POSTGRES_USER: myuser POSTGRES_PASSWORD: secret ``` -------------------------------- ### Local Development with Docker Compose Source: https://context7.com/solectrus/postgres-s3-backup/llms.txt Sets up a local PostgreSQL test environment using Docker Compose. Requires filling in S3 credentials in a .env file. Starts the stack, tails logs, and allows triggering manual backups or restores. ```bash # 1. Copy the template and fill in your S3 credentials cp template.env .env # Edit .env: # S3_REGION=us-east-1 # S3_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE # S3_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY # S3_BUCKET=my-test-bucket # 2. Start the stack (rebuilds the backup image from source) docker compose up -d --build --force-recreate # 3. Tail backup logs docker compose logs -f backup # 4. Trigger a manual backup docker compose exec backup sh backup.sh # 5. Restore from latest backup docker compose exec backup sh restore.sh ``` -------------------------------- ### Build Custom Docker Image (Dockerfile) Source: https://context7.com/solectrus/postgres-s3-backup/llms.txt Builds a Docker image for a specific PostgreSQL major version. Installs necessary tools like pg_dump, pg_restore, gnupg, aws-cli, and go-cron. Supports multi-arch builds. ```bash # Build for PostgreSQL 18 DOCKER_BUILDKIT=1 docker build \ --build-arg POSTGRES_VERSION=18 \ --tag my-pg-backup:18 \ . # Build for PostgreSQL 16 targeting arm64 DOCKER_BUILDKIT=1 docker build \ --build-arg POSTGRES_VERSION=16 \ --platform linux/arm64 \ --tag my-pg-backup:16-arm64 \ . ``` -------------------------------- ### Build Docker Image Locally Source: https://github.com/solectrus/postgres-s3-backup/blob/master/README.md Build the Docker image for the backup service locally. Set the POSTGRES_VERSION build argument to specify the desired PostgreSQL version. ```bash DOCKER_BUILDKIT=1 docker build --build-arg POSTGRES_VERSION=18 . ``` -------------------------------- ### Performing Ad-Hoc PostgreSQL Backup via Docker Exec Source: https://context7.com/solectrus/postgres-s3-backup/llms.txt Command to trigger an immediate backup using the `backup.sh` script within a running container. The output shows the backup creation, upload, and optional old backup removal process. ```sh # Trigger an ad-hoc backup while the container is running docker exec my-backup-container sh backup.sh ``` ```text # Expected output (unencrypted): # Creating backup of myapp database... # Uploading backup to my-backup-bucket... # upload: ./db.dump to s3://my-backup-bucket/postgres/myapp_2024-06-15T03:00:00.dump # Backup complete. # Removing old backups from my-backup-bucket... # delete: s3://my-backup-bucket/postgres/myapp_2024-06-08T03:00:00.dump # Removal complete. ``` ```text # Expected output (encrypted): # Creating backup of myapp database... # Encrypting backup... # Uploading backup to my-backup-bucket... # upload: ./db.dump.gpg to s3://my-backup-bucket/postgres/myapp_2024-06-15T03:00:00.dump.gpg # Backup complete. ``` -------------------------------- ### Configure Docker Compose for PostgreSQL Backup Source: https://github.com/solectrus/postgres-s3-backup/blob/master/README.md Define services for PostgreSQL and the backup container in docker-compose.yml. Configure backup schedule, S3 credentials, and PostgreSQL connection details via environment variables. ```yaml services: postgres: image: postgres:18-alpine environment: POSTGRES_USER: user POSTGRES_PASSWORD: password backup: image: ghcr.io/solectrus/postgres-s3-backup:18 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 ``` -------------------------------- ### Restore from Specific Backup Source: https://github.com/solectrus/postgres-s3-backup/blob/master/README.md Restore the database from a specific backup identified by its timestamp. Provide the timestamp as an argument to the restore script. Caution: This action can lead to data loss. ```bash docker exec sh restore.sh ``` -------------------------------- ### Restore from Latest Backup (restore.sh) Source: https://context7.com/solectrus/postgres-s3-backup/llms.txt Downloads and restores the most recent backup from S3. This script drops and recreates all database objects before restoring. Ensure the backup container is running. ```bash # ⚠️ WARNING: This DROPS and RECREATES all database objects. # Restore from the latest backup docker exec my-backup-container sh restore.sh # Expected output: # Finding latest backup... # Fetching backup from S3... # download: s3://my-backup-bucket/postgres/myapp_2024-06-15T03:00:00.dump to ./db.dump # Restoring from backup... # Restore complete. ``` -------------------------------- ### Restore from Latest Backup Source: https://github.com/solectrus/postgres-s3-backup/blob/master/README.md Initiate a restore operation from the most recent backup available in the S3 bucket. This will drop and re-create all database objects. Caution: This action can lead to data loss if not performed carefully. ```bash docker exec sh restore.sh ``` -------------------------------- ### Restore from Specific Backup (restore.sh ) Source: https://context7.com/solectrus/postgres-s3-backup/llms.txt Restores a specific backup from S3 by providing its ISO 8601 timestamp. This bypasses the auto-discovery and fetches the exact object. The timestamp format must match the backup creation format. ```bash # Restore from a specific point-in-time backup docker exec my-backup-container sh restore.sh 2024-06-10T03:00:00 # Expected output: # Fetching backup from S3... # download: s3://my-backup-bucket/postgres/myapp_2024-06-10T03:00:00.dump to ./db.dump # Restoring from backup... # Restore complete. ``` -------------------------------- ### Trigger Ad-hoc Backup Source: https://github.com/solectrus/postgres-s3-backup/blob/master/README.md Execute the backup script within the running backup container to perform an immediate backup. Replace `` with the actual name of your backup container. ```bash docker exec sh backup.sh ``` -------------------------------- ### Environment Variables for PostgreSQL S3 Backup Source: https://context7.com/solectrus/postgres-s3-backup/llms.txt Required and optional environment variables for configuring the backup solution. S3_ENDPOINT enables compatibility with non-AWS S3 providers. PASSPHRASE enables GPG encryption. BACKUP_KEEP_DAYS controls retention. ```sh # Required variables S3_BUCKET="my-backup-bucket" S3_REGION="us-east-1" S3_ACCESS_KEY_ID="AKIAIOSFODNN7EXAMPLE" S3_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" S3_PREFIX="backups" # S3 key prefix / folder POSTGRES_HOST="postgres" POSTGRES_PORT="5432" # default POSTGRES_DATABASE="myapp" POSTGRES_USER="dbuser" POSTGRES_PASSWORD="secret" # Optional S3_ENDPOINT="https://s3.us-west-004.backblazeb2.com" # S3-compatible provider S3_S3V4="yes" # force SigV4 signing (required for some providers) PASSPHRASE="gpg-passphrase" # enable GPG symmetric encryption BACKUP_KEEP_DAYS="7" # delete backups older than N days SCHEDULE="@daily" # cron schedule; omit for one-shot mode PGDUMP_EXTRA_OPTS="" # extra flags passed directly to pg_dump ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.