How to backup your Postgres DB into AWS S3 in Kubernetes

TL;DR: Use CronJob to schedule a DB daily backup

Based on the photo by Erda Estremera on Unsplash
I know how to recognize good advice

Designing our backup solution

Our business requirements for the backup solution are:

  • Secure and encrypted. Custody of data is essential. We will use secure transport up to S3 and the files will be encrypted with a symmetric key.
  • Kubernetes way. Backups should be done automatically using Kubernetes resources.

Create and configure an AWS S3 bucket

It’s very pretty straightforward create a AWS S3 bucket. We create it using our main AWS account with the “Create bucket wizard”.

AWS S3 New bucket wizard
Just the permissions needed
Wizard to create a user with AWS API access only
Check “Attach existing policies directly”
Our policy ready to be assigned

Write and apply CronJob

We create a bash script with the following steps:

  1. Compression with BZip2.
  2. Encrypt the file.
  3. Upload it to S3.
Our simple but powerful bash script
kubectl apply -f backup-postgres-cronjob.yaml

Check it up!

For a rapid testing, we apply a each 5 minutes cron schedule (*/5 * * * *).

Backup done! AWS S3 console

Conclusion

If you have a Kubernetes cluster, probably you need backups.

Thanks!!!

CTO @ Digitalilusion.com & DigitalSecured.net Beyond-Full-stack developer #go #python #kubernetes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store