github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/docs/developer_docs/integration/backup-and-restore.md (about) 1 --- 2 title: Backup and restore 3 description: Create backup and restore 4 keywords: [add-on, backup, restore] 5 sidebar_position: 3 6 sidebar_label: Backup and restore 7 --- 8 9 import Tabs from '@theme/Tabs'; 10 import TabItem from '@theme/TabItem'; 11 12 # Backup and restore 13 14 This tutorial takes Oracle MySQL as an example and introduces how to create backups and restore data in KubeBlocks. The full PR can be found at [Learn KubeBlocks Add-on](https://github.com/apecloud/learn-kubeblocks-addon/tree/main/tutorial-2-backup-restore/). 15 16 Different classification results in different types of backups, including volume snapshot backup and file backup, data backup and log backup, full backup and incremental backup, as well as scheduled backup and on-demand backup, which differ in terms of their methods, contents, volumes, and timing. 17 18 This tutorial illustrates how to realize the most frequently used snapshot backup and file backup in KubeBlocks. 19 20 - Snapshot backup relies on the volume snapshot capability of Kubernetes. 21 - File backup relies on backup tools provided by database engines. 22 23 Now take a quick look at the basic concepts of KubeBlocks in the table below, which also are elaborated in the following tutorial. 24 25 :paperclip: Table 1. Terminology 26 27 | Term | Description | Scope | 28 | :--- | :---------- | :---- | 29 | Backup | Backup object <br /> It defines the entity to be backed up. | Namespace | 30 | BackupPolicy | Backup policy <br /> It defines the policy for each backup type, such as scheduling, retention time, and tools. | Namespace | 31 | BackupTool | Backup tool <br /> It is the carrier of backup tools in KubeBlocks and should realize the backup and restoration logic of corresponding tools. | Cluster | 32 | BackupPolicyTemplate | Template of backup policy <br /> It is the bridge between the backup and ClusterDefinition. When creating a cluster, KubeBlocks automatically generates a default backup policy for each cluster according to BackupPolicyTemplate. | Cluster | 33 34 ## Before you start 35 36 - Finish the configuration in [Add an add-on to KubeBlocks](./how-to-add-an-add-on.md). 37 - Grasp the basics of K8s concepts, such as Pod, PVC, PV, VolumeSnapshot, etc. 38 39 ## Step 1. Prepare environment 40 41 1. Install CSI Driver. 42 43 Since volume snapshot is only available for CSI Drivers, make sure your Kubernetes is properly configured. 44 45 - For the localhost, you can quickly install `csi-host-driver` by KubeBlocks add-on. 46 47 ```bash 48 kbcli addon enable csi-hostpath-driver 49 ``` 50 51 - For a cloud environment, configure the corresponding CSI Driver based on your environment. 52 53 2. Set the `storageclass` to the default to make it easier to create clusters. 54 55 ```bash 56 kubectl get sc 57 > 58 NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE 59 csi-hostpath-sc (default) hostpath.csi.k8s.io Delete WaitForFirstConsumer true 35s 60 ``` 61 62 ## Step 2. Specify a volume type 63 64 Specify a volume type in the ClusterDefinition and it is required. 65 66 ```yaml 67 componentDefs: 68 - name: mysql-compdef 69 characterType: mysql 70 workloadType: Stateful 71 service: 72 ports: 73 - name: mysql 74 port: 3306 75 targetPort: mysql 76 volumeTypes: 77 - name: data 78 type: data 79 ``` 80 81 `volumeTypes` is used to specify volume type and name. 82 83 There are mainly two kinds of volume types (`volumeTypes.type`): 84 85 - `data`: Data information 86 - `log`: Log information 87 88 KubeBlocks supports different backup methods for data and logs. In this tutorial, only data volume information is configured. 89 90 ## Step 3. Add backup configuration 91 92 Prepare `BackupPolicyTemplate.yml` and `BackupTool.yml` to add the backup configuration. 93 94 ### BackupPolicy template 95 96 It is a template of backup policy, and covers: 97 98 1. Which cluster components to back up 99 2. Whether the backups are scheduled 100 3. How to set up snapshot backup 101 4. How to set up file backup 102 103 ```yaml 104 apiVersion: apps.kubeblocks.io/v1alpha1 105 kind: BackupPolicyTemplate 106 metadata: 107 name: oracle-mysql-backup-policy-template 108 labels: 109 clusterdefinition.kubeblocks.io/name: oracle-mysql # Specify scope through labels (Required) 110 spec: 111 clusterDefinitionRef: oracle-mysql # Specify the scope, indicating which ClusterDef generates the cluster 112 backupPolicies: 113 - componentDefRef: mysql-compdef # Specify the scope, indicating which component is involved 114 schedule: # Specify the timing of scheduled backups and startup status 115 snapshot: 116 enable: true # Enable scheduled snapshot backups 117 cronExpression: "0 18 * * *" 118 datafile: # Disable scheduled datafile backups 119 enable: false 120 cronExpression: "0 18 * * *" 121 snapshot: # Snapshot backup, which keeps the latest 5 versions by default 122 backupsHistoryLimit: 5 123 datafile: # Datafile backup which depends on backup tools 124 backupToolName: oracle-mysql-xtrabackup 125 ``` 126 127 If a scheduled task is enabled, KubeBlocks creates a CronJob in the background. 128 129 After a new cluster is created, KubeBlocks discovers the corresponding template name by `clusterdefinition.kubeblocks.io/name` and creates the corresponding BackupPolicy. 130 131 :::note 132 133 If you have added `BackupPolicyTemplate` but there is no default BackupPolicy for the new cluster, check whether the following requirements: 134 135 1. Whether `ClusterDefinitionRef` is correct. 136 2. Whether the `BackupPolicyTemplate` label is correct. 137 3. Whether there are multiple BackupPolicyTemplates. If yes, mark one as the default template using annotations. 138 139 ```yaml 140 annotations: 141 dataprotection.kubeblocks.io/is-default-policy-template: "true" 142 ``` 143 144 ::: 145 146 ### BackupTool 147 148 :::note 149 150 `BackupTool` mainly serves datafile backup. If you only need snapshot backups, there is no need to configure BackupTool. 151 152 ::: 153 154 `BackTool.yml` describes the detailed execution logic of a backup tool and mainly serves datafile backup. It should cover: 155 156 1. Image of backup tools 157 2. Scripts of backup 158 3. Scripts of restore 159 160 ```yaml 161 apiVersion: dataprotection.kubeblocks.io/v1alpha1 162 kind: BackupTool 163 metadata: 164 name: oracle-mysql-xtrabackup 165 labels: 166 spec: 167 image: docker.io/perconalab/percona-xtrabackup:8.0.32 # Back up via xtrabackup 168 env: # Inject the name of dependent environment variables 169 - name: DATA_DIR 170 value: /var/lib/mysql 171 physical: 172 restoreCommands: # Restore commands 173 - sh 174 - -c 175 ... 176 backupCommands: # Backup commands 177 - sh 178 - -c 179 ... 180 ``` 181 182 The configuration of `BackupTool` is closely related to the tools used. 183 184 For example, if you back up via Percona Xtrabackup, you need to fill in scripts in `backupCommands` and `restoreCommands`. 185 186 ## Step 4. Back up and restore a cluster 187 188 With everything ready, try to back up a cluster and restore data to a new cluster. 189 190 ### 4.1 Create a cluster 191 192 Since `BackupPolicyTemplate` has been added, after a cluster is created, KubeBlocks can discover the backup policy and create a `BackupPolicy` for this cluster. 193 194 1. Create a cluster. 195 196 <Tabs> 197 198 <TabItem value="kbcli" label="kbcli" default> 199 200 ```bash 201 kbcli cluster create mycluster --cluster-definition oracle-mysql 202 ``` 203 204 </TabItem> 205 206 <TabItem value="Helm" label="Helm"> 207 208 ```bash 209 helm install mysql ./tutorial-2-backup-restore/oracle-mysql 210 ``` 211 212 </TabItem> 213 214 </Tabs> 215 216 2. View the backup policy of this cluster. 217 218 ```bash 219 kbcli cluster list-backup-policy mycluster 220 ``` 221 222 ### 4.2 Snapshot backups 223 224 ```bash 225 kbcli cluster backup mycluster --type snapshot 226 ``` 227 228 `type` specifies the backup type, indicating whether it is a snapshot or datafile. 229 230 If there are multiple backup policies, specify it with the `--policy` flag. 231 232 ### 4.3 Datafile backups 233 234 KubeBlocks supports backup to local storage and cloud object storage. The following is an example of backing up to your localhost. 235 236 1. Modify BackupPolicy and specify the PVC name. 237 238 As shown below in `spec.datafile.persistentVolumeClaim.name`, specify the PVC name. 239 240 ```yaml 241 spec: 242 datafile: 243 backupToolName: oracle-mysql-xtrabackup 244 backupsHistoryLimit: 7 245 persistentVolumeClaim: 246 name: mycluster-backup-pvc 247 createPolicy: IfNotPresent 248 initCapacity: 20Gi 249 ``` 250 251 2. Set `--type` to `datafile`. 252 253 ```bash 254 kbcli cluster backup mycluster --type datafile 255 ``` 256 257 ### 4.4 Create a cluster from backups 258 259 1. Check the backups. 260 261 ```bash 262 kbcli cluster list-backups 263 ``` 264 265 2. Select a backup and create a cluster. 266 267 ```bash 268 kbcli cluster restore <clusterName> --backup <backup-name> 269 ``` 270 271 And a new cluster is created. 272 273 :::caution 274 275 It should be noted that some databases only create the root account and password during the first initialization. 276 277 Therefore, although a new root account and password are created when restoring a cluster from backups, they are not effective. You still need to log in with the root account and password of the original cluster. 278 279 ::: 280 281 ## Reference 282 283 - For more details on the backup and restore function of KubeBlocks, refer to [Backup and Restore](./../../user_docs/backup-and-restore/introduction.md). 284 285 ## Appendix 286 287 ### A.1 Cluster data protection policies 288 289 KubeBlocks provides various data protection policies for stateful clusters, each offering various data options. Try the following scenarios: 290 291 1. If you delete a cluster using `kbcli cluster delete`, will the backup still be available? 292 2. If you change the `terminationPolicy` of a cluster to `WipeOut` and then delete it, will the backup still be available? 293 3. If you change the `terminationPolicy` of a cluster to `DoNotTerminate` and then delete it, what will happen? 294 295 :::note 296 297 Refer to the data protection policies of KubeBlocks via [Termination Policy](./../../user_docs/kubeblocks-for-mysql/cluster-management/delete-mysql-cluster.md#termination-policy). 298 299 ::: 300 301 ### A.2 Monitor backup progress 302 303 In [Step 4](#step-4-how-to-back-uprestore-a-cluster), you have created a backup using the backup subcommand. 304 305 ```bash 306 kbcli cluster backup mycluster --type snapshot 307 ``` 308 309 A new backup object is generated and you can view the progress by running the `describe-backup` subcommand. 310 311 ```bash 312 kbcli cluster describe-backup <your-back-up-name> 313 ```