Skip to content

Instantly share code, notes, and snippets.

@davenicoll
Created November 28, 2022 19:22
Show Gist options
  • Save davenicoll/16e52b005db2fa5f998df743fca429b6 to your computer and use it in GitHub Desktop.
Save davenicoll/16e52b005db2fa5f998df743fca429b6 to your computer and use it in GitHub Desktop.
Dump the contents of a k8s cluster
#!/usr/bin/env bash
set -e
CONTEXT="$1"
if [[ -z ${CONTEXT} ]]; then
echo "Usage: $0 context"
exit 1
fi
NAMESPACES=$(kubectl --context ${CONTEXT} get -o json namespaces|jq '.items[].metadata.name'|sed "s/\"//g")
RESOURCES="ingress ingressroute configmap secret daemonset deployment service serviceaccount pv pvc hpa"
echo "Finding resources..."
for ns in ${NAMESPACES};do
for resource in ${RESOURCES};do
echo "${ns}/${resource}"
rsrcs=$(kubectl --context ${CONTEXT} -n ${ns} get -o json ${resource}|jq '.items[].metadata.name'|sed "s/\"//g")
for r in ${rsrcs};do
dir="${ns}/${resource}" # dir="${CONTEXT}/${ns}/${resource}"
mkdir -p "${dir}"
kubectl --context ${CONTEXT} -n ${ns} get -o yaml ${resource} ${r} > "${dir}/${r}.yaml"
done
done
done
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment