Back up Data to S3-Compatible Storage Using Dumpling
This document describes how to back up the data of the TiDB cluster in Kubernetes to the S3-compatible storage. "Backup" in this document refers to full backup (ad-hoc full backup and scheduled full backup). For the underlying implementation, Dumpling is used to get the logic backup of the TiDB cluster, and then this backup data is sent to the S3-compatible storage.
The backup method described in this document is implemented based on CustomResourceDefinition (CRD) in TiDB Operator v1.1 or later versions.
Ad-hoc full backup to S3-compatible storage
Ad-hoc full backup describes the backup by creating a Backup
custom resource (CR) object. TiDB Operator performs the specific backup operation based on this Backup
object. If an error occurs during the backup process, TiDB Operator does not retry and you need to handle this error manually.
For the current S3-compatible storage types, Ceph and Amazon S3 work normally as tested. Therefore, this document shows examples in which the data of the demo1
TiDB cluster in the tidb-cluster
Kubernetes namespace is backed up to Ceph and Amazon S3 respectively.
Prerequisites for ad-hoc full backup
Execute the following command to create the role-based access control (RBAC) resources in the
tidb-cluster
namespace based on backup-rbac.yaml:kubectl apply -f https://raw.githubusercontent.com/pingcap/tidb-operator/v1.1.15/manifests/backup/backup-rbac.yaml -n tidb-clusterGrant permissions to the remote storage.
To grant permissions to access S3-compatible remote storage, refer to AWS account permissions.
If you use Ceph as the backend storage for testing, you can grant permissions by using AccessKey and SecretKey.
Create the
backup-demo1-tidb-secret
secret which stores the root account and password needed to access the TiDB cluster:kubectl create secret generic backup-demo1-tidb-secret --from-literal=password=${password} --namespace=tidb-cluster
Required database account privileges
- The
SELECT
andUPDATE
privileges of themysql.tidb
table: Before and after the backup, theBackup
CR needs a database account with these privileges to adjust the GC time. - The global privileges:
SELECT
,RELOAD
,LOCK TABLES
andREPLICATION CLIENT
An example for creating a backup user:
CREATE USER 'backup'@'%' IDENTIFIED BY '...';
GRANT
SELECT, RELOAD, LOCK TABLES, REPLICATION CLIENT
ON *.*
TO 'backup'@'%';
GRANT
UPDATE, SELECT
ON mysql.tidb
TO 'backup'@'%';
Ad-hoc backup process
Create the
Backup
CR, and back up cluster data to Amazon S3 by importing AccessKey and SecretKey to grant permissions:kubectl apply -f backup-s3.yamlThe content of
backup-s3.yaml
is as follows:--- apiVersion: pingcap.com/v1alpha1 kind: Backup metadata: name: demo1-backup-s3 namespace: tidb-cluster spec: from: host: ${tidb_host} port: ${tidb_port} user: ${tidb_user} secretName: backup-demo1-tidb-secret s3: provider: aws secretName: s3-secret region: ${region} bucket: ${bucket} # prefix: ${prefix} # storageClass: STANDARD_IA # acl: private # endpoint: # dumpling: # options: # - --threads=16 # - --rows=10000 # tableFilter: # - "test.*" # storageClassName: local-storage storageSize: 10GiCreate the
Backup
CR, and back up data to Ceph by importing AccessKey and SecretKey to grant permissions:kubectl apply -f backup-s3.yamlThe content of
backup-s3.yaml
is as follows:--- apiVersion: pingcap.com/v1alpha1 kind: Backup metadata: name: demo1-backup-s3 namespace: tidb-cluster spec: from: host: ${tidb_host} port: ${tidb_port} user: ${tidb_user} secretName: backup-demo1-tidb-secret s3: provider: ceph secretName: s3-secret endpoint: ${endpoint} # prefix: ${prefix} bucket: ${bucket} # dumpling: # options: # - --threads=16 # - --rows=10000 # tableFilter: # - "test.*" # storageClassName: local-storage storageSize: 10GiCreate the
Backup
CR, and back up data to Amazon S3 by binding IAM with Pod to grant permissions:kubectl apply -f backup-s3.yamlThe content of
backup-s3.yaml
is as follows:--- apiVersion: pingcap.com/v1alpha1 kind: Backup metadata: name: demo1-backup-s3 namespace: tidb-cluster annotations: iam.amazonaws.com/role: arn:aws:iam::123456789012:role/user spec: backupType: full from: host: ${tidb_host} port: ${tidb_port} user: ${tidb_user} secretName: backup-demo1-tidb-secret s3: provider: aws region: ${region} bucket: ${bucket} # prefix: ${prefix} # storageClass: STANDARD_IA # acl: private # endpoint: # dumpling: # options: # - --threads=16 # - --rows=10000 # tableFilter: # - "test.*" # storageClassName: local-storage storageSize: 10GiCreate the
Backup
CR, and back up data to Amazon S3 by binding IAM with ServiceAccount to grant permissions:kubectl apply -f backup-s3.yamlThe content of
backup-s3.yaml
is as follows:--- apiVersion: pingcap.com/v1alpha1 kind: Backup metadata: name: demo1-backup-s3 namespace: tidb-cluster spec: backupType: full serviceAccount: tidb-backup-manager from: host: ${tidb_host} port: ${tidb_port} user: ${tidb_user} secretName: backup-demo1-tidb-secret s3: provider: aws region: ${region} bucket: ${bucket} # prefix: ${prefix} # storageClass: STANDARD_IA # acl: private # endpoint: # dumpling: # options: # - --threads=16 # - --rows=10000 # tableFilter: # - "test.*" # storageClassName: local-storage storageSize: 10Gi
In the examples above, all data of the TiDB cluster is exported and backed up to Amazon S3 or Ceph. You can ignore the acl
, endpoint
, and storageClass
fields in the Amazon S3 configuration. Other S3-compatible storages can also use a configuration similar to that of Amazon S3. You can also leave the fields empty if you do not need to configure them, as shown in the above Ceph configuration. For more information about S3-compatible storage configuration, refer to S3 storage fields.
spec.dumpling
refers to Dumpling-related configuration. You can specify Dumpling's operation parameters in the options
field. See Dumpling Option list for more information. These configuration items of Dumpling can be ignored by default. When these items are not specified, the default values of options
fields are as follows:
options:
- --threads=16
- --rows=10000
For more information about the Backup
CR fields, refer to Backup CR fields.
After creating the Backup
CR, use the following command to check the backup status:
kubectl get bk -n tidb-cluster -owide
To get detailed information on a backup job, use the following command. For $backup_job_name
in the command, use the name from the output of the previous command.
kubectl describe bk -n tidb-cluster $backup_job_name
To run ad-hoc backup again, you need to delete the backup CR and create it again.
Scheduled full backup to S3-compatible storage
You can set a backup policy to perform scheduled backups of the TiDB cluster, and set a backup retention policy to avoid excessive backup items. A scheduled full backup is described by a custom BackupSchedule
CR object. A full backup is triggered at each backup time point. Its underlying implementation is the ad-hoc full backup.
Prerequisites for scheduled backup
The prerequisites for the scheduled backup is the same as the prerequisites for ad-hoc full backup.
Scheduled backup process
Create the
BackupSchedule
CR to enable the scheduled full backup to Amazon S3 by importing AccessKey and SecretKey to grant permissions:kubectl apply -f backup-schedule-s3.yamlThe content of
backup-schedule-s3.yaml
is as follows:--- apiVersion: pingcap.com/v1alpha1 kind: BackupSchedule metadata: name: demo1-backup-schedule-s3 namespace: tidb-cluster spec: #maxBackups: 5 #pause: true maxReservedTime: "3h" schedule: "*/2 * * * *" backupTemplate: from: host: ${tidb_host} port: ${tidb_port} user: ${tidb_user} secretName: backup-demo1-tidb-secret s3: provider: aws secretName: s3-secret region: ${region} bucket: ${bucket} # prefix: ${prefix} # storageClass: STANDARD_IA # acl: private # endpoint: # dumpling: # options: # - --threads=16 # - --rows=10000 # tableFilter: # - "test.*" # storageClassName: local-storage storageSize: 10GiCreate the
BackupSchedule
CR to enable the scheduled full backup to Ceph by importing AccessKey and SecretKey to grant permissions:kubectl apply -f backup-schedule-s3.yamlThe content of
backup-schedule-s3.yaml
is as follows:--- apiVersion: pingcap.com/v1alpha1 kind: BackupSchedule metadata: name: demo1-backup-schedule-ceph namespace: tidb-cluster spec: #maxBackups: 5 #pause: true maxReservedTime: "3h" schedule: "*/2 * * * *" backupTemplate: from: host: ${tidb_host} port: ${tidb_port} user: ${tidb_user} secretName: backup-demo1-tidb-secret s3: provider: ceph secretName: s3-secret endpoint: ${endpoint} bucket: ${bucket} # prefix: ${prefix} # dumpling: # options: # - --threads=16 # - --rows=10000 # tableFilter: # - "test.*" # storageClassName: local-storage storageSize: 10GiCreate the
BackupSchedule
CR to enable the scheduled full backup, and back up the cluster data to Amazon S3 by binding IAM with Pod to grant permissions:kubectl apply -f backup-schedule-s3.yamlThe content of
backup-schedule-s3.yaml
is as follows:--- apiVersion: pingcap.com/v1alpha1 kind: BackupSchedule metadata: name: demo1-backup-schedule-s3 namespace: tidb-cluster annotations: iam.amazonaws.com/role: arn:aws:iam::123456789012:role/user spec: #maxBackups: 5 #pause: true maxReservedTime: "3h" schedule: "*/2 * * * *" backupTemplate: from: host: ${tidb_host} port: ${tidb_port} user: ${tidb_user} secretName: backup-demo1-tidb-secret s3: provider: aws region: ${region} bucket: ${bucket} # prefix: ${prefix} # storageClass: STANDARD_IA # acl: private # endpoint: # dumpling: # options: # - --threads=16 # - --rows=10000 # tableFilter: # - "test.*" # storageClassName: local-storage storageSize: 10GiCreate the
BackupSchedule
CR to enable the scheduled full backup, and back up the cluster data to Amazon S3 by binding IAM with ServiceAccount to grant permissions:kubectl apply -f backup-schedule-s3.yamlThe content of
backup-schedule-s3.yaml
is as follows:--- apiVersion: pingcap.com/v1alpha1 kind: BackupSchedule metadata: name: demo1-backup-schedule-s3 namespace: tidb-cluster spec: #maxBackups: 5 #pause: true maxReservedTime: "3h" schedule: "*/2 * * * *" serviceAccount: tidb-backup-manager backupTemplate: from: host: ${tidb_host} port: ${tidb_port} user: ${tidb_user} secretName: backup-demo1-tidb-secret s3: provider: aws region: ${region} bucket: ${bucket} # prefix: ${prefix} # storageClass: STANDARD_IA # acl: private # endpoint: # dumpling: # options: # - --threads=16 # - --rows=10000 # tableFilter: # - "test.*" # storageClassName: local-storage storageSize: 10Gi
After creating the scheduled full backup, you can use the following command to check the backup status:
kubectl get bks -n tidb-cluster -owide
You can use the following command to check all the backup items:
kubectl get bk -l tidb.pingcap.com/backup-schedule=demo1-backup-schedule-s3 -n tidb-cluster
From the example above, you can see that the backupSchedule
configuration consists of two parts. One is the unique configuration of backupSchedule
, and the other is backupTemplate
.
backupTemplate
specifies the configuration related to the cluster and remote storage, which is the same as the spec
configuration of the Backup
CR. For the unique configuration of backupSchedule
, refer to BackupSchedule CR fields.
Delete the backup CR
Refer to Delete the Backup CR.
Troubleshooting
If you encounter any problem during the backup process, refer to Common Deployment Failures.