Skip to content

Instantly share code, notes, and snippets.

@gneissguise
Last active January 12, 2021 13:07
Show Gist options
  • Save gneissguise/a5d407207a06edd8c7104812cb983445 to your computer and use it in GitHub Desktop.
Save gneissguise/a5d407207a06edd8c7104812cb983445 to your computer and use it in GitHub Desktop.
Kubernetes Cluster Backup Script
#!/bin/bash
## Run on Master/Control Plane!
IFS=$'\n'
arr1=($(kubectl get -o=name namespace))
for ns in "${arr1[@]}"; do
names="${ns##*/}"
echo "$names"
arr2=($(kubectl get -o=name pvc,configmap,serviceaccount,secret,ingress,service,deployment,statefulset,hpa,job,cronjob,daemonset -n $names))
for rs in "${arr2[@]}"; do
echo "..$rs"
mkdir -p "$names/$(dirname $rs)"
kubectl get -o=yaml $rs -n $names > "$names/$rs.yaml"
done
done
arr3=($(kubectl get -o=name storageclass,persistentvolume,clusterrolebinding,clusterrole,rolebinding,role,serviceaccount))
for rl in "${arr3[@]}"; do
echo "$rl"
mkdir -p "$(dirname $rl)"
kubectl get -o=yaml $rl -n $names > "$rl.yaml"
done
@gneissguise
Copy link
Author

I needed a quick and easy way to export my k8s configs to yaml for a migration, so I wrote this script.

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