Skip to content

Instantly share code, notes, and snippets.

@v0lkan
Created October 24, 2022 01:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save v0lkan/5d199187b395c8b8f3cf2d10ae81cc69 to your computer and use it in GitHub Desktop.
Save v0lkan/5d199187b395c8b8f3cf2d10ae81cc69 to your computer and use it in GitHub Desktop.
Dump everything in your Kubernetes cluster into folders a YAML files
#!/usr/bin/env bash
# Dumps everything in your Kubernetes cluster into folders a YAML files.
# Use it at your own risk.
set -e
CONTEXT=“$1”
if [[ -z ${CONTEXT} ]]; then
echo “Usage: $0 KUBE-CONTEXT”
exit 1
fi
NAMESPACES=$(kubectl —context ${CONTEXT} get -o json namespaces|jq ‘.items[].metadata.name’|sed “s/\”//g”)
RESOURCES=$(kubectl api-resources —namespaced -o name | tr “\n” “ “)
# Note that the above will put some presure on the API server, you might want to
# use something less agressive, similar to this:
# RESOURCES=“configmap secret daemonset deployment service hpa”
for ns in ${NAMESPACES};do
for resource in ${RESOURCES};do
rsrcs=$(kubectl —context ${CONTEXT} -n ${ns} get -o json ${resource}|jq ‘.items[].metadata.name’|sed “s/\”//g”)
for r in ${rsrcs};do
dir=“${CONTEXT}/${ns}/${resource}”
mkdir -p “${dir}”
kubectl —context ${CONTEXT} -n ${ns} get -o yaml ${resource} ${r} > “${dir}/${r}.yaml”
done
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment