Skip to content

Instantly share code, notes, and snippets.

@kassah
Created April 21, 2022 16:09
Show Gist options
  • Save kassah/15af622306596413b67ba7a68649b70a to your computer and use it in GitHub Desktop.
Save kassah/15af622306596413b67ba7a68649b70a to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
echo "Fetching possible namespaced resource types."
resources=$(kubectl api-resources --verbs=list --namespaced -o name | xargs | sed -e 's/ /,/g')
echo "Storing each namespace contents into it's own directory..."
for ns in $(kubectl get -o=name ns | xargs basename)
do
echo "Storing namespace ${ns}..."
for n in $(kubectl -n ${ns} get -o=name ${resources})
do
mkdir -p ns/${ns}/$(dirname $n)
kubectl -n ${ns} get -o=yaml $n > ns/${ns}/$n.yaml
done
done
echo "Storing each namespace definition it's namespaced directory, instead of cluster wide resources."
for n in $(kubectl get -o=name namespaces)
do
mkdir -p ns/$(basename $n)/$(dirname $n)
kubectl -n ${ns} get -o=yaml $n > ns/$(basename $n)/$n.yaml
done
echo "Fetching non-namespaced resource types..."
resources=$(kubectl api-resources --verbs=list --namespaced=false -o name \
| grep -v "namespaces" \
| xargs | sed -e 's/ /,/g')
echo "Storing clusterwide resources."
for n in $(kubectl get -o=name ${resources})
do
mkdir -p no-ns/$(dirname $n)
kubectl -n ${ns} get -o=yaml $n > no-ns/$n.yaml
done
@kassah
Copy link
Author

kassah commented Apr 21, 2022

Before blowing away a Kubernetes cluster, I run this first, just in case there is anything I need to restore or figure out.

It stores things in two main directories
ns (for all namespaced items, namespaces are included here, even though their not technically namespaced)
no-ns (for all non-namespaced items)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment