-
Notifications
You must be signed in to change notification settings - Fork 4
/
kube-save.sh
executable file
·56 lines (47 loc) · 1.27 KB
/
kube-save.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
# Save output data from a Persistent Volume on a Kubernetes cluster.
# parse command-line arguments
if [[ $# != 2 ]]; then
echo "usage: $0 <pvc-name> <remote-path>"
exit -1
fi
PVC_NAME="$1"
PVC_PATH="/workspace"
POD_FILE="pod.yaml"
POD_NAME="${USER}-save-$(printf %04x ${RANDOM})"
REMOTE_PATH="$2"
# create pod config file
cat > ${POD_FILE} <<EOF
apiVersion: v1
kind: Pod
metadata:
name: ${POD_NAME}
spec:
containers:
- name: ${POD_NAME}
image: ubuntu
args: ["sleep", "infinity"]
volumeMounts:
- mountPath: ${PVC_PATH}
name: ${PVC_NAME}
restartPolicy: Never
volumes:
- name: ${PVC_NAME}
persistentVolumeClaim:
claimName: ${PVC_NAME}
EOF
# create pod
kubectl create -f ${POD_FILE}
# wait for pod to initialize
POD_STATUS=""
while [[ ${POD_STATUS} != "Running" ]]; do
sleep 2
POD_STATUS="$(kubectl get pod --no-headers --output jsonpath={.status.phase} ${POD_NAME})"
done
# copy output data from pod
echo "copying data..."
kubectl exec ${POD_NAME} -- bash -c "for f in \$(find ${PVC_PATH}/${USER}/${REMOTE_PATH} -type l); do cp --remove-destination \$(readlink \$f) \$f; done"
kubectl cp "${POD_NAME}:${PVC_PATH}/${USER}/${REMOTE_PATH}" "$(basename ${REMOTE_PATH})"
# delete pod
kubectl delete -f ${POD_FILE}
rm -f ${POD_FILE}