|
| 1 | +#!/bin/bash |
| 2 | +# Step 01: Create Velero backup/restore strategies for VMInstance and VMDisk |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 6 | +source "$SCRIPT_DIR/00-helpers.sh" |
| 7 | + |
| 8 | +print_header "Step 1: Create Velero Strategies" |
| 9 | + |
| 10 | +log_step "Creating VMInstance strategy..." |
| 11 | +log_command "kubectl apply -f - (Velero strategy: vminstance-strategy)" |
| 12 | + |
| 13 | +kubectl apply -f - <<'EOF' |
| 14 | +apiVersion: strategy.backups.cozystack.io/v1alpha1 |
| 15 | +kind: Velero |
| 16 | +metadata: |
| 17 | + name: vminstance-strategy |
| 18 | +spec: |
| 19 | + template: |
| 20 | + # Symmetric restore filters: kubevirt-velero-plugin requires launcher pods in the backup, |
| 21 | + # but restore orLabelSelectors limit what is applied from the tarball (e.g. skip another |
| 22 | + # VM's pod that Velero added via PVC item action). VMDisk OR branches are appended by |
| 23 | + # the controller from backup.status.underlyingResources or the Velero backup annotation. |
| 24 | + restoreSpec: |
| 25 | + existingResourcePolicy: update |
| 26 | + includedNamespaces: |
| 27 | + - '{{ .Application.metadata.namespace }}' |
| 28 | + orLabelSelectors: |
| 29 | + - matchLabels: |
| 30 | + app.kubernetes.io/instance: 'vm-instance-{{ .Application.metadata.name }}' |
| 31 | + - matchLabels: |
| 32 | + apps.cozystack.io/application.kind: '{{ .Application.kind }}' |
| 33 | + apps.cozystack.io/application.name: '{{ .Application.metadata.name }}' |
| 34 | + includedResources: |
| 35 | + - helmreleases.helm.toolkit.fluxcd.io |
| 36 | + - virtualmachines.kubevirt.io |
| 37 | + - virtualmachineinstances.kubevirt.io |
| 38 | + - pods |
| 39 | + - persistentvolumeclaims |
| 40 | + - configmaps |
| 41 | + - secrets |
| 42 | + - controllerrevisions.apps |
| 43 | + includeClusterResources: false |
| 44 | + excludedResources: |
| 45 | + # Required to avoid conflict with restored DV from HR VMDisk |
| 46 | + - datavolumes.cdi.kubevirt.io |
| 47 | +
|
| 48 | + spec: # see https://velero.io/docs/v1.17/api-types/backup/ |
| 49 | + includedNamespaces: |
| 50 | + - '{{ .Application.metadata.namespace }}' |
| 51 | + orLabelSelectors: |
| 52 | + # VM resources (VirtualMachine, DataVolume, PVC, etc.) |
| 53 | + - matchLabels: |
| 54 | + app.kubernetes.io/instance: 'vm-instance-{{ .Application.metadata.name }}' |
| 55 | + # HelmRelease (the Cozystack app object) |
| 56 | + - matchLabels: |
| 57 | + apps.cozystack.io/application.kind: '{{ .Application.kind }}' |
| 58 | + apps.cozystack.io/application.name: '{{ .Application.metadata.name }}' |
| 59 | + includedResources: |
| 60 | + - helmreleases.helm.toolkit.fluxcd.io |
| 61 | + - virtualmachines.kubevirt.io |
| 62 | + - virtualmachineinstances.kubevirt.io |
| 63 | + # Required by kubevirt-velero-plugin for running VMs ("launcher pod must be in backup"). |
| 64 | + - pods |
| 65 | + # Required by kubevirt-velero-plugin requires DV to be in backup of VM, but it excludes in restores |
| 66 | + - datavolumes.cdi.kubevirt.io |
| 67 | + - persistentvolumeclaims |
| 68 | + - configmaps |
| 69 | + - secrets |
| 70 | + - controllerrevisions.apps |
| 71 | + includeClusterResources: false |
| 72 | + storageLocation: '{{ .Parameters.backupStorageLocationName }}' |
| 73 | + volumeSnapshotLocations: |
| 74 | + - '{{ .Parameters.backupStorageLocationName }}' |
| 75 | + snapshotVolumes: true |
| 76 | + snapshotMoveData: true |
| 77 | + ttl: 720h0m0s |
| 78 | + itemOperationTimeout: 24h0m0s |
| 79 | +EOF |
| 80 | + |
| 81 | +log_success "VMInstance strategy created" |
| 82 | + |
| 83 | +separator |
| 84 | + |
| 85 | +log_step "Creating VMDisk strategy..." |
| 86 | +log_command "kubectl apply -f - (Velero strategy: vmdisk-strategy)" |
| 87 | + |
| 88 | +kubectl apply -f - <<'EOF' |
| 89 | +apiVersion: strategy.backups.cozystack.io/v1alpha1 |
| 90 | +kind: Velero |
| 91 | +metadata: |
| 92 | + name: vmdisk-strategy |
| 93 | +spec: |
| 94 | + template: |
| 95 | + restoreSpec: |
| 96 | + existingResourcePolicy: update |
| 97 | + includedNamespaces: |
| 98 | + - '{{ .Application.metadata.namespace }}' |
| 99 | + orLabelSelectors: |
| 100 | + - matchLabels: |
| 101 | + app.kubernetes.io/instance: 'vm-disk-{{ .Application.metadata.name }}' |
| 102 | + - matchLabels: |
| 103 | + apps.cozystack.io/application.kind: '{{ .Application.kind }}' |
| 104 | + apps.cozystack.io/application.name: '{{ .Application.metadata.name }}' |
| 105 | + includedResources: |
| 106 | + - helmreleases.helm.toolkit.fluxcd.io |
| 107 | + - persistentvolumeclaims |
| 108 | + - configmaps |
| 109 | + includeClusterResources: false |
| 110 | +
|
| 111 | + spec: |
| 112 | + includedNamespaces: |
| 113 | + - '{{ .Application.metadata.namespace }}' |
| 114 | + orLabelSelectors: |
| 115 | + - matchLabels: |
| 116 | + app.kubernetes.io/instance: 'vm-disk-{{ .Application.metadata.name }}' |
| 117 | + - matchLabels: |
| 118 | + apps.cozystack.io/application.kind: '{{ .Application.kind }}' |
| 119 | + apps.cozystack.io/application.name: '{{ .Application.metadata.name }}' |
| 120 | + includedResources: |
| 121 | + - helmreleases.helm.toolkit.fluxcd.io |
| 122 | + - persistentvolumeclaims |
| 123 | + - configmaps |
| 124 | + includeClusterResources: false |
| 125 | + storageLocation: '{{ .Parameters.backupStorageLocationName }}' |
| 126 | + volumeSnapshotLocations: |
| 127 | + - '{{ .Parameters.backupStorageLocationName }}' |
| 128 | + snapshotVolumes: true |
| 129 | + snapshotMoveData: true |
| 130 | + ttl: 720h0m0s |
| 131 | + itemOperationTimeout: 24h0m0s |
| 132 | +EOF |
| 133 | + |
| 134 | +log_success "VMDisk strategy created" |
| 135 | + |
| 136 | +separator |
| 137 | + |
| 138 | +log_step "Verifying strategies..." |
| 139 | +log_command "kubectl get velero.strategy.backups.cozystack.io" |
| 140 | +kubectl get velero.strategy.backups.cozystack.io |
| 141 | + |
| 142 | +separator |
| 143 | + |
| 144 | +log_success "Velero strategies are ready" |
| 145 | +echo -e "\n${GREEN}${BOLD}Next step:${NC} ./02-create-backupclass.sh" |
0 commit comments