Skip to content

Instantly share code, notes, and snippets.

@galexrt
Created February 3, 2018 14:08
Show Gist options
  • Save galexrt/cba1e3658427eb5ed11a3d166761321c to your computer and use it in GitHub Desktop.
Save galexrt/cba1e3658427eb5ed11a3d166761321c to your computer and use it in GitHub Desktop.
A bash script using jq to backup a Kubernetes cluster. I recommend using a tool like heptio/ark for K8s backups/disaster recovery.
#!/bin/bash
RESSOURCE_IGNORE="^(namespaces|pods|events|nodes|clusters|storageclasses|thirdpartyresources|clusterrolebindings|clusterroles|componentstatuses|persistentvolumes)$"
echo "-> Getting namespaces ..."
kubectl get --export -o=json ns | \
jq '.items[] |
select(.metadata.name!="kube-system") |
select(.metadata.name!="default") |
del(.status,
.metadata.uid,
.metadata.selfLink,
.metadata.resourceVersion,
.metadata.creationTimestamp,
.metadata.generation
)' > ./backup-namespaces.json
echo "=> Namespaces saved."
echo "-> Saving every namespaced objects"
for ns in $(jq -r '.metadata.name' < ./backup-namespaces.json); do
echo "--> Currently saving namespace: $ns"
mkdir "backup-$ns"
for ressource in $(kubectl get --help 2>&1 | grep ' \* ' | sed 1d | awk '{ print $2}' | tr "\n" ' '); do
if [[ "$ressource" =~ $RESSOURCE_IGNORE ]]; then
continue
fi
echo "---> Saving ressource: $ressource"
EXPORT="$(kubectl --namespace="${ns}" get --export -o=json "$ressource" | \
jq '.items[] |
select(.type!="kubernetes.io/service-account-token") |
del(
.spec.clusterIP,
.metadata.uid,
.metadata.selfLink,
.metadata.resourceVersion,
.metadata.creationTimestamp,
.metadata.generation,
.status,
.spec.template.spec.securityContext,
.spec.template.spec.dnsPolicy,
.spec.template.spec.terminationGracePeriodSeconds,
.spec.template.spec.restartPolicy
)')"
if [ -n "$EXPORT" ]; then
echo "$EXPORT" >> "./backup-$ns/$ressource.json"
fi
echo "---> Saved ressource: $ressource"
done
echo "--> Namespace saved: $ns"
done
echo "=> Namespaced objects saved."
echo "-> Saving non-namespaced objects..."
kubectl --namespace="${ns}" get --export -o=json clusterrolebindings,clusterroles,componentstatuses,storageclasses,thirdpartyresources,persistentvolumes | \
jq '.items[] |
select(.type!="kubernetes.io/service-account-token") |
del(
.spec.clusterIP,
.metadata.uid,
.metadata.selfLink,
.metadata.resourceVersion,
.metadata.creationTimestamp,
.metadata.generation,
.status,
.spec.template.spec.securityContext,
.spec.template.spec.dnsPolicy,
.spec.template.spec.terminationGracePeriodSeconds,
.spec.template.spec.restartPolicy
)' >> "./backup-non-namespaced.json"
echo "=> Saved non-namespaced objects."
echo "Completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment