Skip to content

Instantly share code, notes, and snippets.

@amosshapira
Created September 13, 2022 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amosshapira/533f3bca1e233dcefa6d801bddd29c2b to your computer and use it in GitHub Desktop.
Save amosshapira/533f3bca1e233dcefa6d801bddd29c2b to your computer and use it in GitHub Desktop.
Export all K8s resources
#!/bin/env bash
## Source: https://github.com/kubernetes/kubernetes/issues/24873#issuecomment-416689257
## https://github.com/kubernetes/kubernetes/issues/24873#issuecomment-416189335
# This version excludes service account tokens from the export
i=$((0))
for n in $(kubectl get -o=custom-columns=NAMESPACE:.metadata.namespace,KIND:.kind,NAME:.metadata.name pv,pvc,configmap,ingress,service,secret,deployment,statefulset,hpa,job,cronjob --all-namespaces | grep -v 'secrets/default-token')
do
if (( $i < 1 )); then
namespace=$n
i=$(($i+1))
if [[ "$namespace" == "PersistentVolume" ]]; then
kind=$n
i=$(($i+1))
fi
elif (( $i < 2 )); then
kind=$n
i=$(($i+1))
elif (( $i < 3 )); then
name=$n
i=$((0))
if [[ "$namespace" != "NAMESPACE" ]]; then
mkdir -p $namespace
yaml=$((kubectl get $kind -o=yaml $name -n $namespace ) 2>/dev/null)
if [[ $kind != 'Secret' || $yaml != *"type: kubernetes.io/service-account-token"* ]]; then
echo "Saving ${namespace}/${kind}.${name}.yaml"
kubectl get $kind -o=yaml --export $name -n $namespace > $namespace/$kind.$name.yaml
fi
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment