Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor backup #301

Merged
merged 12 commits into from
Mar 12, 2019
4 changes: 2 additions & 2 deletions charts/tidb-backup/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{{- if .Values.backup.create }}
{{- if eq .Values.mode "backup" }}
1. Watch backup job is created
watch kubectl get jobs --namespace {{ .Release.Namespace }} -l app.kubernetes.io/component=backup
2. Wait until backup pods becomes completed
watch kubectl get po --namespace {{ .Release.Namespace }} -l app.kubernetes.io/component=backup -o wide
{{- end -}}
{{- if .Values.restore.create }}
{{- if eq .Values.mode "restore" }}
1. Watch restore job is created
watch kubectl get jobs --namespace {{ .Release.Namespace }} -l app.kubernetes.io/instance={{ .Release.Name }} -o wide
2. Wait until restore pod becomes completed
Expand Down
49 changes: 32 additions & 17 deletions charts/tidb-backup/templates/backup-job.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{{- if .Values.backup.create }}
{{- if eq .Values.mode "backup" }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you validate the mode? Maybe have {{- if neq .Values.mode "restore" && neq .Values.mode "backup" }} ERROR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that should be added. But I can't find any document about fail or error in Helm template.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a fail function. Helm says they have Sprig functions. http://masterminds.github.io/sprig/flow_control.html

apiVersion: batch/v1
kind: Job
metadata:
name: {{ .Values.clusterName }}-backup
name: {{ .Values.clusterName }}-{{ .Values.name }}
labels:
app.kubernetes.io/name: {{ template "chart.name" . }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
Expand All @@ -21,8 +21,8 @@ spec:
spec:
initContainers:
- name: get-ts
image: {{ .Values.backup.binlogImage }}
imagePullPolicy: {{ .Values.backup.binlogImagePullPolicy | default "IfNotPresent" }}
image: {{ .Values.image.binlog }}
imagePullPolicy: {{ .Values.image.pullPolicy | default "IfNotPresent" }}
command:
- /binlogctl
- -pd-urls=http://{{ .Values.clusterName }}-pd:2379
Expand All @@ -33,8 +33,8 @@ spec:
mountPath: "/savepoint-dir"
containers:
- name: backup
image: {{ .Values.backup.mydumperImage }}
imagePullPolicy: {{ .Values.backup.mydumperImagePullPolicy | default "IfNotPresent" }}
image: {{ .Values.image.backup }}
imagePullPolicy: {{ .Values.image.pullPolicy | default "IfNotPresent" }}
command:
- /bin/sh
- -c
Expand All @@ -45,35 +45,50 @@ spec:
mountPath: "/savepoint-dir"
- name: data
mountPath: "/data"
{{- if .Values.backup.gcp }}
{{- if .Values.gcp }}
- name: gcp-credentials
mountPath: "/gcp"
readOnly: true
{{- end }}
env:
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
{{- if .Values.backup.gcp }}
- name: BACKUP_NAME
value: {{ .Values.name | quote }}
{{- if .Values.gcp }}
- name: GOOGLE_APPLICATION_CREDENTIALS
value: /gcp/credentials.json
{{- end }}
{{- if .Values.ceph }}
- name: AWS_ACCESS_KEY_ID
valueFrom:
secretKeyRef:
name: {{ .Values.ceph.secretName }}
key: access_key
- name: AWS_SECRET_ACCESS_KEY
valueFrom:
secretKeyRef:
name: {{ .Values.ceph.secretName }}
key: secret_key
{{- end }}
- name: TIDB_USER
valueFrom:
secretKeyRef:
name: {{ .Values.secretName }}
key: user
- name: TIDB_PASSWORD
valueFrom:
secretKeyRef:
name: {{ .Values.clusterName }}-backup
key: user-password
name: {{ .Values.secretName }}
key: password
restartPolicy: OnFailure
volumes:
- name: savepoint-dir
emptyDir: {}
- name: data
persistentVolumeClaim:
claimName: {{ .Values.clusterName }}-backup
{{- if .Values.backup.gcp }}
claimName: {{ .Values.clusterName }}-{{ .Values.name }}
{{- if .Values.gcp }}
- name: gcp-credentials
secret:
secretName: {{ .Values.clusterName }}-backup
tennix marked this conversation as resolved.
Show resolved Hide resolved
secretName: {{ .Values.gcp.secretName }}
{{- end }}
{{- end }}
8 changes: 3 additions & 5 deletions charts/tidb-backup/templates/backup-pvc.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{{- if .Values.backup.create }}
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: {{ .Values.clusterName }}-backup
name: {{ .Values.clusterName }}-{{ .Values.name }}
labels:
app.kubernetes.io/name: {{ template "chart.name" . }}
app.kubernetes.io/managed-by: tidb-operator
Expand All @@ -15,6 +14,5 @@ spec:
volumeMode: Filesystem
resources:
requests:
storage: {{ .Values.backup.storage }}
storageClassName: {{ .Values.backup.storageClassName }}
{{- end }}
storage: {{ .Values.storage.size }}
storageClassName: {{ .Values.storage.className }}
18 changes: 0 additions & 18 deletions charts/tidb-backup/templates/backup-secret.yaml

This file was deleted.

44 changes: 29 additions & 15 deletions charts/tidb-backup/templates/restore-job.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{{- if .Values.restore.create }}
{{- if eq .Values.mode "restore" }}
apiVersion: batch/v1
kind: Job
metadata:
name: {{ .Values.clusterName }}-restore
name: {{ .Values.clusterName }}-restore-{{ .Values.name }}
labels:
app.kubernetes.io/name: {{ template "chart.name" . }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
Expand All @@ -20,7 +20,7 @@ spec:
restartPolicy: OnFailure
containers:
- name: tidb-restore-job
image: {{ .Values.restore.image }}
image: {{ .Values.image.backup }}
command:
- /bin/sh
- -c
Expand All @@ -29,33 +29,47 @@ spec:
volumeMounts:
- name: data
mountPath: "/data"
{{- if .Values.restore.gcp }}
{{- if .Values.gcp }}
- name: gcp-credentials
mountPath: "/gcp"
readOnly: true
{{- end }}
env:
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
{{- if .Values.restore.gcp }}
- name: BACKUP_NAME
value: {{ .Values.restore.backupName | quote }}
{{- if .Values.gcp }}
- name: GOOGLE_APPLICATION_CREDENTIALS
value: /gcp/credentials.json
{{- end }}
{{- if .Values.ceph }}
- name: AWS_ACCESS_KEY_ID
valueFrom:
secretKeyRef:
name: {{ .Values.ceph.secretName }}
key: access_key
- name: AWS_SECRET_ACCESS_KEY
valueFrom:
secretKeyRef:
name: {{ .Values.ceph.secretName }}
key: secret_key
{{- end }}
- name: TIDB_USER
valueFrom:
secretKeyRef:
name: {{ .Values.secretName }}
key: user
- name: TIDB_PASSWORD
valueFrom:
secretKeyRef:
name: {{ .Values.clusterName }}-restore
key: user-password
name: {{ .Values.secretName }}
key: password
volumes:
- name: data
persistentVolumeClaim:
claimName: {{ .Values.clusterName }}-restore
{{- if .Values.restore.gcp }}
claimName: {{ .Values.clusterName }}-{{ .Values.name }}
{{- if .Values.gcp }}
- name: gcp-credentials
secret:
secretName: {{ .Values.clusterName }}-restore
secretName: {{ .Values.gcp.secretName }}
{{- end }}
{{- end }}

21 changes: 0 additions & 21 deletions charts/tidb-backup/templates/restore-pvc.yaml

This file was deleted.

19 changes: 0 additions & 19 deletions charts/tidb-backup/templates/restore-secret.yaml

This file was deleted.

27 changes: 14 additions & 13 deletions charts/tidb-backup/templates/scripts/_start_backup.sh.tpl
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
set -euo pipefail
dirname=backup-`date +%Y-%m-%dT%H%M%S`-${MY_POD_NAME}

host=`echo {{ .Values.clusterName }}_TIDB_SERVICE_HOST | tr '[a-z]' '[A-Z]' | tr '-' '_'`

mkdir -p /data/${dirname}/
cp /savepoint-dir/savepoint /data/${dirname}/
dirname=/data/${BACKUP_NAME}
mkdir -p ${dirname}
cp /savepoint-dir/savepoint ${dirname}/

/mydumper \
--outputdir=/data/${dirname} \
--outputdir=${dirname} \
--host=`eval echo '${'$host'}'` \
--port=4000 \
--user={{ .Values.backup.user }} \
--user=${TIDB_USER} \
--password=${TIDB_PASSWORD} \
{{ .Values.backup.options }}
{{ .Values.backupOptions }}

{{- if .Values.backup.gcp }}
{{- if .Values.gcp }}
uploader \
--cloud=gcp \
--bucket={{ .Values.backup.gcp.bucket }} \
--backup-dir=/data/${dirname}
--bucket={{ .Values.gcp.bucket }} \
--backup-dir=${dirname}
{{- end }}

{{- if .Values.backup.ceph }}
{{- if .Values.ceph }}
uploader \
--cloud=ceph \
--bucket={{ .Values.backup.ceph.bucket }} \
--endpoint={{ .Values.backup.ceph.endpoint }} \
--backup-dir=/data/${dirname}
--bucket={{ .Values.ceph.bucket }} \
--endpoint={{ .Values.ceph.endpoint }} \
--backup-dir=${dirname}
{{- end }}
29 changes: 14 additions & 15 deletions charts/tidb-backup/templates/scripts/_start_restore.sh.tpl
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
set -euo pipefail

dirname=restore-`date +%Y-%m-%dT%H%M%S`-${MY_POD_NAME}
dataDir=/data/${dirname}
mkdir -p ${dataDir}/
dirname=/data/${BACKUP_NAME}
mkdir -p ${dirname}
host=`echo {{ .Values.clusterName }}_TIDB_SERVICE_HOST | tr '[a-z]' '[A-Z]' | tr '-' '_'`

{{- if .Values.restore.gcp }}
{{- if .Values.gcp }}
downloader \
--cloud=gcp \
--bucket={{ .Values.restore.gcp.bucket }} \
--srcDir={{ .Values.restore.srcDir }} \
--destDir=${dataDir}
--bucket={{ .Values.gcp.bucket }} \
--srcDir=${BACKUP_NAME} \
--destDir=${dirname}
{{- end }}

{{- if .Values.restore.ceph }}
{{- if .Values.ceph }}
downloader \
--cloud=ceph \
--bucket={{ .Values.restore.ceph.bucket }} \
--endpoint={{ .Values.restore.ceph.endpoint }} \
--srcDir={{ .Values.restore.srcDir }} \
--destDir=${dataDir}
--bucket={{ .Values.ceph.bucket }} \
--endpoint={{ .Values.ceph.endpoint }} \
--srcDir=${BACKUP_NAME} \
--destDir=${dirname}
{{- end }}

/loader \
-d ${dataDir}/{{ .Values.restore.srcDir }} \
-d ${BACKUP_NAME} \
-h `eval echo '${'$host'}'` \
-u {{ .Values.restore.user }} \
-u ${TIDB_USER} \
-p ${TIDB_PASSWORD} \
-P 4000 \
{{ .Values.restore.options }}
{{ .Values.restoreOptions }}
Loading