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
Go to Environments and click Add Environment.
Set Type to Docker and Source to Image.
Set Image to
postgres:16-alpine— it shipspg_dump, but not the AWS CLI, so add that under Additional Dockerfile Instructions:dockerfileRUN apk add --no-cache aws-cliSave the environment.
Write the schedule
Go to Schedules and click Add Schedule.
Set Environment to the Docker environment created above.
Under Environment Variables, add the database connection details and AWS credentials:
Name Value 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_dumpreadsPGPASSWORDautomatically, and the AWS CLI reads theAWS_*variables — neither needs to be passed on the command line.Set Command to dump, compress, and upload in one pipeline:
bashpg_dump -h "$DB_HOST" -U "$DB_USER" "$DB_NAME" \ | gzip \ | aws s3 cp - "s3://backups/$DB_NAME-$(date +%F).sql.gz"Set Schedule to
0 2 * * *for a nightly backup at 2am.Save the schedule and set it to Active.
Adjusting the example
- For MySQL or MariaDB, use a
mysql:8ormariadb:11image and swap the Command formysqldump -h "$DB_HOST" -u "$DB_USER" "$DB_NAME" | gzip | aws s3 cp - .... - To upload elsewhere, replace the final
aws s3 cpwith the CLI for your target — for exampleaz storage blob uploadfor Azure Blob Storage, installed the same way via Additional Dockerfile Instructions. - Enable Notify on failed runs on the schedule so a failed
pg_dumpor upload doesn't go unnoticed — see Notifications.