Skip to content

Commit

Permalink
feat: PG SQL backup into DO Space
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoraboeuf committed Oct 29, 2023
1 parent e12d934 commit b8489c7
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.iml
local/
/.cr-release-packages
/values.yaml
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions charts/pg-db-backup-do-space/Chart.yaml
Original file line number Diff line number Diff line change
@@ -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"
19 changes: 19 additions & 0 deletions charts/pg-db-backup-do-space/README.md
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions charts/pg-db-backup-do-space/templates/db-extraction-job.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
17 changes: 17 additions & 0 deletions charts/pg-db-backup-do-space/templates/storage.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
45 changes: 45 additions & 0 deletions charts/pg-db-backup-do-space/values.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b8489c7

Please sign in to comment.