How to backup your Postgres DB into AWS S3 in Kubernetes

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

Image for post
Image for post
Based on the photo by Erda Estremera on Unsplash
Image for post
Image for post
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”.

Image for post
Image for post
AWS S3 New bucket wizard
Image for post
Image for post
Just the permissions needed
Image for post
Image for post
Wizard to create a user with AWS API access only
Image for post
Image for post
Check “Attach existing policies directly”
Image for post
Image for post
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 * * * *).

Image for post
Image for post
Backup done! AWS S3 console

Conclusion

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

Image for post
Image for post
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