Last active
December 13, 2025 07:41
-
-
Save zburgermeiszter/52806b02465e35f6111638b3f8bbf171 to your computer and use it in GitHub Desktop.
Export all resources from Kubernetes (k8s) clusters. Resources are exported to separate folders and files by namespace and API name.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| backup_folder="kubernetes_backup" | |
| # Create a directory to store the backup | |
| mkdir -p $backup_folder | |
| # Get a list of all resource resources, including both namespaced and non-namespaced resources | |
| resources=$(kubectl api-resources --verbs=list -o name) | |
| # Get a list of all namespaces | |
| namespaces=$(kubectl get namespaces -o=jsonpath='{.items[*].metadata.name}') | |
| # Retrieve non-namespaced resources | |
| for resource in $resources; do | |
| echo "$(date +"%Y-%m-%d %H:%M:%S") - Exporting: $resource" | |
| # Check if the resource is namespaced | |
| if [ $(kubectl get $resource -o jsonpath='{.items[*].metadata.namespace}' | wc -w) -gt 0 ]; then # if namespaced | |
| echo "$(date +"%Y-%m-%d %H:%M:%S") - Exporting (namespaced): $resource" | |
| for namespace in $namespaces; do | |
| echo "$(date +"%Y-%m-%d %H:%M:%S") - Exporting: $namespace/$resource" | |
| if [ $(kubectl get $resource --namespace=$namespace -o jsonpath='{.items[*].metadata.name}' | wc -w) -gt 0 ]; then | |
| mkdir -p $backup_folder/$namespace/$resource | |
| # Write each item to its own file | |
| for item in $(kubectl get $resource --namespace=$namespace -o jsonpath='{.items[*].metadata.name}'); do | |
| echo "$(date +"%Y-%m-%d %H:%M:%S") - Exporting: $namespace/$resource/$item" | |
| kubectl get $resource $item --namespace=$namespace -o yaml >$backup_folder/$namespace/$resource/$item.yaml | |
| done | |
| fi | |
| done | |
| else # if not namespaced | |
| echo "$(date +"%Y-%m-%d %H:%M:%S") - Exporting (non-namespaced): $resource" | |
| if [ $(kubectl get $resource -o jsonpath='{.items[*].metadata.name}' | wc -w) -gt 0 ]; then | |
| # kubectl get $resource -o yaml >$backup_folder/${resource}.yaml | |
| mkdir -p $backup_folder/non-namespaced/$resource | |
| for item in $(kubectl get $resource -o jsonpath='{.items[*].metadata.name}'); do | |
| echo "$(date +"%Y-%m-%d %H:%M:%S") - Exporting: $resource/$item" | |
| kubectl get $resource $item -o yaml >$backup_folder/non-namespaced/$resource/$item.yaml | |
| done | |
| fi | |
| fi | |
| done |
kubectl plugin get-resources does this
Get all resources
kubectl get-resources
Get specific namespace resources
kubectl get-resources --namespace=default --exclude-cluster-resources=true
Get multiple namespace resources
kubectl get-resources --namespace=ns1 --namespace=ns2 --namespace=ns3 ... --exclude-cluster-resources=true
Output resources to a directory
kubectl get-resources --namespace=default --exclude-cluster-resources=true --output=your_output_dir
See --help for other options
kubectl get-resources --help
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This worked pretty well, but if you don't have resources in the default namespace the first
ifon line 17 doesn't work. I had to modify this to add the-Aflag to search every namespace here as I didn't have pods or deployments in the default namespace and it would end up skipping pods or deployments completely if nothing existed in the default namespace: