Shell - backup k8s
2022-06-17
#!/bin/bash
kinds="configmaps endpoints persistentvolumeclaims secrets serviceaccounts services daemonsets deployments horizontalpodautoscalers cronjobs ingresses ingresses networkpolicies"
cluster=$1
if [[ -z ${cluster} ]];then
echo 'please input cluster'
exit 1
fi
namespaces=$(kubectl --kubeconfig /root/.kube/all-config/${cluster} get ns | grep -v NAME | awk '{print $1}' )
for kind in ${kinds}
do
for namespace in ${namespaces}
do
names=$(kubectl --kubeconfig /root/.kube/all-config/${cluster} -n ${namespace} get ${kind} | grep -v NAME | awk '{print $1}')
temp_path="/tmp/k8s/${cluster}/${namespace}/${kind}"
mkdir -p ${temp_path}
for name in ${names}
do
kubectl --kubeconfig /root/.kube/all-config/${cluster} -n ${namespace} get ${kind} ${name} -o yaml > ${temp_path}/${name}.yaml
done
done
done
kinds="persistentvolumes storageclasses"
for kind in ${kinds}
do
names=$(kubectl --kubeconfig /root/.kube/all-config/${cluster} get ${kind} | grep -v NAME | awk '{print $1}')
temp_path="/tmp/k8s/${cluster}/${kind}"
mkdir -p ${temp_path}
for name in ${names}
do
kubectl --kubeconfig /root/.kube/all-config/${cluster} get ${kind} ${name} -o yaml > ${temp_path}/${name}.yaml
done
done