Skip to content

Commit 56253c1

Browse files
authored
Merge pull request #66 from cloudkite-io/mssql-backups
Add templates to support backup resources
2 parents d2ad45a + 93753dd commit 56253c1

File tree

5 files changed

+135
-2
lines changed

5 files changed

+135
-2
lines changed

mssql/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ apiVersion: v2
22
name: mssql
33
description: A Helm chart for Kubernetes
44
type: application
5-
version: 0.1.3
6-
appVersion: "0.1.3"
5+
version: 0.1.4
6+
appVersion: "0.1.4"

mssql/sample.values.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ databases:
2828
memory: 4Gi
2929
limits:
3030
memory: 4Gi
31+
backup:
32+
enabled: true
33+
schedule: "0 2 * * *" # Daily at 2:00 AM UTC
34+
s3Bucket: "your-primary-backup-bucket"
35+
s3Region: "us-east-1"
36+
image:
37+
repository: gcr.io/cloudkite-public/mssql
38+
tag: "2022"
39+
pullPolicy: IfNotPresent
40+
serviceAccount:
41+
create: true
42+
name: "db-a-backup-sa"
43+
# Add your IRSA role ARN annotation here
44+
annotations:
45+
eks.amazonaws.com/role-arn: "arn:aws:iam::123456789012:role/mssql-backup-role-for-db-a"
46+
3147
- name: db-b
3248
database: db_b
3349
port: 1433
@@ -49,3 +65,5 @@ databases:
4965
memory: 4Gi
5066
limits:
5167
memory: 4Gi
68+
backup:
69+
enabled: false
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{{- range .Values.databases }}
2+
{{- if .backup.enabled }}
3+
---
4+
apiVersion: v1
5+
kind: ConfigMap
6+
metadata:
7+
name: {{ .name }}-backup-script
8+
namespace: {{ $.Release.Namespace }}
9+
labels:
10+
app.kubernetes.io/name: {{ .name }}-backup
11+
data:
12+
backup-script.sh: |
13+
#!/bin/bash
14+
set -eo pipefail
15+
16+
# --- Configuration ---
17+
DB_SERVER="{{ .name }}-svc"
18+
DB_DATABASE="{{ .database }}"
19+
DB_USER="sa"
20+
DB_PASSWORD="${MSSQL_SA_PASSWORD}"
21+
22+
# S3 Backup details from values
23+
S3_BUCKET="{{ .backup.s3Bucket }}"
24+
S3_REGION="{{ .backup.s3Region }}"
25+
S3_ENDPOINT="${S3_BUCKET}.s3.${S3_REGION}.amazonaws.com"
26+
BACKUP_FILENAME="${DB_DATABASE}-$(date +%Y-%m-%d-%H-%M-%S).bak"
27+
S3_URL="s3://${S3_ENDPOINT}/backups/${BACKUP_FILENAME}"
28+
CREDENTIAL_NAME="s3://${S3_ENDPOINT}"
29+
30+
echo "Starting backup for database [${DB_DATABASE}] to ${S3_URL}"
31+
32+
# --- T-SQL Commands ---
33+
CREATE_CREDENTIAL_SQL="
34+
IF NOT EXISTS (SELECT 1 FROM sys.credentials WHERE name = '${CREDENTIAL_NAME}')
35+
BEGIN
36+
CREATE CREDENTIAL [${CREDENTIAL_NAME}]
37+
WITH IDENTITY = 'S3 Access Key',
38+
SECRET = '${AWS_ACCESS_KEY_ID}:${AWS_SECRET_ACCESS_KEY}'
39+
END"
40+
41+
BACKUP_DATABASE_SQL="
42+
BACKUP DATABASE [${DB_DATABASE}]
43+
TO URL = '${S3_URL}'
44+
WITH FORMAT, COMPRESSION, STATS = 10, MAXTRANSFERSIZE = 20971520;"
45+
46+
# --- Execution ---
47+
echo "Ensuring S3 credential exists..."
48+
sqlcmd -S "${DB_SERVER}" -U "${DB_USER}" -P "${DB_PASSWORD}" -Q "${CREATE_CREDENTIAL_SQL}" -b -C
49+
50+
echo "Executing backup..."
51+
sqlcmd -S "${DB_SERVER}" -U "${DB_USER}" -P "${DB_PASSWORD}" -Q "${BACKUP_DATABASE_SQL}" -b -C -t 600
52+
53+
echo "Backup of [${DB_DATABASE}] completed successfully."
54+
{{- end }}
55+
{{- end }}

mssql/templates/cronjob.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{{- range .Values.databases }}
2+
{{- if .backup.enabled }}
3+
---
4+
apiVersion: batch/v1
5+
kind: CronJob
6+
metadata:
7+
name: {{ .name }}-backup-cronjob
8+
namespace: {{ $.Release.Namespace }}
9+
labels:
10+
app.kubernetes.io/name: {{ .name }}-backup
11+
spec:
12+
schedule: {{ .backup.schedule | quote }}
13+
concurrencyPolicy: Forbid
14+
successfulJobsHistoryLimit: 3
15+
failedJobsHistoryLimit: 1
16+
jobTemplate:
17+
spec:
18+
template:
19+
metadata:
20+
labels:
21+
app.kubernetes.io/name: {{ .name }}-backup
22+
spec:
23+
serviceAccountName: {{ .backup.serviceAccount.name | default (printf "%s-backup-sa" .name) }}
24+
restartPolicy: OnFailure
25+
containers:
26+
- name: mssql-backup
27+
image: "{{ .backup.image.repository }}:{{ .backup.image.tag }}"
28+
imagePullPolicy: {{ .backup.image.pullPolicy }}
29+
command: ["/bin/bash", "-c", "/scripts/backup-script.sh"]
30+
env:
31+
# Mount the database password from the specified secret
32+
- name: MSSQL_SA_PASSWORD
33+
valueFrom:
34+
secretKeyRef:
35+
name: {{ .secretName }}
36+
key: {{ .secretKey }}
37+
volumeMounts:
38+
- name: backup-script-volume
39+
mountPath: /scripts
40+
volumes:
41+
- name: backup-script-volume
42+
configMap:
43+
name: {{ .name }}-backup-script
44+
defaultMode: 0755
45+
{{- end }}
46+
{{- end }}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{{- range .Values.databases }}
2+
{{- if and .backup.enabled .backup.serviceAccount.create }}
3+
---
4+
apiVersion: v1
5+
kind: ServiceAccount
6+
metadata:
7+
name: {{ .backup.serviceAccount.name | default (printf "%s-backup-sa" .name) }}
8+
namespace: {{ $.Release.Namespace }}
9+
annotations:
10+
{{- toYaml .backup.serviceAccount.annotations | nindent 4 }}
11+
labels:
12+
app.kubernetes.io/name: {{ .name }}-backup
13+
{{- end }}
14+
{{- end }}

0 commit comments

Comments
 (0)