备份所有命名空间
#!/bin/sh
for ns in $(kubectl get ns | awk '{print $1}'|grep -v NAME);do
result_get=$(kubectl get -o=name pvc,configmap,serviceaccount,secret,ingress,service,deployment,statefulset,hpa,job,cronjob,DaemonSet -n $ns)
for n in $result_get; do
mkdir -p `date +%Y%m%d-$ns`/$(dirname $n)
kubectl get -o=yaml $n -n $ns > `date +%Y%m%d-$ns`/$n.yaml
rm -rf `date -d "-60 day" +%Y%m%d-$ns`
done
done
计划任务
加入计划任务 每天2点进行资源备份
echo " * 2 * * * /usr/bin/sh /opt/k8sbak.sh " >> /var/spool/cron/root
备份结果查看
查找我们想要的yaml
例如calico
[root@zcsmaster1 opt]# cd 20221220-kube-system
[root@zcsmaster1 20221220-kube-system]# ls -lrt */*|grep -i calico
想要备份一个命名空间也可以
k8s 资源备份(ccse空间)及查看
#!/bin/sh
result_get=`kubectl get -o=name pvc,configmap,serviceaccount,secret,ingress,service,deployment,statefulset,hpa,job,cronjob -n develop`
for n in $result_get; do
mkdir -p `date +%Y%m%d`/$(dirname $n)
kubectl get -o=yaml $n -n ccse > `date +%Y%m%d`/$n.yaml
done
rm -rf `date -d "-60 day" +%Y%m%d`