7.1 處理 MySQL 備份

與 MySQL 備份相關的主要主題有三個

  • 備份設定檔:描述一般備份結構,包括儲存、排程和 MySQL Shell 傾印相關選項。定義設定檔是選擇性的,而設定檔會依名稱分隔。

  • 備份請求:要求備份會啟動一個新物件,並建立一個新 Pod 來執行備份。

  • 備份排程:定義為定期備份的 cron 表示式,或者在執行一次性備份時沒有排程。

另請參閱 第 8 章,MySQL 運算子自訂資源屬性,以取得所有 MySQLBackup 資源選項的清單。

使用 backupProfiles 的備份設定檔

備份設定檔是使用 backupProfiles 規格物件定義和重複使用於定期備份和一次性備份。設定檔會在 InnoDB 叢集規格物件中定義和呼叫,或者值可以在沒有設定檔的個別備份請求中定義。

如何建立備份

Kubernetes 的 MySQL 運算子透過定義相關聯的 dumpInstance 規格物件(包含 dumpOptionsstorage 規格物件),支援使用 MySQL Shell 的 dumpInstance() 命令

  • 選用的 dumpOptions 值是直接傳遞至 MySQL Shell 的 DumpInstance() 函數的鍵值配對字典。請參閱 執行個體傾印公用程式、結構描述傾印公用程式和表格傾印公用程式,以取得相關選項的清單。

    Kubernetes 的 MySQL 運算子預設會新增定義,例如根據系統宣告的 CPU 數量來定義 threads,但這些值可以覆寫。

  • storage 組態規格提供 Kubernetes 的 MySQL 運算子 8.0.29 的兩個選項:persistentVolumeClaimociObjectStorage (OCI 是指 Oracle Cloud Infrastructure)。

    注意

    限制:Kubernetes 的 MySQL 運算子 8.0.29 目前不提供 persistentVolumeClaim 的還原功能,且 ociObjectStorage 的使用僅限於 Oracle Cloud Infrastructure (OCI)。

  • backupSchedules schedule 會利用 Kubernetes 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

此範例需要一個名為「myexample-pvc」的 PersistentVolumeClaim 定義;請參閱官方 Kubernetes 永續磁碟區文件以取得 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 叢集定義會使用名為「mypwds」的密碼,以供其根使用者使用,例如

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

建立範例 InnoDB 叢集之後,您可以使用現有的設定檔執行一次性備份,例如

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

執行此操作會建立一個 Pod,其名稱類似於 a-cool-one-off-backup-20220330-215635-t6thv,該 Pod 會執行備份,並在備份操作之後保持在「已完成」狀態。

使用 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 密碼看起來類似於

    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) 相同,但可能具有不同權限的不同使用者,例如對 OS 沒有寫入權限。

複製

可以使用備份或透過使用 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

donorpwds 密碼包含名為 rootPassword 的單一欄位,因此舉例來說,您可以在建立原始叢集時重複使用主要 secretName (在範例中名為 mypwds)。此方法會利用 MySQL 的複製外掛程式,因此適用標準限制 (例如需要相同的 MySQL 版本)。理論上,複製也可以用於建立備份。