diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index da4aae8..c717faf 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -31,6 +31,7 @@ jobs: run: | helm package \ charts/pg-db-owner \ + charts/pg-db-backup-do-space \ --destination .cr-release-packages - name: Run chart-releaser diff --git a/.gitignore b/.gitignore index 3c44142..9f7a2c0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.iml local/ /.cr-release-packages +/values.yaml diff --git a/README.md b/README.md index 3e51e27..1bfab0d 100644 --- a/README.md +++ b/README.md @@ -19,3 +19,4 @@ ontrack` to see the charts. # Charts * [`pg-db-owner`](charts/pg-db-owner/README.md) - setting up the owner of a Postgres database inside a cluster +* [`pg-db-backup-do-space`](charts/pg-db-backup-do-space/README.md) - job to backup a Digital Ocean (DO) Postgres database into a DO Space diff --git a/charts/pg-db-backup-do-space/Chart.yaml b/charts/pg-db-backup-do-space/Chart.yaml new file mode 100644 index 0000000..df0dffe --- /dev/null +++ b/charts/pg-db-backup-do-space/Chart.yaml @@ -0,0 +1,8 @@ +apiVersion: v2 +name: pg-db-backup-do-space +description: This chart is used to set up a job which saves the content of a Digital Ocean database as SQL into a Digital Ocean space. + +type: application + +version: 0.1.0 +appVersion: "0.1.0" diff --git a/charts/pg-db-backup-do-space/README.md b/charts/pg-db-backup-do-space/README.md new file mode 100644 index 0000000..dc7b70e --- /dev/null +++ b/charts/pg-db-backup-do-space/README.md @@ -0,0 +1,19 @@ +[`pg-db-backup-do-space`](values.yaml) +====================================== + +This chart is used to set up a job which saves the content of a Digital Ocean database +as SQL into a Digital Ocean space. + +# Usage + +```bash +helm install my-db-owner postgres-helpers/pg-db-backup-do-space --values values.yaml +``` + +> See [`values.yaml`](values.yaml) for the list of options. + +# Change log + +## 0.1.0 + +* Initial version diff --git a/charts/pg-db-backup-do-space/templates/db-extraction-job.yaml b/charts/pg-db-backup-do-space/templates/db-extraction-job.yaml new file mode 100644 index 0000000..4ad9afb --- /dev/null +++ b/charts/pg-db-backup-do-space/templates/db-extraction-job.yaml @@ -0,0 +1,47 @@ +apiVersion: batch/v1 +kind: CronJob +metadata: + name: "{{ .Release.Name }}-pg-db-backup-do-space" +spec: + schedule: {{ .Values.backup.schedule | quote }} + jobTemplate: + spec: + template: + spec: + restartPolicy: OnFailure + containers: + - name: extraction + image: "postgres:{{ .Values.database.version }}" + imagePullPolicy: IfNotPresent + command: [ "sh", "-c" ] + args: + - | + + pg_dump \ + --username {{ .Values.auth.username }} \ + --no-password \ + --dbname {{ .Values.database.name }} \ + --host {{ .Values.connection.host }} \ + --port {{ .Values.connection.port }} \ + --file dump.sql + + apt-get update + apt-get install -y s3cmd + + s3cmd \ + --access_key=${DO_SPACE_ACCESS_KEY} \ + --secret_key=${DO_SPACE_SECRET_KEY} \ + --host={{ .Values.digitalocean.space.region }}.digitaloceanspaces.com \ + --host-bucket='%(bucket)s.{{ .Values.digitalocean.space.region }}.digitaloceanspaces.com' \ + put \ + dump.sql \ + s3://{{ .Values.digitalocean.space.bucket }}/{{ .Values.digitalocean.space.path.root }}/{{ .Values.database.name }}/dump-$(date +%Y%m%d%H%M%S).sql + env: + - name: PGSSLMODE + value: "require" + - name: PGPASSWORD + value: {{ .Values.auth.password | quote }} + - name: DO_SPACE_ACCESS_KEY + value: {{ .Values.digitalocean.space.accessKey | quote }} + - name: DO_SPACE_SECRET_KEY + value: {{ .Values.digitalocean.space.secretKey | quote }} diff --git a/charts/pg-db-backup-do-space/templates/storage.yaml b/charts/pg-db-backup-do-space/templates/storage.yaml new file mode 100644 index 0000000..8d82ad3 --- /dev/null +++ b/charts/pg-db-backup-do-space/templates/storage.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: "{{ .Release.Name }}-pg-db-backup" +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: {{ .Values.persistence.size | quote }} +{{- if .Values.persistence.storageClass }} +{{- if (eq "-" .Values.persistence.storageClass) }} + storageClassName: "" +{{- else }} + storageClassName: "{{ .Values.persistence.storageClass }}" +{{- end }} +{{- end }} diff --git a/charts/pg-db-backup-do-space/values.yaml b/charts/pg-db-backup-do-space/values.yaml new file mode 100644 index 0000000..e0a307b --- /dev/null +++ b/charts/pg-db-backup-do-space/values.yaml @@ -0,0 +1,45 @@ +--- +# Temporary space +persistence: + # Size of the working directory where to put the SQL file + size: 2Gi + # Storage class to be used (empty for default) + storageClass: "" +# Extraction parameters +backup: + # Schedule + schedule: "0 0 * * *" +# Connection to the PG cluster +connection: + # Host name or IP + host: "" + # Port to reach the PG cluster + port: 0 +# Credentials to connect to the PG cluster as DB admin +auth: + # User name + username: "" + # Password + password: "" +# Database to backup +database: + # Version of PG to use + version: 15 + # Name of the database to backup + name: "" +# Digital Ocean setup +digitalocean: + # Space to target + space: + # Access key + accessKey: "" + # Secret key + secretKey: "" + # Region + region: "fra1" + # Bucket + bucket: "ontrack-run-do-env-production-eu-cluster-database-backup" + # Path definition + path: + # Root + root: sql