Postgres Backup on Kubernetes and Truenas

As TrueCharts is moving to a breaking change (New Breaking Common Release) which requires recreating Postgres databases for charts, I figured it’s a good time to share my backup script. I’ve been running the script for several months and have already restored dumps from it multiple times.

If not using with TrueCharts or using a different pod naming pattern, it should be easy to adjust the pattern for grep to get the correct pods containing Postgres.

The script works like the following

  1. Find all apps with Postgres databases
  2. Scale down the app so no writes occur anymore to the database
  3. Connect to the Postgres pod
  4. Dump the database inside the pod
  5. Copy the dump from the pod to the host directory
  6. Keep a minimum of x backups and deletes the rest

No more talk, here’s the script:

References

  1. New Breaking Common Release. https://truecharts.org/news/common-release-schedule. Accessed 06.03.2023.