可以参考借鉴的项目代码
太复杂,备份出的东西较多 https://github.com/solomonxu/k8s-backup-restore https://dockone.io/article/9194
值得学习,备份结果比较简单,但需要进一步处理 https://github.com/sangwook/k8s-cluster-backup
全套服务(本地、容器、k8s三种部署方式) https://kube-dump.woozymasta.ru/
#!/usr/bin/env bash
# pip3 install jq
# download install yq
# curl -sLo /usr.local/bin/jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64
# chmod +x /usr.local/bin/jq
# curl -sLo /usr.local/bin/yq https://github.com/mikefarah/yq/releases/download/v4.5.0/yq_linux_amd64
# chmod +x /usr.local/bin/yq
bkpdir="backup_$(date +%y%m%d)"
mkdir $bkpdir
cd $bkpdir
for curns in $(kubectl get ns -o jsonpath={.items[*].metadata.name}); do
mkdir -p $curns
kubectl get -o=yaml ns $curns | yq -y \
'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
)' > $curns.ns.yaml
#for type in ing deploy cm svc ds sts cj pvc secret sa; do
for type in ingresses deployments configmaps services daemonsets statefulsets cronjobs jobs pvc pv secret serviceaccounts; do
for name in $(kubectl --namespace=$curns get $type -l OWNER!=TILLER -o custom-columns=NAME:.metadata.name --no-headers); do
kubectl --namespace=$curns get -o=yaml $type $name | yq -y \
'del(
.metadata.annotations."control-plane.alpha.kubernetes.io/leader",
.metadata.annotations."kubectl.kubernetes.io/last-applied-configuration",
.metadata.annotations."pv.kubernetes.io/bind-completed",
.metadata.annotations."pv.kubernetes.io/bound-by-controller",
.metadata.annotations."volume.beta.kubernetes.io/storage-provisioner",
.metadata.creationTimestamp,
.metadata.finalizers,
.metadata.generation,
.metadata.resourceVersion,
.metadata.selfLink,
.metadata.uid,
.spec.clusterIPs,
.spec.volumeName,
.status
)' > $curns/$name.$type.yaml
done
done
done