Back up Data to GCS Using Dumpling

    The backup method described in this document is implemented using CustomResourceDefinition (CRD) in TiDB Operator v1.1 or later versions. Dumpling is used to get the logic backup of the TiDB cluster, and then this backup data is sent to the remote GCS.

    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.

    You can use the backup method described in this document if you want to make an or scheduled full backup of the TiDB cluster data to GCS 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

    Prerequisites

    Before you use Dumpling to back up the TiDB cluster data to GCS, make sure that you have the following privileges:

    • The and UPDATE privileges of the mysql.tidb table: Before and after the backup, the Backup CR needs a database account with these privileges to adjust the GC time.
    • SELECT
    • RELOAD
    • LOCK TABLES
    • REPLICATION CLIENT

    Ad-hoc full backup describes a backup operation 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.

    To better explain how to perform the backup operation, this document shows an example in which the data of the demo1 TiDB cluster is backed up to the test1 Kubernetes namespace.

    1. Download backup-rbac.yaml and execute the following command to create the role-based access control (RBAC) resources in the test1 namespace:

    2. Grant permissions to the remote storage.

      Refer to .

      1. Create the Backup CR and back up data to GCS:

        1. kubectl apply -f backup-gcs.yaml

        The content of backup-gcs.yaml is as follows:

        The example above backs up all data in the TiDB cluster to GCS. Some parameters in spec.gcs can be ignored, such as location, objectAcl, bucketAcl, and storageClass. For more information about GCS configuration, refer to GCS fields.

        spec.dumpling refers to Dumpling-related configuration. You can specify Dumpling’s operation parameters in the options field. See 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:

        1. options:
        2. - --threads=16

        For more information about the Backup CR fields, refer to Backup CR fields.

      2. After creating the Backup CR, use the following command to check the backup status:

        1. kubectl get bk -n test1 -owide

      Scheduled full backup to GCS

      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.

      The preparation for the scheduled backup is the same as the prepare for ad-hoc full backup.

      1. Create the BackupSchedule CR, and back up cluster data as described below:

        1. ---
        2. apiVersion: pingcap.com/v1alpha1
        3. kind: BackupSchedule
        4. metadata:
        5. name: demo1-backup-schedule-gcs
        6. namespace: test1
        7. spec:
        8. #maxBackups: 5
        9. #pause: true
        10. maxReservedTime: "3h"
        11. schedule: "*/2 * * * *"
        12. backupTemplate:
        13. from:
        14. port: ${tidb_port}
        15. user: ${tidb_user}
        16. secretName: backup-demo1-tidb-secret
        17. secretName: gcs-secret
        18. projectId: ${project_id}
        19. bucket: ${bucket}
        20. # prefix: ${prefix}
        21. # location: us-east1
        22. # storageClass: STANDARD_IA
        23. # objectAcl: private
        24. # bucketAcl: private
        25. # dumpling:
        26. # options:
        27. # - --threads=16
        28. # - --rows=10000
        29. # tableFilter:
        30. # - "test.*"
        31. # storageClassName: local-storage
        32. storageSize: 10Gi
      2. After creating the scheduled full backup, use the following command to check the backup status:

          Use the following command to check all the backup items:

        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 . For the unique configuration of backupSchedule, refer to BackupSchedule CR fields.

        Note

        TiDB Operator creates a PVC used for both ad-hoc full backup and scheduled full backup. The backup data is stored in PV first and then uploaded to remote storage. If you want to delete this PVC after the backup is completed, you can refer to to delete the backup Pod first, and then delete the PVC.

        If the backup data is successfully uploaded to remote storage, TiDB Operator automatically deletes the local data. If the upload fails, the local data is retained.

        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.