7.1 处理MySQL备份

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

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

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

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

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

使用backupProfiles进行备份配置文件

使用backupProfiles规范对象定义和重用备份配置文件,用于定期备份和一次性备份。在InnoDB集群规范对象中定义和调用配置文件,或者可以在不使用配置文件的情况下在单个备份请求中定义值。

如何创建备份

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

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

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

  • 在Kubernetes 8.0.29版MySQL Operator中,storage配置规范提供了两个选项:persistentVolumeClaimociObjectStorage(OCI指的是Oracle云基础设施)。

    注意

    限制:在Kubernetes 8.0.29版MySQL Operator中,persistentVolumeClaim尚不支持还原功能,而ociObjectStorage的使用特定于Oracle云基础设施(OCI)。

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

PersistentVolumeClaim计划备份示例

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

注意

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

Press CTRL+C to copy
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

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

Press CTRL+C to copy
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集群定义为其root用户使用名为“mypwds”的密钥,例如

Press CTRL+C to copy
$> kubectl create secret generic mypwds \ --from-literal=rootUser=root \ --from-literal=rootHost=% \ --from-literal=rootPassword="sakila"

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

Press CTRL+C to copy
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,该Pod执行备份,并在备份操作后保持在完成状态。

使用OciObjectStorage

使用相同的示例,但针对Oracle云基础设施(OCI)而不是PVC,将dumpInstance.storage从PrivateVolumeClaim修改为类似于以下内容的ociObjectStorage对象

Press CTRL+C to copy
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密钥类似于

Press CTRL+C to copy
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命令行工具一起使用。

Press CTRL+C to copy
$> 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集群

Press CTRL+C to copy
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实例

Press CTRL+C to copy
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

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