

Automate Fly Postgres Database Backup & Store To S3 bucket With GitHub Actio...
source link: https://blog.saeloun.com/2023/12/20/automating-fly-db-backup-with-github-actions/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Automate Fly Postgres Database Backup & Store To S3 bucket With GitHub Actions
Dec 20, 2023Prasanth Chaduvula
I'm a React, Javascript & Rails full-stack Software Engineer. I have been working remotely for the past two years in a remote village. Before joining as a Software Engineer I founded kwiq - a hyperlocal delivery startup to deliver things in remote villages.
Data security and reliability are paramount for any application’s success.
Regular backups play a crucial role in ensuring that our data remains safe and accessible.
Fly.io’s postgres offers automated snapshots, but for added peace of mind, relying on pg_dump
for backups became essential.
GitHub Actions allows us to automate, customize, and execute software development workflows directly in our repositories.
In this blog post, we’ll explore how to automate the backup process of a Fly.io postgres database using GitHub Actions and saving them to a designated S3 bucket.
-
Fly Deploy Tokens:
Deploy tokens are used for secure authentication and authorization purposes during the deployment process. Before going further please read about Fly Deploy Tokens and create a deploy token.
-
S3 Bucket Creation Steps
As we are using S3 bucket to store the db backup files. Please follow the S3 Bucket Creation Steps to have S3 bucket with required credentials.
Within our GitHub repository, head to the .github/workflows
directory (if absent, create it),
and generate a new YAML file titled backup.yml
.
Insert the following YAML content into our freshly crafted backup.yml
file:
name: Back up database
run-name: Task
# every sunday
on:
schedule:
- cron: "0 0 * * 0"
workflow_dispatch:
jobs:
backup:
runs-on: ubuntu-latest
env:
FLY_API_TOKEN: ${{ secrets.FLY_PRODUCTION_DB_TOKEN }}
FLY_DB_APP: ${{ secrets.FLY_DB_APP_NAME }}
PGUSER: postgres
PGPASSWORD: ${{ secrets.PGPASSWORD }}
PGDATABASE: app_production
PGHOST: localhost
PGPORT: 5434
S3_BUCKET: app-db-backup
steps:
- uses: s3-actions/[email protected]
with:
provider: aws
region: us-east-1
access_key: ${{ secrets.S3_ACCESS_KEY }}
secret_key: ${{ secrets.S3_SECRET_KEY }}
- uses: superfly/flyctl-actions/setup-flyctl@master
- name: Set filename
run: echo "filename=db-$(date -u +"%Y-%m-%d-%H%M%S").sql" >> $GITHUB_ENV
- name: install pg_dump version 15.2
run: |
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo tee /etc/apt/trusted.gpg.d/pgdg.asc &>/dev/null
sudo apt-get update
sudo apt-get install -y postgresql-client-15
psql --version
- name: Dump database, gzip, and upload to S3
run: |
flyctl proxy $PGPORT:5432 -a $FLY_DB_APP &
sleep 3
echo Dumping ...
PGPASSWORD=${PGPASSWORD} pg_dump -h $PGHOST -p $PGPORT -x -U $PGUSER -F c -b -v -Z0 -f ${{ env.filename }} ${PGDATABASE}
gzip ${{ env.filename }}
ls
s3cmd put --acl-private ${{ env.filename }}.gz s3://$S3_BUCKET/${{ env.filename }}.gz
Update all the values like FLY_API_TOKEN
, FLY_DB_APP
, PGPASSWORD
and S3 bucket access_key
and secret_key
with the help of Github secrets.
The above Github action runs on every sunday. It takes the db backup and stores it on S3 bucket.
Share this post!
Recommend
-
62
bbb31/slurp master ...
-
18
As a techy, many of the debates I engage in can be boiled down to just one question: Should we pick the new thing or the proven one? A...
-
23
In this tutorial I would like to demonstrate the ease of creating a REST API using postgres functions. The library that I will be using is @thrinz/pgapi . ...
-
61
README.md pq - A pure Go postgres driver for Go's database/sql package
-
14
How to Backup Heroku Postgres Database to an Encrypted AWS S3 Bucket Updated May 12, 2020 16 comments 8 minute read
-
8
This is in continuation of my previous blog post Backup and Recovery of SAP HANA Database Di...
-
11
How to backup and restore a Postgres database Thu 04 June 2020, by Matthew Segal Category: DevOps You've...
-
9
How to automate your Postgres database backups Fri 05 June 2020, by Matthew Segal Category: DevOps If you...
-
7
NAKIVO Blog > Data Protection > Data Protection Fundamentals: How to Backup an Amazon S3 Bucket...
-
5
PostgreSQL 101 for Non-Postgres DBAs (Simple Backup and Restore) Back to the Blog It’s n...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK