Back up Data to PV

This document describes how to back up the data of a TiDB cluster in Kubernetes to Persistent Volumes (PVs). PVs in this documentation can be any Kubernetes supported PV types. This document uses NFS as an example PV type.

The backup method described in this document is implemented based on CustomResourceDefinition (CRD) in TiDB Operator. For the underlying implementation, BR is used to get the backup data of the TiDB cluster, and then send the data to PVs. BR stands for Backup & Restore, which is a command-line tool for distributed backup and recovery of the TiDB cluster data.

Usage scenarios

If you have the following backup needs, you can use BR to make an ad-hoc backup or scheduled full backup of the TiDB cluster data to PVs:

  • To back up a large volume of data at a fast speed
  • To get a direct backup of data as SST files (key-value pairs)

For other backup needs, refer to Backup and Restore Overview to choose an appropriate backup method.

Back Up Data - 图1Note

  • BR is only applicable to TiDB v3.1 or later releases.
  • Data that is backed up using BR can only be restored to TiDB instead of other databases.

Ad-hoc backup

Ad-hoc backup supports both full backup and incremental backup.

To get an Ad-hoc backup, you need to create a Backup Custom Resource (CR) object to describe the backup details. Then, 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.

This document provides an example about how to back up the data of the demo1 TiDB cluster in the test1 Kubernetes namespace to NFS. The following are the detailed steps.

Step 1: Prepare for an ad-hoc backup

  1. Download backup-rbac.yaml to the server that runs the backup task.

  2. Execute the following command to create the role-based access control (RBAC) resources in the test1 namespace:

    1. kubectl apply -f backup-rbac.yaml -n test1
  3. Make sure that the NFS server is accessible from your Kubernetes cluster, and you have configured TiKV to mount the same NFS server directory to the same local path as in backup jobs. To mount NFS for TiKV, refer to the configuration below:

    1. spec:
    2. tikv:
    3. additionalVolumes:
    4. # Specify volume types that are supported by Kubernetes, Ref: https://kubernetes.io/docs/concepts/storage/persistent-volumes/#types-of-persistent-volumes
    5. - name: nfs
    6. nfs:
    7. server: 192.168.0.2
    8. path: /nfs
    9. additionalVolumeMounts:
    10. # This must match `name` in `additionalVolumes`
    11. - name: nfs
    12. mountPath: /nfs
  4. For a TiDB version earlier than v4.0.8, you also need to complete the following preparation steps. For TiDB v4.0.8 or a later version, skip these preparation steps.

    1. Make sure that you have the SELECT and UPDATE privileges on the mysql.tidb table of the backup database so that the Backup CR can adjust the GC time before and after the backup.

    2. Create the backup-demo1-tidb-secret secret to store the account and password to access the TiDB cluster:

      1. kubectl create secret generic backup-demo1-tidb-secret --from-literal=password=${password} --namespace=test1

Step 2: Perform an ad-hoc backup

  1. Create the Backup CR, and back up cluster data to NFS as described below:

    1. kubectl apply -f backup-nfs.yaml

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

    1. ---
    2. apiVersion: pingcap.com/v1alpha1
    3. kind: Backup
    4. metadata:
    5. name: demo1-backup-nfs
    6. namespace: test1
    7. spec:
    8. # # backupType: full
    9. # # Only needed for TiDB Operator < v1.1.10 or TiDB < v4.0.8
    10. # from:
    11. # host: ${tidb-host}
    12. # port: ${tidb-port}
    13. # user: ${tidb-user}
    14. # secretName: backup-demo1-tidb-secret
    15. br:
    16. cluster: demo1
    17. clusterNamespace: test1
    18. # logLevel: info
    19. # statusAddr: ${status-addr}
    20. # concurrency: 4
    21. # rateLimit: 0
    22. # checksum: true
    23. # options:
    24. # - --lastbackupts=420134118382108673
    25. local:
    26. prefix: backup-nfs
    27. volume:
    28. name: nfs
    29. nfs:
    30. server: ${nfs_server_ip}
    31. path: /nfs
    32. volumeMount:
    33. name: nfs
    34. mountPath: /nfs

    When configuring backup-nfs.yaml, note the following:

    • If you want to back up data incrementally, you only need to specify the last backup timestamp --lastbackupts in spec.br.options. For the limitations of incremental backup, refer to Use BR to Back up and Restore Data.

    • spec.local refers to the configuration related to PVs. For more information about PV configuration, refer to Local storage fields.

    • Some parameters in spec.br are optional, such as logLevel, statusAddr, concurrency, rateLimit, checksum, and timeAgo. For more information about spec.br fields, refer to BR fields.

    • For v4.0.8 or a later version, BR can automatically adjust tikv_gc_life_time. You do not need to configure spec.tikvGCLifeTime and spec.from fields in the Backup CR.

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

  2. After creating the Backup CR, TiDB Operator automatically starts the backup task. You can use the following command to check the backup status:

    1. kubectl get bk -n test1 -owide

Backup CR examples

Back up data of all clusters

  1. ---
  2. apiVersion: pingcap.com/v1alpha1
  3. kind: Backup
  4. metadata:
  5. name: demo1-backup-nfs
  6. namespace: test1
  7. spec:
  8. # # backupType: full
  9. # # Only needed for TiDB Operator < v1.1.10 or TiDB < v4.0.8
  10. # from:
  11. # host: ${tidb-host}
  12. # port: ${tidb-port}
  13. # user: ${tidb-user}
  14. # secretName: backup-demo1-tidb-secret
  15. br:
  16. cluster: demo1
  17. clusterNamespace: test1
  18. local:
  19. prefix: backup-nfs
  20. volume:
  21. name: nfs
  22. nfs:
  23. server: ${nfs_server_ip}
  24. path: /nfs
  25. volumeMount:
  26. name: nfs
  27. mountPath: /nfs

Back up data of a single database

The following example backs up data of the db1 database.

  1. ---
  2. apiVersion: pingcap.com/v1alpha1
  3. kind: Backup
  4. metadata:
  5. name: demo1-backup-nfs
  6. namespace: test1
  7. spec:
  8. # # backupType: full
  9. # # Only needed for TiDB Operator < v1.1.10 or TiDB < v4.0.8
  10. # from:
  11. # host: ${tidb-host}
  12. # port: ${tidb-port}
  13. # user: ${tidb-user}
  14. # secretName: backup-demo1-tidb-secret
  15. tableFilter:
  16. - "db1.*"
  17. br:
  18. cluster: demo1
  19. clusterNamespace: test1
  20. local:
  21. prefix: backup-nfs
  22. volume:
  23. name: nfs
  24. nfs:
  25. server: ${nfs_server_ip}
  26. path: /nfs
  27. volumeMount:
  28. name: nfs
  29. mountPath: /nfs

Back up data of a single table

The following example backs up data of the db1.table1 table.

  1. ---
  2. apiVersion: pingcap.com/v1alpha1
  3. kind: Backup
  4. metadata:
  5. name: demo1-backup-nfs
  6. namespace: test1
  7. spec:
  8. # # backupType: full
  9. # # Only needed for TiDB Operator < v1.1.10 or TiDB < v4.0.8
  10. # from:
  11. # host: ${tidb-host}
  12. # port: ${tidb-port}
  13. # user: ${tidb-user}
  14. # secretName: backup-demo1-tidb-secret
  15. tableFilter:
  16. - "db1.table1"
  17. br:
  18. cluster: demo1
  19. clusterNamespace: test1
  20. local:
  21. prefix: backup-nfs
  22. volume:
  23. name: nfs
  24. nfs:
  25. server: ${nfs_server_ip}
  26. path: /nfs
  27. volumeMount:
  28. name: nfs
  29. mountPath: /nfs

Back up data of multiple tables using the table filter

The following example backs up data of the db1.table1 table and db1.table2 table.

  1. ---
  2. apiVersion: pingcap.com/v1alpha1
  3. kind: Backup
  4. metadata:
  5. name: demo1-backup-nfs
  6. namespace: test1
  7. spec:
  8. # # backupType: full
  9. # # Only needed for TiDB Operator < v1.1.10 or TiDB < v4.0.8
  10. # from:
  11. # host: ${tidb-host}
  12. # port: ${tidb-port}
  13. # user: ${tidb-user}
  14. # secretName: backup-demo1-tidb-secret
  15. tableFilter:
  16. - "db1.table1"
  17. - "db1.table2"
  18. br:
  19. cluster: demo1
  20. clusterNamespace: test1
  21. local:
  22. prefix: backup-nfs
  23. volume:
  24. name: nfs
  25. nfs:
  26. server: ${nfs_server_ip}
  27. path: /nfs
  28. volumeMount:
  29. name: nfs
  30. mountPath: /nfs

Scheduled full backup

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 a scheduled full backup

The steps to prepare for a scheduled full backup are the same as that of Prepare for an ad-hoc backup.

Step 2: Perform a scheduled full backup

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

    1. kubectl apply -f backup-schedule-nfs.yaml

    The content of backup-schedule-nfs.yaml is as follows:

    1. ---
    2. apiVersion: pingcap.com/v1alpha1
    3. kind: BackupSchedule
    4. metadata:
    5. name: demo1-backup-schedule-nfs
    6. namespace: test1
    7. spec:
    8. #maxBackups: 5
    9. #pause: true
    10. maxReservedTime: "3h"
    11. schedule: "*/2 * * * *"
    12. backupTemplate:
    13. # Only needed for TiDB Operator < v1.1.10 or TiDB < v4.0.8
    14. # from:
    15. # host: ${tidb_host}
    16. # port: ${tidb_port}
    17. # user: ${tidb_user}
    18. # secretName: backup-demo1-tidb-secret
    19. br:
    20. cluster: demo1
    21. clusterNamespace: test1
    22. # logLevel: info
    23. # statusAddr: ${status-addr}
    24. # concurrency: 4
    25. # rateLimit: 0
    26. # checksum: true
    27. local:
    28. prefix: backup-nfs
    29. volume:
    30. name: nfs
    31. nfs:
    32. server: ${nfs_server_ip}
    33. path: /nfs
    34. volumeMount:
    35. name: nfs
    36. mountPath: /nfs

    From the backup-schedule-nfs.yaml 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.

    • For the unique configuration of backupSchedule, refer to BackupSchedule CR fields.
    • backupTemplate specifies the configuration related to the cluster and remote storage, which is the same as the spec configuration of the Backup CR.
  2. After creating the scheduled full backup, use the following command to check the backup status:

    1. kubectl get bks -n test1 -owide

    Use the following command to check all the backup items:

    1. kubectl get bk -l tidb.pingcap.com/backup-schedule=demo1-backup-schedule-nfs -n test1

Delete the backup CR

If you no longer need the backup CR, refer to Delete the Backup CR.

Troubleshooting

If you encounter any problem during the backup process, refer to Common Deployment Failures.