Skip to content

Backing Up a Database to S3

A Docker environment is enough to run a nightly database backup without a dedicated backup service — dump the database, compress it, and upload it straight to S3. This guide walks through the setup for a PostgreSQL database, using an environment, a schedule, and a Command that pipes three tools together.

Create the environment

  1. Go to Environments and click Add Environment.

  2. Set Type to Docker and Source to Image.

  3. Set Image to postgres:16-alpine — it ships pg_dump, but not the AWS CLI, so add that under Additional Dockerfile Instructions:

    dockerfile
    RUN apk add --no-cache aws-cli
  4. Save the environment.

Write the schedule

  1. Go to Schedules and click Add Schedule.

  2. Set Environment to the Docker environment created above.

  3. Under Environment Variables, add the database connection details and AWS credentials:

    NameValue
    DB_HOSTyour database host
    DB_USERyour database user
    DB_NAMEthe database to back up
    PGPASSWORDthe database user's password
    AWS_ACCESS_KEY_IDAWS access key with S3 write access
    AWS_SECRET_ACCESS_KEYthe matching secret key
    AWS_DEFAULT_REGIONthe S3 bucket's region

    pg_dump reads PGPASSWORD automatically, and the AWS CLI reads the AWS_* variables — neither needs to be passed on the command line.

  4. Set Command to dump, compress, and upload in one pipeline:

    bash
    pg_dump -h "$DB_HOST" -U "$DB_USER" "$DB_NAME" \
      | gzip \
      | aws s3 cp - "s3://backups/$DB_NAME-$(date +%F).sql.gz"
  5. Set Schedule to 0 2 * * * for a nightly backup at 2am.

  6. Save the schedule and set it to Active.

Adjusting the example

  • For MySQL or MariaDB, use a mysql:8 or mariadb:11 image and swap the Command for mysqldump -h "$DB_HOST" -u "$DB_USER" "$DB_NAME" | gzip | aws s3 cp - ....
  • To upload elsewhere, replace the final aws s3 cp with the CLI for your target — for example az storage blob upload for Azure Blob Storage, installed the same way via Additional Dockerfile Instructions.
  • Enable Notify on failed runs on the schedule so a failed pg_dump or upload doesn't go unnoticed — see Notifications.
Was this page helpful?