Docker image that performs PostgreSQL database backups using restic for CloudNativePG clusters in Kubernetes. The image provides both backup and restore capabilities, using pg_dump in custom format (-Fc) for efficient backups.
- Uses S3 as restic repository backend
- Configurable backup schedule via Kubernetes CronJob
- Restore functionality via Kubernetes Job
- Designed for CloudNativePG database clusters
- Uses pg_dump's custom format for efficient backups
- Incremental backups using restic
- Kubernetes cluster
- CloudNativePG operator installed
- Helm 3.x
- kubectl
- S3 bucket and credentials
The project provides two Helm charts:
helm install backup ./charts/backup -f values.yamlExample values.yaml for backup:
backups:
- name: example
namespace: default
dbAppConfig: cluster-example-app # Secret with database credentials
resticSecretName: restic-secret # Secret with restic configuration
schedule: "0 * * * *" # Hourly backuphelm install restore ./charts/restore -f values.yamlExample values.yaml for restore:
restores:
- name: example
namespace: default
dbAppConfig: cluster-restored-app # Secret with database credentials
resticSecretName: restic-secret # Secret with restic configuration
doCleanup: true # Cleanup db before restore. Defaults to false
snapshotID: latest # Restore specific snapshotID. Defaults to latestCloudNativePG automatically creates secrets containing database credentials. These are referenced in the dbAppConfig values.
Create a secret with restic and S3 configuration:
apiVersion: v1
kind: Secret
metadata:
name: restic-secret
data:
RESTIC_REPOSITORY: base64-encoded-s3-url # e.g., s3:s3.amazonaws.com/bucket-name/path
RESTIC_PASSWORD: base64-encoded-password # used for client-side encryption of the db-dump
AWS_ACCESS_KEY_ID: base64-encoded-key
AWS_SECRET_ACCESS_KEY: base64-encoded-secretBackups run automatically according to the schedule. To trigger a manual backup:
kubectl create job --from=cronjob/restic-backup-example manual-backupDeploy the restore chart to restore the latest backup:
helm install restore ./charts/restore -f values.yamlCheck backup/restore status:
# For backup jobs
kubectl logs -l job-name=restic-backup-example
# For restore jobs
kubectl logs -l job-name=restic-restore-exampleThe project includes a test environment using Kind:
./test/kind-cluster.shThis script:
- Creates a Kind cluster
- Installs CloudNativePG operator
- Creates test PostgreSQL clusters
- Sets up a local persistent volume for testing (not needed for production S3 usage)
- Performs test backup and restore operations
The test environment uses a local persistent volume instead of S3 for easier testing. The volume configuration can be found in the test directory and is not required for production use with S3.
- Tables should be created as the app user to avoid permission issues with backup/restore
- The S3 bucket should have appropriate permissions for the provided AWS credentials
- Backup and restore operations use the same database user credentials as the application