KubeBlocks
BlogsKubeBlocks Cloud
Overview
QuickStart

Topologies

MySQL Semi-Synchronous Cluster
MySQL Cluster with ProxySQL
MySQL Group Replication Cluster
MySQL Group Replication with ProxySQL
MySQL Cluster with Orchestrator
MySQL with Orchestrator & ProxySQL

Operations

Lifecycle Management
Vertical Scaling
Horizontal Scaling
Volume Expansion
Manage MySQL Services
Minor Version Upgrade
Modify MySQL Parameters
Planned Switchover in MySQL
Decommission MySQL Replica
Recovering MySQL Replica

Backup And Restores

Create BackupRepo
Create Full Backup
Scheduled Backups
Scheduled Continuous Backup
Restore MySQL Cluster
Restore with PITR

Custom Secret

Custom Password
Custom Password Policy

TLS

MySQL Cluster with TLS
MySQL Cluster with User-Provided TLS
MySQL Cluster with mTLS

Monitoring

Observability for MySQL Clusters

Advanced Pod Management

Custom Scheduling Policies
Custom Pod Resources
Pod Management Parallelism
Using OnDelete for Controlled Pod Updates
Gradual Rolling Update
  1. Prerequisites
  2. Verify Backup Status
  3. Restore the Backup Using Cluster Annotation
    1. Retrieve Encrypted System Accounts
    2. Create Restored Cluster
    3. Monitor Restoration Progress
  4. Perform Restoration via Ops API
    1. Step 1: Create a Restore Request
    2. Step 2: Monitor Ops Progress
  5. Verify the Restored Cluster Status
  6. Cleanup
  7. Summary

Restore a MySQL Cluster from Backup

This guide explains how to restore a new MySQL cluster from an existing backup in KubeBlocks. It showcases two methods: using Cluster Annotation or the Ops API.

The Ops API provides enhanced control and progress monitoring capabilities for restore operations, acting as a wrapper around the Cluster Annotation.

Prerequisites

  • KubeBlocks Environment:
    • KubeBlocks operator and required CRDs installed.
    • kubectl configured to access your Kubernetes cluster.
  • Existing Backup:
    • A valid backup named example-mysql-backup-backup in the 'demo' namespace.

Verify Backup Status

Before restoring, ensure that a full backup is available. The restoration process will use this backup to create a new MySQL cluster.

Run the following command to check the backup status:

kubectl get backup -n demo

Expected Output:

NAME                           POLICY                                      METHOD       REPO      STATUS      TOTAL-SIZE   DURATION   DELETION-POLICY   CREATION-TIME          COMPLETION-TIME        EXPIRATION-TIME
example-mysql-cluster-backup   example-mysql-cluster-mysql-backup-policy   xtrabackup   s3-repo   Completed   1633717      18s        Delete            2025-03-07T03:25:22Z   2025-03-07T03:25:40Z

Restore the Backup Using Cluster Annotation

Retrieve Encrypted System Accounts

Extract encrypted credentials for system accounts from the backup metadata:

kubectl get backup -n demo example-mysql-cluster-backup -o jsonpath='{.metadata.annotations.kubeblocks\.io/encrypted-system-accounts}'

Expected Output:

{"mysql":{"kbadmin":"+UbL/HmrnDShZCdoZxpAWxTHT48VKrJBhBqnNDjvwscW2YBBj8biYf1REDA=","kbdataprotection":"zycglr6K6oeRpZlZBeFeTUoxGm+eSIC7bNotnz4UmFCLH8O77JsGM9gf3js=","kbmonitoring":"qX66By8D2ZJdMh5Pe9u149Fev4wVyNchi+U9QkDqPsW6hoVrbew/srZUEmE=","kbprobe":"n2VKC65TGg4N1GVr9en6EOZ0MQEf/DyDdQ0qeK9TbMRO2QMaJvKlpW9Bm5w=","kbreplicator":"1BOeBExnprxNshSCAHVGmBQ8dk+72edxQb1P2/2L2hD4M39cRLhoB60nW9Y=","proxysql":"u6NRl02g7+do5z1SVjhLTUYFqNp860wMQ1pfkH1JpsB/RKxIU6Gds2wNu7I=","root":"0YQW649uCaf0QqgAVDMX8OtNbatKYNlZE1aNRD8C2J8a9xHqPi8="}}

Create Restored Cluster

Create a new cluster with restoration configuration referencing the backup, set encryptedSystemAccounts with the encrypted system account credentials got from last step.

Note: Use " to escape double quotes in the JSON string.

kubectl apply -f - <<EOF
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
  name: example-mysql-cluster-restored
  namespace: demo
  annotations:
    kubeblocks.io/restore-from-backup: '{"mysql":{"encryptedSystemAccounts":"{\"kbadmin\":\"+UbL/HmrnDShZCdoZxpAWxTHT48VKrJBhBqnNDjvwscW2YBBj8biYf1REDA=\",\"kbdataprotection\":\"zycglr6K6oeRpZlZBeFeTUoxGm+eSIC7bNotnz4UmFCLH8O77JsGM9gf3js=\",\"kbmonitoring\":\"qX66By8D2ZJdMh5Pe9u149Fev4wVyNchi+U9QkDqPsW6hoVrbew/srZUEmE=\",\"kbprobe\":\"n2VKC65TGg4N1GVr9en6EOZ0MQEf/DyDdQ0qeK9TbMRO2QMaJvKlpW9Bm5w=\",\"kbreplicator\":\"1BOeBExnprxNshSCAHVGmBQ8dk+72edxQb1P2/2L2hD4M39cRLhoB60nW9Y=\",\"proxysql\":\"u6NRl02g7+do5z1SVjhLTUYFqNp860wMQ1pfkH1JpsB/RKxIU6Gds2wNu7I=\",\"root\":\"0YQW649uCaf0QqgAVDMX8OtNbatKYNlZE1aNRD8C2J8a9xHqPi8=\"}"
,"name":"example-mysql-cluster-backup","namespace":"demo","volumeRestorePolicy":"Parallel"}}'
spec:
  terminationPolicy: WipeOut
  componentSpecs:
    - name: mysql
      componentDef: "mysql-8.0"
      serviceVersion: 8.0.35
      disableExporter: false
      replicas: 2
      resources:
        limits:
          cpu: '0.5'
          memory: 0.5Gi
        requests:
          cpu: '0.5'
          memory: 0.5Gi
      volumeClaimTemplates:
        - name: data
          spec:
            storageClassName: ""
            accessModes:
              - ReadWriteOnce
            resources:
              requests:
                storage: 20Gi
EOF

Monitor Restoration Progress

Watch Pod Initialization:

kubectl get pods -n demo -w

Expected Workflow:

  1. Data Preparation Pods:
restore-preparedata-XXXXX-<hash>   0/1     Init:0/1   0          6s
restore-preparedata-XXXXX-<hash>   1/1     Running    0          12s
restore-preparedata-XXXXX-<hash>   0/1     Completed 0          20s

These pods copy backup data to Persistent Volumes (PVCs).

  1. MySQL Cluster Pods:
example-mysql-cluster-restored-mysql-0     0/4     Pending        0          0s
example-mysql-cluster-restored-mysql-0     4/4     Running        0          20s

Pods initialize with restored data and start MySQL services.

Perform Restoration via Ops API

Alternatively, use the Ops API to initiate the restoration process:

Step 1: Create a Restore Request

kubectl apply -f - <<EOF
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: example-mysql-cluster-restore
  namespace: demo
spec:
  clusterName: example-mysql-cluster-restored
  force: false
  restore:
    backupName: example-mysql-cluster-backup
    backupNamespace: demo
  type: Restore
EOF

Step 2: Monitor Ops Progress

Monitor the progress of the restoration operation:

kubectl get ops example-mysql-cluster-restore -n demo -w

Expected Output:

NAME                            TYPE      CLUSTER                         STATUS    PROGRESS   AGE
example-mysql-cluster-restore   Restore   example-mysql-cluster-restored   Running   -/-        55s
example-mysql-cluster-restore   Restore   example-mysql-cluster-restored   Succeed   -/-        3m3s

Verify the Restored Cluster Status

After the restoration process is complete, verify the status of the newly restored cluster:

kubectl get cluster example-mysql-cluster-restored -n demo

Expected Output:

NAME                    CLUSTER-DEFINITION   TERMINATION-POLICY   STATUS    AGE
example-mysql-cluster-restored                        WipeOut               Running   3m2s

Cleanup

To remove all created resources, delete the MySQL cluster along with its namespace:

kubectl delete cluster example-mysql-cluster -n demo
kubectl delete cluster example-mysql-cluster-restored -n demo
kubectl delete ns demo

Summary

This guide demonstrated how to restore a MySQL cluster from an existing backup using either the Cluster Annotation or Ops API. By following these steps, you can efficiently restore critical data to a new MySQL cluster in KubeBlocks.

© 2025 ApeCloud PTE. Ltd.