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
Create Restored Cluster
Create a new cluster with restoration configuration referencing the backup.
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":{"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:
- 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).
- 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.
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
Example 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.