Back up Data to S3-Compatible Storage Using Dumpling
This document describes how to back up the data of the TiDB cluster on Kubernetes to an S3-compatible storage. "Backup" in this document refers to full backup (ad-hoc full backup and scheduled full backup).
The backup method described in this document is implemented based on CustomResourceDefinition (CRD) in TiDB Operator v1.1 or later versions. 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.
Dumpling is a data export tool that exports data stored in TiDB/MySQL as SQL or CSV files and can be used to make a logical full backup or export.
Usage scenarios
You can use the backup method described in this document if you want to make an ad-hoc full backup or scheduled full backup of the TiDB cluster data to S3-compatible storages with the following needs:
- To export SQL or CSV files
- To limit the memory usage of a single SQL statement
- To export the historical data snapshot of TiDB
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
Before you use Dumpling to back up the TiDB cluster data to the S3-compatible storage, make sure that you have the following 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'@'%';
Step 1: Prepare 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.6.0/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
Step 2: Perform ad-hoc backup
This section lists multiple storage access methods. Only follow the method that matches your situation. The methods are as follows:
- Amazon S3 by importing AccessKey and SecretKey
- Ceph by importing AccessKey and SecretKey
- Amazon S3 by binding IAM with Pod
- Amazon S3 by binding IAM with ServiceAccount
Method 1: 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: 10GiMethod 2: Create 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: 10GiMethod 3: Create 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: 10GiMethod 4: Create 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.
Step 1: Prepare for scheduled backup
The prerequisites for the scheduled backup is the same as the prepare for ad-hoc full backup.
Step 2: Perform scheduled backup
Method 1: 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: 10GiMethod 2: Create 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: 10GiMethod 3: Create 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: 10GiMethod 4: Create 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
After the backup, you might need to delete the backup CR. For details, refer to Delete the Backup CR.
Troubleshooting
If you encounter any problem during the backup process, refer to Common Deployment Failures.