Last active
June 8, 2021 08:47
-
-
Save gadiener/7a191028525fcadf2e8f9d04fa1fd221 to your computer and use it in GitHub Desktop.
Script to get all the resources yaml file from the Kubernetes context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# How to run: `./backup_cluster.sh | tee export-$(date +%s).log` | |
set -e | |
if [ -n "${DEBUG}" ]; then | |
set -x | |
fi | |
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | |
NAMESPACES=$(kubectl get namespace | awk '{print $1}' | tail -n +2) | |
RESOURCES=$(kubectl api-resources --verbs=list --namespaced=true -o name) | |
GLOBAL_RESOURCES=$(kubectl api-resources --verbs=list --namespaced=false -o name) | |
CURRENT_CONTEXT=$(kubectl config current-context) | |
NAMESPACES_COUNT=$(echo "${NAMESPACES}" | wc -l | sed 's/ //g') | |
RESOURCES_COUNT=$(echo "${RESOURCES}" | wc -l | sed 's/ //g') | |
GLOBAL_RESOURCES_COUNT=$(echo "${GLOBAL_RESOURCES}" | wc -l | sed 's/ //g') | |
if [ -d "${SCRIPT_DIR}/${CURRENT_CONTEXT}" ]; then | |
echo "The directory '${CURRENT_CONTEXT}' already exists" | |
exit 1 | |
fi | |
if [ "$(ls -A "${SCRIPT_DIR}/${CURRENT_CONTEXT}" >/dev/null 2>&1)" ]; then | |
echo "The directory '${CURRENT_CONTEXT}' is not empty" | |
exit 1 | |
fi | |
mkdir "${SCRIPT_DIR}/${CURRENT_CONTEXT}" | |
echo "Exporting '${CURRENT_CONTEXT}' context" | |
echo | |
echo "Namespaces: ${NAMESPACES_COUNT}" | |
echo "Global resources: ${GLOBAL_RESOURCES_COUNT}" | |
echo "Resources: ${RESOURCES_COUNT}" | |
echo | |
echo "[Exporting global resources]" | |
echo | |
for resources in ${GLOBAL_RESOURCES[@]}; do | |
RESOURCE_COUNT=$(kubectl get --show-kind --ignore-not-found "${resources}" | tail -n +2 | wc -l | sed 's/ //g') | |
echo "> Exporting '${resources}': ${RESOURCE_COUNT}" | |
if [ "${RESOURCE_COUNT}" != "0" ]; then | |
kubectl get --show-kind --ignore-not-found "${resources}" -o yaml \ | |
| yq eval 'del(.items[].metadata.resourceVersion)' - \ | |
| yq eval 'del(.items[].metadata.uid)' - \ | |
| yq eval 'del(.items[].metadata.creationTimestamp)' - \ | |
| yq eval 'del(.items[].metadata.selfLink)' - \ | |
| yq eval 'del(.items[].metadata.managedFields)' - \ | |
| yq eval 'del(.items[].status)' - > "${SCRIPT_DIR}/${CURRENT_CONTEXT}/${resources}.yaml" | |
fi | |
done | |
echo | |
echo "[Exporting namespaced resources]" | |
echo | |
for namespace in ${NAMESPACES[@]}; do | |
if [ "${namespace}" == "kube-system" ] || [ "${namespace}" == "kube-node-lease" ]; then | |
echo "# Skipped '${namespace}'!" | |
continue | |
fi | |
mkdir "${SCRIPT_DIR}/${CURRENT_CONTEXT}/${namespace}" | |
echo "> Exporting namespace '${namespace}':" | |
for resources in ${RESOURCES[@]}; do | |
RESOURCE_COUNT=$(kubectl get --show-kind --ignore-not-found -n "${namespace}" "${resources}" | tail -n +2 | wc -l | sed 's/ //g') | |
echo " - ${resources}: ${RESOURCE_COUNT}" | |
if [ "${RESOURCE_COUNT}" != "0" ]; then | |
kubectl get --show-kind --ignore-not-found -n "${namespace}" "${resources}" -o yaml \ | |
| yq eval 'del(.items[].metadata.resourceVersion)' - \ | |
| yq eval 'del(.items[].metadata.uid)' - \ | |
| yq eval 'del(.items[].metadata.creationTimestamp)' - \ | |
| yq eval 'del(.items[].metadata.selfLink)' - \ | |
| yq eval 'del(.items[].metadata.managedFields)' - \ | |
| yq eval 'del(.items[].status)' - > "${SCRIPT_DIR}/${CURRENT_CONTEXT}/${namespace}/${resources}.yaml" | |
fi | |
done | |
echo "+ Completed export '${namespace}'!" | |
done | |
echo "Completed export for '${CURRENT_CONTEXT}' context" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment