Last active
July 12, 2019 13:07
-
-
Save sacreman/0c67cf10e9f9b080573ace91e348d1bb to your computer and use it in GitHub Desktop.
Backup all K8s config to a timestamped location
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 -e | |
BACKUP_DIR="/var/tmp/k8sbackup/$(date +%s)" | |
echo "Backing up cluster to ${BACKUP_DIR}" | |
NAMESPACES=$(kubectl get ns -o jsonpath={.items[*].metadata.name}) | |
RESOURCETYPES="${RESOURCETYPES:-"ingress deployment configmap secret svc rc ds networkpolicy statefulset cronjob pvc"}" | |
GLOBALRESOURCES="${GLOBALRESOURCES:-"namespace storageclass clusterrole clusterrolebinding customresourcedefinition"}" | |
mkdir -p ${BACKUP_DIR} | |
cd ${BACKUP_DIR} | |
# Start kubernetes state export | |
for resource in $GLOBALRESOURCES; do | |
echo "Exporting resource: ${resource}" >/dev/stderr | |
kubectl get -o=json "$resource" | jq --sort-keys \ | |
'del( | |
.items[].metadata.annotations."kubectl.kubernetes.io/last-applied-configuration", | |
.items[].metadata.annotations."control-plane.alpha.kubernetes.io/leader", | |
.items[].metadata.uid, | |
.items[].metadata.selfLink, | |
.items[].metadata.resourceVersion, | |
.items[].metadata.creationTimestamp, | |
.items[].metadata.generation | |
)' | python -c 'import sys, yaml, json; yaml.safe_dump(json.load(sys.stdin), sys.stdout, default_flow_style=False)' >"${BACKUP_DIR}/${resource}.yaml" | |
done | |
for namespace in $NAMESPACES; do | |
[ -d "${BACKUP_DIR}/${namespace}" ] || mkdir -p "${BACKUP_DIR}/${namespace}" | |
for type in $RESOURCETYPES; do | |
echo "[${namespace}] Exporting resources: ${type}" >/dev/stderr | |
label_selector="" | |
if [[ "$type" == 'configmap' && -z "${INCLUDE_TILLER_CONFIGMAPS:-}" ]]; then | |
label_selector="-l OWNER!=TILLER" | |
fi | |
kubectl --namespace="${namespace}" get "$type" $label_selector -o custom-columns=SPACE:.metadata.namespace,KIND:..kind,NAME:.metadata.name --no-headers | while read -r a b name; do | |
[ -z "$name" ] && continue | |
# Service account tokens cannot be exported | |
if [[ "$type" == 'secret' && $(kubectl get -n "${namespace}" -o jsonpath="{.type}" secret "$name") == "kubernetes.io/service-account-token" ]]; then | |
continue | |
fi | |
kubectl --namespace="${namespace}" get -o=json "$type" "$name" | jq --sort-keys \ | |
'del( | |
.metadata.annotations."control-plane.alpha.kubernetes.io/leader", | |
.metadata.annotations."kubectl.kubernetes.io/last-applied-configuration", | |
.metadata.creationTimestamp, | |
.metadata.generation, | |
.metadata.resourceVersion, | |
.metadata.selfLink, | |
.metadata.uid, | |
.spec.clusterIP, | |
.status | |
)' | python -c 'import sys, yaml, json; yaml.safe_dump(json.load(sys.stdin), sys.stdout, default_flow_style=False)' >"${BACKUP_DIR}/${namespace}/${name}.${type}.yaml" | |
done | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment