6.1 处理 MySQL 备份

与 MySQL 备份相关的三个主要主题:

  • 备份配置文件:描述了包括存储、计划和 MySQL Shell 转储相关选项的一般备份结构。定义配置文件是可选的,配置文件按名称分隔。

  • 备份请求:请求备份会启动一个新对象,该对象会创建一个新的 Pod 来执行备份。

  • 备份计划:定义为定期备份的 cron 表达式,或者在执行一次性备份时没有计划。

另请参阅第 7 章,MySQL 运算符自定义资源属性以获取所有MySQLBackup资源选项的列表。

使用 backupProfiles 备份配置文件

使用backupProfiles 规范对象 定义备份配置文件并将其重新用于常规备份和一次性备份 。从 InnoDB Cluster 规范对象中定义和调用配置文件,或者可以在没有配置文件的情况下从单个备份请求中定义值。

如何创建备份

MySQL Operator for Kubernetes通过定义 包含 和规范对象的关联dumpInstance规范对象,支持使用 MySQL Shell 的dumpInstance()命令 : dumpOptionsstorage

  • 可选dumpOptions值是直接传递给 MySQL Shell 的 DumpInstance() 函数的键值对字典。有关相关选项的列表, 请参阅 实例转储实用程序、架构转储实用程序和表转储实用程序。

    MySQL Operator for Kubernetes 默认添加了定义,比如 threads根据系统声明的 CPU 数量来定义;但这些值可以被覆盖。

  • storageMySQL Operator for Kubernetes 8.0.29 开始,配置规范提供了两个选项: persistentVolumeClaimociObjectStorage(OCI 指的是 Oracle Cloud Infrastructure)。

    笔记

    限制:自 MySQL Operator for Kubernetes 8.0.29 起,恢复功能不适用于 persistentVolumeClaim,并且 ociObjectStorage 的使用特定于 Oracle Cloud Infrastructure (OCI)。

  • 利用backupSchedules scheduleKubernetes CronJob 控制器进行定期备份。

PersistentVolumeClaim 计划备份示例

此示例使用 PersistentVolumeClaim (PVC),设置每日备份计划,并在 backupProfiles 对象中定义名为“myfancyprofile”的备份配置文件。

笔记

此示例定义单个 backupProfile 和计划,但可以根据需要定义多个配置文件和计划。例如,除了每晚的完整备份之外,易失性表可能还有每小时的备份。

apiVersion: mysql.oracle.com/v2
kind: InnoDBCluster
metadata:
  name: mycluster
spec:
  instances: 3
  router:
    instances: 1
  secretName: mypwds
  tlsUseSelfSigned: true
  backupProfiles:       
  - name: myfancyprofile  # Embedded backup profile
    dumpInstance:         # MySQL Shell Dump
      dumpOptions:
        excludeTables: "[world.country]" # Example to exclude one table 
      storage:
        persistentVolumeClaim:
          claimName: myexample-pvc # store to this pre-existing PVC
      backupSchedules:
        - name: mygreatschedule
          schedule: "0 0 * * *" # Daily, at midnight
          backupProfileName:  myfancyprofile # reference the desired backupProfiles's name 
          enabled: true # backup schedules can be temporarily disabled

这个例子需要一个PersistentVolumeClaim 名为“myexample-pvc”的定义;有关详细信息,请参阅官方 Kubernetes Persistent Volumes文档 PersistentVolumeClaim。一个简单的例子:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: myexample-pv
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /tmp
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: myexample-pvc
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

示例“mycluster”InnoDB Cluster 定义为其根用户使用名为“mypwds”的秘密,例如:

$> kubectl create secret generic mypwds \
        --from-literal=rootUser=root \
        --from-literal=rootHost=% \
        --from-literal=rootPassword="sakila"

创建示例 InnoDB Cluster 后,您可能希望使用现有配置文件执行一次性备份,例如:

apiVersion: mysql.oracle.com/v2
kind: MySQLBackup
metadata:
    name: a-cool-one-off-backup
spec:
  clusterName: mycluster
  backupProfileName: myfancyprofile

执行此操作会创建一个名称类似于 a-cool-one-off-backup-20220330-215635-t6thv 的 pod 来执行备份,并且在备份操作后保持 Completed 状态。

使用 OciObjectStorage

使用相同的示例,但对于 Oracle Cloud Infrastructure (OCI) 而不是 PVC,修改 dumpInstance。storage 从 PrivateVolumeClaim 到 ociObjectStorage 对象类似于:

    dumpInstance:
      storage:
        ociObjectStorage:
          prefix: someprefix         # a prefix (directory) used for ObjectStorage
          bucketName: bucket         # the ObjectStorage bucket
          credentials: backup-apikey # a secret with credentials ...

此 OCI 示例中使用的 backup-apikey secret 类似于:

    apiVersion: v1
    kind: Secret
    type: Opaque
    metadata:
      name: backup-apikey
    stringData:
      fingerprint: 06:e9:e1:c6:e5:df:81:f3:......
      passphrase: ....
      privatekey: |
        -----BEGIN RSA PRIVATE KEY-----
        MIIEogIBAAKCAQEAwmQ1JGOGUBNwyJuq4msGpBfK24toKrWaqAkbZ1Z/XLOFLvEE
       ....
      region: us-ashburn-1..
      tenancy: ocid1.tenancy...
      user: ocid1.user.....

创建秘密的示例方法;值可以在从 OCI 下载的配置文件中找到,该文件与 OCI 命令行工具一起使用。

$> kubectl create secret generic <secret_name> \
        --from-literal=user=<userid> \
        --from-literal=fingerprint=<fingerprint> \
        --from-literal=tenancy=<tenancy> \
        --from-literal=region=<region> \
        --from-literal=passphrase=<passphrase> \
        --from-file=privatekey=<path_to_api_key.pem>

使用配置文件 ( backupProfileName) 是可选的,因此它可能看起来像以下具有相同设置的内容。此示例从 ociObjectStorage 恢复到新的 InnoDB 集群:

    apiVersion: mysql.oracle.com/v2
    kind: InnoDBCluster
    metadata:
      name: newcluster
    spec:
      instances: 3
      router:
        instances: 1
      secretName: newpwds
      tlsUseSelfSigned: true
      baseServerId: 2000
      initDB:
        dump:
          name: some-name
          storage:
            ociObjectStorage:
              prefix: someprefix
              bucketName: bucket
              credentials: restore-apikey

秘密 ( restore-apikey) 可能与备份示例 ( backup-apikey) 相同,但可能是具有不同权限的不同用户,例如没有对操作系统的写入权限。

克隆

iniDB可以使用备份或通过使用及其 donorURL选项 克隆现有且正在运行的 MySQL 实例来初始化数据:

apiVersion: mysql.oracle.com/v2
kind: InnoDBCluster
metadata:
  name: copycluster
spec:
  instances: 1
  secretName: pwds
  tlsUseSelfSigned: true
  initDB:
    clone:
      donorUrl: root@mycluster-0.mycluster-instances.testns.svc.cluster.local:3306
      secretKeyRef:
        name: donorpwds

secret 包含一个名为 rootPassword的donorpwds字段,因此例如您可以重用创建原始集群时使用的主要 secretName( mypwds在示例中命名)。这利用 了MySQL 的克隆插件,因此适用标准限制(例如需要相同的 MySQL 版本)。理论上,克隆也可以用于创建备份。