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

restore database from cloud #160

Merged
merged 13 commits into from
Nov 7, 2018
62 changes: 62 additions & 0 deletions charts/tidb-cluster/templates/restore-job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{- if .Values.restore.create }}
apiVersion: batch/v1
kind: Job
metadata:
name: {{ .Values.clusterName }}-restore
labels:
app.kubernetes.io/name: {{ template "chart.name" . }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
app.kubernetes.io/instance: {{ .Values.clusterName }}
app.kubernetes.io/component: restore
helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
spec:
template:
metadata:
labels:
app.kubernetes.io/name: {{ template "chart.name" . }}
app.kubernetes.io/instance: {{ .Values.clusterName }}
app.kubernetes.io/component: restore
spec:
restartPolicy: OnFailure
containers:
- name: tidb-restore-job
image: {{ .Values.restore.image }}
command:
- /bin/sh
- -c
- |-
{{ tuple "scripts/_start_restore.sh.tpl" . | include "helm-toolkit.utils.template" | indent 10 }}
volumeMounts:
- name: data
mountPath: "/data"
{{- if .Values.restore.gcp }}
- name: gcp-credentials
mountPath: "/gcp"
readOnly: true
{{- end }}
env:
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
{{- if .Values.restore.gcp }}
- name: GOOGLE_APPLICATION_CREDENTIALS
value: /gcp/credentials.json
{{- end }}
- name: TIDB_PASSWORD
valueFrom:
secretKeyRef:
name: {{ .Values.clusterName }}-restore-secret
key: user-password
restartPolicy: OnFailure
Copy link
Contributor

Choose a reason for hiding this comment

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

This is here a second time.

volumes:
- name: data
persistentVolumeClaim:
claimName: {{ .Values.clusterName }}-restore
{{- if .Values.restore.gcp }}
- name: gcp-credentials
secret:
secretName: {{ .Values.clusterName }}-restore-secret
{{- end }}
{{- end }}

21 changes: 21 additions & 0 deletions charts/tidb-cluster/templates/restore-pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{- if .Values.restore.create }}
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: {{ .Values.clusterName }}-restore
labels:
app.kubernetes.io/name: {{ template "chart.name" . }}
app.kubernetes.io/managed-by: tidb-operator
app.kubernetes.io/instance: {{ .Values.clusterName }}
app.kubernetes.io/component: restore
helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
spec:
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage: {{ .Values.restore.storage }}
storageClassName: {{ .Values.restore.storageClassName }}
{{- end }}

19 changes: 19 additions & 0 deletions charts/tidb-cluster/templates/restore-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{- if .Values.restore.create }}
apiVersion: v1
kind: Secret
metadata:
name: {{ .Values.clusterName }}-restore
labels:
app.kubernetes.io/name: {{ template "chart.name" . }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
app.kubernetes.io/instance: {{ .Values.clusterName }}
app.kubernetes.io/component: restore
helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
type: Opaque
data:
{{- if .Values.restore.gcp }}
credentials.json: {{ .Values.restore.gcp.credentialsData }}
{{- end }}
user-password: {{ .Values.restore.password | b64enc | quote }}
{{- end }}

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ host=`echo {{ .Values.clusterName }}_TIDB_SERVICE_HOST | tr '[a-z]' '[A-Z]'`
mkdir -p /data/${dirname}/
cp /savepoint-dir/savepoint /data/${dirname}/

mydumper \
/mydumper \
--outputdir=/data/${dirname} \
--host=`eval echo '${'$host'}'` \
--port=4000 \
Expand Down
23 changes: 23 additions & 0 deletions charts/tidb-cluster/templates/scripts/_start_restore.sh.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
set -euo pipefail

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

{{- if .Values.restore.gcp }}
downloader \
Copy link
Contributor

Choose a reason for hiding this comment

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

This fails for me. /bin/sh: line 7: downloader: command not found

Copy link
Member

Choose a reason for hiding this comment

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

The latest image is just pushed to DockerHub.

--cloud=gcp \
--bucket={{ .Values.restore.gcp.bucket }} \
--srcDir={{ .Values.restore.gcp.srcDir }} \
--destDir=${dataDir}

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

25 changes: 25 additions & 0 deletions charts/tidb-cluster/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,31 @@ binlog:
# kafkaAddrs: "127.0.0.1:9092"
# kafkaVersion: "0.8.2.0"

restore:
create: false
# https://github.com/tennix/tidb-cloud-backup
image: pingcap/tidb-cloud-backup:latest
# storageClassName is a StorageClass provides a way for administrators to describe the "classes" of storage they offer.
# different classes might map to quality-of-service levels, or to backup policies,
# or to arbitrary policies determined by the cluster administrators.
# refer to https://kubernetes.io/docs/concepts/storage/storage-classes
storageClassName: local-storage
storage: 1000Gi
# loader options https://www.pingcap.com/docs-cn/tools/loader/
options: "-t 16"
# user must set tidb cluster user and password
user: restore
password: "Password here ..."
# backup data comes from gcp
gcp: {}
# credentialsData is base64 format of the service account json file data
# https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually
# and then: cat credentials.json | base64 | tr -d '\n'
# credentialsData: ""
# bucket: ""
# srcDir is the directory of backup data in bucket
# srcDir: ""

metaInstance: "{{ $labels.instance }}"
metaType: "{{ $labels.type }}"
metaValue: "{{ $value }}"