shell - 备份k8s资源文件
2023-12-25
#!/bin/bash
k8s_clusters="xxxx"
kinds="deployment statefulsets service ingress configmap secret pvc"
path="/opt/backup"
date=$(date "+%Y%m%d")
mkdir -p ${path}
for k8s_cluster in ${k8s_clusters}
do
namespaces=$(/usr/local/bin/kubectl --kubeconfig ~/.kube/${k8s_cluster} get namespace | awk '{print $1}' | grep -v NAME)
for namespace in ${namespaces}
do
for kind in ${kinds}
do
mkdir -p ${path}/k8s/${k8s_cluster}/${namespace}/${kind}/${date}
names=$(/usr/local/bin/kubectl --kubeconfig ~/.kube/${k8s_cluster} -n ${namespace} get ${kind} | awk '{print $1}' | grep -v NAME)
for name in ${names}
do
/usr/local/bin/kubectl --kubeconfig ~/.kube/${k8s_cluster} -n ${namespace} get ${kind} ${name} -o yaml > ${path}/k8s/${k8s_cluster}/${namespace}/${kind}/${date}/${name}
done
done
done
done
find ${path}/k8s/${k8s_cluster}/${namespace} -type d |xargs rmdir
find ${path}/k8s/${k8s_cluster}/${namespace} -type d |xargs rmdir