Skip to content

Instantly share code, notes, and snippets.

@jmjoy
Created November 18, 2021 03:32
Show Gist options
  • Select an option

  • Save jmjoy/6cb371ede9e25d4d8396c150c0ef0c40 to your computer and use it in GitHub Desktop.

Select an option

Save jmjoy/6cb371ede9e25d4d8396c150c0ef0c40 to your computer and use it in GitHub Desktop.
备份所有的K8S资源(Backup all k8s resources yaml)
#!/usr/bin/env bash
ROOT=$PWD
# Backup global resources
while read -r resource
do
echo " scanning resource '${resource}'"
while read -r item x
do
mkdir -p "${ROOT}/non-namespaced/${resource}"
echo " exporting item '${item}'"
kubectl get "$resource" "$item" -o yaml > "${ROOT}/non-namespaced/${resource}/$item.yaml" &
done < <(kubectl get "$resource" --all-namespaces 2>&1 | tail -n +2)
done < <(kubectl api-resources --namespaced=false 2>/dev/null | grep -vE "componentstatuses|nodes|mutatingwebhookconfigurations|validatingwebhookconfigurations|apiservices|tokenreviews|.*subject.*|certificatesigningrequests|runtimeclasses|csi.*|volumeattachments" | tail -n +2 | awk '{print $1}' | sort | uniq)
wait
# Backup namespeced resources
while read -r resource
do
echo " scanning resource '${resource}'"
while read -r namespace item x
do
mkdir -p "${ROOT}/${namespace}/${resource}"
echo " exporting item '${namespace} ${item}'"
kubectl get "$resource" -n "$namespace" "$item" -o yaml > "${ROOT}/${namespace}/${resource}/$item.yaml" &
done < <(kubectl get "$resource" --all-namespaces 2>&1 | tail -n +2)
done < <(kubectl api-resources --namespaced=true 2>/dev/null | grep -vE "events|controllerrevisions|endpoints|replicasets|pods" | tail -n +2 | awk '{print $1}')
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment