@@ -56,19 +56,19 @@ The following are important considerations to evaluate:
56
56
``` bash
57
57
#! /bin/sh
58
58
# Patch the Persistent Volume in case ReclaimPolicy is Delete
59
- namespace =$1
59
+ NAMESPACE =$1
60
60
i=1
61
- for pvc in $( kubectl get pvc -n $namespace | awk ' { print $1}' ) ; do
61
+ for PVC in $( kubectl get pvc -n $namespace | awk ' { print $1}' ) ; do
62
62
# Ignore first record as it contains header
63
63
if [ $i -eq 1 ]; then
64
64
i=$(( i + 1 ))
65
65
else
66
- pv =" $( kubectl get pvc $pvc -n $namespace -o jsonpath=' {.spec.volumeName}' ) "
67
- reclaimPolicy =" $( kubectl get pv $pv -n $namespace -o jsonpath=' {.spec.persistentVolumeReclaimPolicy}' ) "
68
- echo " Reclaim Policy for Persistent Volume $pv is $reclaimPolicy "
69
- if [[ $reclaimPolicy == " Delete" ]]; then
66
+ PV =" $( kubectl get pvc $PVC -n $NAMESPACE -o jsonpath=' {.spec.volumeName}' ) "
67
+ RECLAIMPOLICY =" $( kubectl get pv $PV -n $NAMESPACE -o jsonpath=' {.spec.persistentVolumeReclaimPolicy}' ) "
68
+ echo " Reclaim Policy for Persistent Volume $PV is $RECLAIMPOLICY "
69
+ if [[ $RECLAIMPOLICY == " Delete" ]]; then
70
70
echo " Updating ReclaimPolicy for $pv to Retain"
71
- kubectl patch pv $pv -p ' {"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'
71
+ kubectl patch pv $PV -p ' {"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'
72
72
fi
73
73
fi
74
74
done
@@ -95,79 +95,79 @@ The following are important considerations to evaluate:
95
95
#! /bin/sh
96
96
# kubectl get pvc -n <namespace> --sort-by=.metadata.creationTimestamp -o custom-columns=NAME:.metadata.name,CreationTime:.metadata.creationTimestamp,StorageClass:.spec.storageClassName,Size:.spec.resources.requests.storage
97
97
# TimeFormat 2022-04-20T13:19:56Z
98
- namespace =$1
99
- fileName =$( date +%Y%m%d%H%M) -$namespace
100
- existingStorageClass =$2
101
- storageClassNew =$3
102
- starttimestamp =$4
103
- endtimestamp =$5
98
+ NAMESPACE =$1
99
+ FILENAME =$( date +%Y%m%d%H%M) -$NAMESPACE
100
+ EXISTING_STORAGE_CLASS =$2
101
+ STORAGE_CLASS_NEW =$3
102
+ STARTTIMESTAMP =$4
103
+ ENDTIMESTAMP =$5
104
104
i=1
105
- for pvc in $( kubectl get pvc -n $namespace | awk ' { print $1}' ) ; do
105
+ for PVC in $( kubectl get pvc -n $NAMESPACE | awk ' { print $1}' ) ; do
106
106
# Ignore first record as it contains header
107
107
if [ $i -eq 1 ]; then
108
108
i=$(( i + 1 ))
109
109
else
110
- pvcCreationTime =$( kubectl get pvc $pvc -n $namespace -o jsonpath=' {.metadata.creationTimestamp}' )
111
- if [[ $pvcCreationTime > $starttimestamp ]]; then
112
- if [[ $endtimestamp > $pvcCreationTime ]]; then
113
- pv =" $( kubectl get pvc $pvc -n $namespace -o jsonpath=' {.spec.volumeName}' ) "
114
- reclaimPolicy =" $( kubectl get pv $pv -n $namespace -o jsonpath=' {.spec.persistentVolumeReclaimPolicy}' ) "
115
- storageClass =" $( kubectl get pv $pv -n $namespace -o jsonpath=' {.spec.storageClassName}' ) "
116
- echo $pvc
117
- reclaimPolicy =" $( kubectl get pv $pv -n $namespace -o jsonpath=' {.spec.persistentVolumeReclaimPolicy}' ) "
118
- if [[ $reclaimPolicy == " Retain" ]]; then
119
- if [[ $storageClass == $existingStorageClass ]]; then
120
- storageSize =" $( kubectl get pv $pv -n $namespace -o jsonpath=' {.spec.capacity.storage}' ) "
121
- skuName =" $( kubectl get storageClass $storageClass -o jsonpath=' {.reclaimPolicy}' ) "
122
- diskURI =" $( kubectl get pv $pv -n $namespace -o jsonpath=' {.spec.azureDisk.diskURI}' ) "
123
- persistentVolumeReclaimPolicy =" $( kubectl get pv $pv -n $namespace -o jsonpath=' {.spec.persistentVolumeReclaimPolicy}' ) "
110
+ PVC_CREATION_TIME =$( kubectl get pvc $PVC -n $NAMESPACE -o jsonpath=' {.metadata.creationTimestamp}' )
111
+ if [[ $PVC_CREATION_TIME > $STARTTIMESTAMP ]]; then
112
+ if [[ $ENDTIMESTAMP > $PVC_CREATION_TIME ]]; then
113
+ PV =" $( kubectl get pvc $PVC -n $NAMESPACE -o jsonpath=' {.spec.volumeName}' ) "
114
+ RECLAIM_POLICY =" $( kubectl get pv $PV -n $NAMESPACE -o jsonpath=' {.spec.persistentVolumeReclaimPolicy}' ) "
115
+ STORAGECLASS =" $( kubectl get pv $PV -n $NAMESPACE -o jsonpath=' {.spec.storageClassName}' ) "
116
+ echo $PVC
117
+ RECLAIM_POLICY =" $( kubectl get pv $PV -n $NAMESPACE -o jsonpath=' {.spec.persistentVolumeReclaimPolicy}' ) "
118
+ if [[ $RECLAIM_POLICY == " Retain" ]]; then
119
+ if [[ $STORAGECLASS == $EXISTING_STORAGE_CLASS ]]; then
120
+ STORAGE_SIZE =" $( kubectl get pv $PV -n $NAMESPACE -o jsonpath=' {.spec.capacity.storage}' ) "
121
+ SKU_NAME =" $( kubectl get storageClass $STORAGECLASS -o jsonpath=' {.reclaimPolicy}' ) "
122
+ DISK_URI =" $( kubectl get pv $PV -n $NAMESPACE -o jsonpath=' {.spec.azureDisk.diskURI}' ) "
123
+ PERSISTENT_VOLUME_RECLAIM_POLICY =" $( kubectl get pv $PV -n $NAMESPACE -o jsonpath=' {.spec.persistentVolumeReclaimPolicy}' ) "
124
124
125
- cat > $pvc -csi.yaml << EOF
125
+ cat > $PVC -csi.yaml << EOF
126
126
apiVersion: v1
127
127
kind: PersistentVolume
128
128
metadata:
129
129
annotations:
130
130
pv.kubernetes.io/provisioned-by: disk.csi.azure.com
131
- name: $pv -csi
131
+ name: $PV -csi
132
132
spec:
133
133
accessModes:
134
134
- ReadWriteOnce
135
135
capacity:
136
- storage: $storageSize
136
+ storage: $STORAGE_SIZE
137
137
claimRef:
138
138
apiVersion: v1
139
139
kind: PersistentVolumeClaim
140
- name: $pvc -csi
141
- namespace: $namespace
140
+ name: $PVC -csi
141
+ namespace: $NAMESPACE
142
142
csi:
143
143
driver: disk.csi.azure.com
144
144
volumeAttributes:
145
- csi.storage.k8s.io/pv/name: $pv -csi
146
- csi.storage.k8s.io/pvc/name: $pvc -csi
147
- csi.storage.k8s.io/pvc/namespace: $namespace
148
- requestedsizegib: "$storageSize "
149
- skuname: $skuName
150
- volumeHandle: $diskURI
151
- persistentVolumeReclaimPolicy: $persistentVolumeReclaimPolicy
152
- storageClassName: $storageClassNew
145
+ csi.storage.k8s.io/pv/name: $PV -csi
146
+ csi.storage.k8s.io/pvc/name: $PVC -csi
147
+ csi.storage.k8s.io/pvc/namespace: $NAMESPACE
148
+ requestedsizegib: "$STORAGE_SIZE "
149
+ skuname: $SKU_NAME
150
+ volumeHandle: $DISK_URI
151
+ persistentVolumeReclaimPolicy: $PERSISTENT_VOLUME_RECLAIM_POLICY
152
+ storageClassName: $STORAGE_CLASS_NEW
153
153
---
154
154
apiVersion: v1
155
155
kind: PersistentVolumeClaim
156
156
metadata:
157
- name: $pvc -csi
158
- namespace: $namespace
157
+ name: $PVC -csi
158
+ namespace: $NAMESPACE
159
159
spec:
160
160
accessModes:
161
161
- ReadWriteOnce
162
- storageClassName: $storageClassNew
162
+ storageClassName: $STORAGE_CLASS_NEW
163
163
resources:
164
164
requests:
165
- storage: $storageSize
166
- volumeName: $pv -csi
165
+ storage: $STORAGE_SIZE
166
+ volumeName: $PV -csi
167
167
EOF
168
- kubectl apply -f $pvc -csi.yaml
169
- line ="PVC:$pvc ,PV:$pv ,StorageClassTarget:$storageClassNew "
170
- printf '%s\n' "$line " >>$fileName
168
+ kubectl apply -f $PVC -csi.yaml
169
+ LINE ="PVC:$PVC ,PV:$PV ,StorageClassTarget:$STORAGE_CLASS_NEW "
170
+ printf '%s\n' "$LINE " >>$FILENAME
171
171
fi
172
172
fi
173
173
fi
@@ -251,89 +251,89 @@ Before proceeding, verify the following:
251
251
#!/bin/sh
252
252
#kubectl get pvc -n <namespace> --sort-by=.metadata.creationTimestamp -o custom-columns=NAME:.metadata.name,CreationTime:.metadata.creationTimestamp,StorageClass:.spec.storageClassName,Size:.spec.resources.requests.storage
253
253
# TimeFormat 2022-04-20T13:19:56Z
254
- namespace =$1
255
- fileName=$namespace -$(date +%Y%m%d%H%M)
256
- existingStorageClass =$2
257
- storageClassNew =$3
258
- volumestorageClass =$4
259
- starttimestamp =$5
260
- endtimestamp =$6
254
+ NAMESPACE =$1
255
+ FILENAME=$NAMESPACE -$(date +%Y%m%d%H%M)
256
+ EXISTING_STORAGE_CLASS =$2
257
+ STORAGE_CLASS_NEW =$3
258
+ VOLUME_STORAGE_CLASS =$4
259
+ START_TIME_STAMP =$5
260
+ END_TIME_STAMP =$6
261
261
i=1
262
- for pvc in $(kubectl get pvc -n $namespace | awk ' { print $1 }' ); do
262
+ for PVC in $(kubectl get pvc -n $NAMESPACE | awk ' { print $1 }' ); do
263
263
# Ignore first record as it contains header
264
264
if [ $i -eq 1 ]; then
265
265
i=$((i + 1))
266
266
else
267
- pvcCreationTime =$(kubectl get pvc $pvc -n $namespace -o jsonpath=' {.metadata.creationTimestamp}' )
268
- if [[ $pvcCreationTime > $starttimestamp ]]; then
269
- if [[ $endtimestamp > $pvcCreationTime ]]; then
270
- pv ="$(kubectl get pvc $pvc -n $namespace -o jsonpath=' {.spec.volumeName}' )"
271
- reclaimPolicy ="$(kubectl get pv $pv -n $namespace -o jsonpath=' {.spec.persistentVolumeReclaimPolicy}' )"
272
- storageClass ="$(kubectl get pv $pv -n $namespace -o jsonpath=' {.spec.storageClassName}' )"
273
- echo $pvc
274
- reclaimPolicy ="$(kubectl get pv $pv -n $namespace -o jsonpath=' {.spec.persistentVolumeReclaimPolicy}' )"
275
- if [[ $storageClass == $existingStorageClass ]]; then
276
- storageSize ="$(kubectl get pv $pv -n $namespace -o jsonpath=' {.spec.capacity.storage}' )"
277
- skuName ="$(kubectl get storageClass $storageClass -o jsonpath=' {.reclaimPolicy}' )"
278
- diskURI ="$(kubectl get pv $pv -n $namespace -o jsonpath=' {.spec.azureDisk.diskURI}' )"
279
- targetResourceGroup ="$(cut -d' /' -f5 <<<"$diskURI ")"
280
- echo $diskURI
281
- echo $targetResourceGroup
282
- persistentVolumeReclaimPolicy ="$(kubectl get pv $pv -n $namespace -o jsonpath=' {.spec.persistentVolumeReclaimPolicy}' )"
283
- az snapshot create --resource-group $targetResourceGroup --name $pvc-$fileName --source "$diskURI "
284
- snapshotPath =$(az snapshot list --resource-group $targetResourceGroup --query "[?name == ' $pvc - $fileName ' ].id | [0]")
285
- snapshotHandle =$(echo "$snapshotPath " | tr -d ' " ')
286
- echo $snapshotHandle
267
+ PVC_CREATION_TIME =$(kubectl get pvc $PVC -n $NAMESPACE -o jsonpath=' {.metadata.creationTimestamp}' )
268
+ if [[ $PVC_CREATION_TIME > $START_TIME_STAMP ]]; then
269
+ if [[ $END_TIME_STAMP > $PVC_CREATION_TIME ]]; then
270
+ PV ="$(kubectl get pvc $PVC -n $NAMESPACE -o jsonpath=' {.spec.volumeName}' )"
271
+ RECLAIM_POLICY ="$(kubectl get pv $PV -n $NAMESPACE -o jsonpath=' {.spec.persistentVolumeReclaimPolicy}' )"
272
+ STORAGE_CLASS ="$(kubectl get pv $PV -n $NAMESPACE -o jsonpath=' {.spec.storageClassName}' )"
273
+ echo $PVC
274
+ RECLAIM_POLICY ="$(kubectl get pv $PV -n $NAMESPACE -o jsonpath=' {.spec.persistentVolumeReclaimPolicy}' )"
275
+ if [[ $STORAGE_CLASS == $EXISTING_STORAGE_CLASS ]]; then
276
+ STORAGE_SIZE ="$(kubectl get pv $PV -n $NAMESPACE -o jsonpath=' {.spec.capacity.storage}' )"
277
+ SKU_NAME ="$(kubectl get storageClass $STORAGE_CLASS -o jsonpath=' {.reclaimPolicy}' )"
278
+ DISK_URI ="$(kubectl get pv $PV -n $NAMESPACE -o jsonpath=' {.spec.azureDisk.diskURI}' )"
279
+ TARGET_RESOURCE_GROUP ="$(cut -d' /' -f5 <<<"$DISK_URI ")"
280
+ echo $DISK_URI
281
+ echo $TARGET_RESOURCE_GROUP
282
+ PERSISTENT_VOLUME_RECLAIM_POLICY ="$(kubectl get pv $PV -n $NAMESPACE -o jsonpath=' {.spec.persistentVolumeReclaimPolicy}' )"
283
+ az snapshot create --resource-group $TARGET_RESOURCE_GROUP --name $PVC-$FILENAME --source "$DISK_URI "
284
+ SNAPSHOT_PATH =$(az snapshot list --resource-group $TARGET_RESOURCE_GROUP --query "[?name == ' $PVC - $FILENAME ' ].id | [0]")
285
+ SNAPSHOT_HANDLE =$(echo "$SNAPSHOT_PATH " | tr -d ' " ')
286
+ echo $SNAPSHOT_HANDLE
287
287
sleep 10
288
288
# Create Restore File
289
- cat <<EOF >$pvc -csi.yml
289
+ cat <<EOF >$PVC -csi.yml
290
290
apiVersion: snapshot.storage.k8s.io/v1
291
291
kind: VolumeSnapshotContent
292
292
metadata:
293
- name: $pvc - $fileName
293
+ name: $PVC - $FILENAME
294
294
spec:
295
295
deletionPolicy: 'Delete'
296
296
driver: 'disk.csi.azure.com'
297
- volumeSnapshotClassName: $volumestorageClass
297
+ volumeSnapshotClassName: $VOLUME_STORAGE_CLASS
298
298
source:
299
- snapshotHandle: $snapshotHandle
299
+ snapshotHandle: $SNAPSHOT_HANDLE
300
300
volumeSnapshotRef:
301
301
apiVersion: snapshot.storage.k8s.io/v1
302
302
kind: VolumeSnapshot
303
- name: $pvc - $fileName
303
+ name: $PVC - $FILENAME
304
304
namespace: $1
305
305
---
306
306
apiVersion: snapshot.storage.k8s.io/v1
307
307
kind: VolumeSnapshot
308
308
metadata:
309
- name: $pvc - $fileName
309
+ name: $PVC - $FILENAME
310
310
namespace: $1
311
311
spec:
312
- volumeSnapshotClassName: $volumestorageClass
312
+ volumeSnapshotClassName: $VOLUME_STORAGE_CLASS
313
313
source:
314
- volumeSnapshotContentName: $pvc - $fileName
314
+ volumeSnapshotContentName: $PVC - $FILENAME
315
315
---
316
316
apiVersion: v1
317
317
kind: PersistentVolumeClaim
318
318
metadata:
319
- name: csi-$pvc
319
+ name: csi-$PVC
320
320
namespace: $1
321
321
spec:
322
322
accessModes:
323
323
- ReadWriteOnce
324
- storageClassName: $storageClassNew
324
+ storageClassName: $STORAGE_CLASS_NEW
325
325
resources:
326
326
requests:
327
- storage: $storageSize
327
+ storage: $STORAGE_SIZE
328
328
dataSource:
329
- name: $pvc - $fileName
329
+ name: $PVC - $FILENAME
330
330
kind: VolumeSnapshot
331
331
apiGroup: snapshot.storage.k8s.io
332
332
333
333
EOF
334
- kubectl create -f $pvc -csi.yml
335
- line =" OLDPVC:$pvc ,OLDPV:$pv ,VolumeSnapshotContent:volumeSnapshotContent-$fileName ,VolumeSnapshot:volumesnapshot$fileName ,OLDdisk:$diskURI "
336
- printf '%s\n' " $line " >>$fileName
334
+ kubectl create -f $PVC -csi.yml
335
+ LINE =" OLDPVC:$PVC ,OLDPV:$PV ,VolumeSnapshotContent:volumeSnapshotContent-$FILENAME ,VolumeSnapshot:volumesnapshot$FILENAME ,OLDdisk:$DISK_URI "
336
+ printf '%s\n' " $LINE " >>$FILENAME
337
337
fi
338
338
fi
339
339
fi
0 commit comments