k8s每日备份脚本

#!/bin/bash
init_dir=/opt/k8s_backup/k8s_yaml_`whoami`
mkdir -p ${init_dir}/{clusterrole,clusterrolebinding,configmap,daemonsets,deployments,statefulsets,ingresses,nodes,ns,sa,secrets,services,files,pv,pvc}

cd ${init_dir}/ns
kubectl get namespaces|grep -vE "kube-*|default|^NAME"|while read line
do
echo $line
ns=`echo $line|awk '{print $1}'`
kubectl get ns ${ns} -o yaml > ${ns}.yaml
done

cd ${init_dir}/pvc
kubectl get pvc --all-namespaces|grep -vE "kube-*|default|^NAME"|while read line
do
echo $line
ns=`echo $line|awk '{print $1}'`
deploy=`echo $line|awk '{print $2}'`
kubectl get pvc ${deploy} -n ${ns} -o yaml > ${deploy}.yaml
done

cd ${init_dir}/pv
kubectl get pv -o yaml > pv.yaml

cd ${init_dir}/deployments
kubectl get deployments --all-namespaces |grep -vE "^NAME"|while read line
do
echo $line
ns=`echo $line|awk '{print $1}'`
deploy=`echo $line|awk '{print $2}'`
mkdir -p ${ns}
kubectl get deployments ${deploy} -n ${ns} -o yaml > ${ns}/${deploy}.yaml
done

cd ${init_dir}/daemonsets
kubectl get daemonsets --all-namespaces |grep -vE "^NAME"|while read line
do
echo $line
ns=`echo $line|awk '{print $1}'`
deploy=`echo $line|awk '{print $2}'`
mkdir -p ${ns}
kubectl get daemonsets ${deploy} -n ${ns} -o yaml > ${ns}/${deploy}.yaml
done

cd ${init_dir}/statefulsets
kubectl get statefulsets --all-namespaces |grep -vE "^NAME"|while read line
do
echo $line
ns=`echo $line|awk '{print $1}'`
deploy=`echo $line|awk '{print $2}'`
mkdir -p ${ns}
kubectl get statefulsets ${deploy} -n ${ns} -o yaml > ${ns}/${deploy}.yaml
done
#####

cd ${init_dir}/configmap
kubectl get configmap --all-namespaces |grep -vE "^NAME"|while read line
do
echo $line
ns=`echo $line|awk '{print $1}'`
deploy=`echo $line|awk '{print $2}'`
mkdir -p ${ns}
kubectl get configmap ${deploy} -n ${ns} -o yaml > ${ns}/${deploy}.yaml
done

cd ${init_dir}/secrets
kubectl get secrets --all-namespaces |grep -vE "^NAME"|while read line
do
echo $line
ns=`echo $line|awk '{print $1}'`
deploy=`echo $line|awk '{print $2}'`
mkdir -p ${ns}
kubectl get secrets ${deploy} -n ${ns} -o yaml > ${ns}/${deploy}.yaml
done

cd ${init_dir}/services
kubectl get service --all-namespaces |grep -vE "^NAME"|while read line
do
echo $line
ns=`echo $line|awk '{print $1}'`
deploy=`echo $line|awk '{print $2}'`
mkdir -p ${ns}
kubectl get service ${deploy} -n ${ns} -o yaml > ${ns}/${deploy}.yaml
done

cd ${init_dir}/ingresses
kubectl get ingresses --all-namespaces |grep -vE "^NAME"|while read line
do
echo $line
ns=`echo $line|awk '{print $1}'`
deploy=`echo $line|awk '{print $2}'`
mkdir -p ${ns}
kubectl get ingresses ${deploy} -n ${ns} -o yaml > ${ns}/${deploy}.yaml
done
####

cd ${init_dir}/sa
kubectl get sa --all-namespaces |grep -vE "^NAME"|while read line
do
echo $line
ns=`echo $line|awk '{print $1}'`
deploy=`echo $line|awk '{print $2}'`
mkdir -p ${ns}
kubectl get sa ${deploy} -n ${ns} -o yaml > ${ns}/${deploy}.yaml
done

cd ${init_dir}/clusterrole
kubectl get clusterrole --all-namespaces |grep -vE "^NAME"|while read line
do
echo $line
deploy=`echo $line|awk '{print $1}'`
kubectl get clusterrole ${deploy} -o yaml > ${deploy}.yaml
done

cd ${init_dir}/clusterrolebinding
kubectl get clusterrolebinding --all-namespaces |grep -vE "^NAME"|while read line
do
echo $line
deploy=`echo $line|awk '{print $1}'`
kubectl get clusterrolebinding ${deploy} -o yaml > ${deploy}.yaml
done

cd ${init_dir}/nodes
kubectl get nodes --all-namespaces |grep -vE "^NAME"|while read line
do
echo $line
deploy=`echo $line|awk '{print $1}'`
kubectl get nodes ${deploy} -o yaml > ${deploy}.yaml
done

cd ${init_dir}/deployments
ls */*|grep .yaml$|grep -vE "^auth-center|^apollo|^ops-monit|^kube-|^default"|while read line
do
echo "$line"
ns=`echo "$line"|awk -F / '{print $1}'`
deploy=`echo "$line"|awk -F / '{print $2}'|sed s/^deploy-//g|sed s/.yaml//g`
tag=`grep "image:" $line|grep -v busybox|head -1|awk -F ": " '{print $NF}'|sed "s/'//g"|sed 's/ $//g'|sed 's/"//g'`
echo "$ns $deploy $tag" >>../deploy.list
done

if [ $(whoami) == "cassmall-gamma" ];then
kubectl get deployments --all-namespaces |grep -E "^cis|^terminal"|while read line
do
echo $line
ns=`echo $line|awk '{print $1}'`
deploy=`echo $line|awk '{print $2}'`
pod=`kubectl get pods -n ${ns} |grep ${deploy}|grep Running |head -1|awk '{print $1}'`
kubectl cp ${ns}/${pod}:/usr/src/app/config/backup.json ${init_dir}/files/${ns}.${deploy}_config_backup.json
kubectl cp ${ns}/${pod}:/usr/src/app/config/backup.js ${init_dir}/files/${ns}.${deploy}_config_backup.js
done
fi

mv ${init_dir} ${init_dir}_`/usr/bin/date "+%Y.%m.%d"`

上一篇:页面下载多个文件创建临时压缩包调用浏览器自带弹窗下载


下一篇:【二】基于open62541的服务器配置