Skip to content

Instantly share code, notes, and snippets.

@maesa
Created October 8, 2025 09:51
Show Gist options
  • Select an option

  • Save maesa/5764534f019a7f1c44be6ca45839059c to your computer and use it in GitHub Desktop.

Select an option

Save maesa/5764534f019a7f1c44be6ca45839059c to your computer and use it in GitHub Desktop.
Kubernetes Object Exporter
#!/usr/bin/env bash
# Enhanced Kubernetes object exporter
# Exports all application-related Kubernetes objects to YAML files
# Usage: ./exporter.sh <application-name> [namespace]
# Reference: https://stackoverflow.com/a/58747118
set -e # Exit on any error
# Check if application name is provided
if [ $# -eq 0 ]; then
echo "Usage: $0 <application-name> [namespace]"
echo "Example: $0 clickhouse beeza-app-prod"
echo "Example: $0 redis"
exit 1
fi
APP_NAME="$1"
NAMESPACE="${2:-}"
# Define all Kubernetes object types to check
OBJECT_TYPES="deployment,daemonset,statefulset,pvc,pv,configmap,ingress,service,secret,hpa,job,cronjob,serviceaccount,role,rolebinding,clusterrole,clusterrolebinding,networkpolicy,poddisruptionbudget,namespace,endpoints,clickhouseinstallation,clickhousekeeperinstallation"
# Set namespace parameter if provided
if [ -n "$NAMESPACE" ]; then
NAMESPACE_PARAM="--namespace=$NAMESPACE"
else
NAMESPACE_PARAM=""
fi
echo "Exporting $APP_NAME-related Kubernetes objects..."
if [ -n "$NAMESPACE" ]; then
echo "Namespace: $NAMESPACE"
else
echo "Namespace: all namespaces"
fi
# Create output directory
mkdir -p exported-objects
cd exported-objects
# Get all objects and filter for the application name
for obj in $(kubectl get -o=name $OBJECT_TYPES $NAMESPACE_PARAM 2>/dev/null | grep -i "$APP_NAME" || true)
do
if [ -n "$obj" ]; then
# Create a safe filename by replacing slashes with underscores
safe_filename=$(echo "$obj" | sed 's|/|_|g')
echo "Exporting: $obj -> ${safe_filename}.yaml"
# Export the object
if kubectl get $obj $NAMESPACE_PARAM -o yaml > "${safe_filename}.yaml" 2>/dev/null; then
echo "✓ Successfully exported $obj"
else
echo "✗ Failed to export $obj"
fi
fi
done
echo ""
echo "Export completed for application: $APP_NAME"
echo "Files saved in: $(pwd)"
echo "Total files created: $(ls -1 *.yaml 2>/dev/null | wc -l)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment