备份资源类型列表
RESOURCES=(
"pods"
"services"
"deployments"
"configmaps"
"secrets"
"persistentvolumeclaims"
"statefulsets"
"daemonsets"
"replicasets")
bash
#!/bin/bash
###
# @Description: 一键备份K8s集群YAML文件脚本
# @Author: hanxiong
# @Mail: nnaigos@gmail.com
# @Date: 2022-05-27 10:54:24
# @LastEditTime: 2023-05-27 10:54:24
# @LastEditors: hanxiong
###
# 定时任务示例
# 0 0 */1 * * /usr/bin/bash /data/scritps/k8s-yaml-all-bak/k8s_yaml_backup.sh > /data/scritps/k8s-yaml-all-bak/yaml_bak.logs 2>&1
set -o nounset # Treat unset variables as an error
# 当前时间
DATE=$(date +%Y-%m-%d--%H-%M-%S)
# 备份路径
K8S_YAML_BACKUP_DIR="/data/backup/k8s_yaml_bak"
# 脚本存放目录
K8S_YAML_SHELL_DIR="/data/scritps/k8s-yaml-all-bak"
# 创建备份目录
if [ ! -d $K8S_YAML_BACKUP_DIR ];then
mkdir -p $K8S_YAML_BACKUP_DIR
fi
# 资源类型列表
RESOURCES=("pods" "services" "deployments" "configmaps" "secrets" "persistentvolumeclaims" "statefulsets" "daemonsets" "replicasets")
# 每次备份单独创建一个家目录+时间
mkdir -p ${K8S_YAML_BACKUP_DIR}/k8s-${DATE}
# 获取家目录
GET_HOME_DIR=$(ls -l ${K8S_YAML_BACKUP_DIR} | tail -n 1 | awk '{print $9}')
echo "备份路径:$K8S_YAML_BACKUP_DIR/$GET_HOME_DIR"
# 获取k8s名称空间
NAMESPACE=$(kubectl get ns | awk '{print $1}' | tail -n +2)
DumpYaml(){
# 遍历NS
for NS in $NAMESPACE ;do
# 创建NS备份目录
mkdir -p ${K8S_YAML_BACKUP_DIR}/${GET_HOME_DIR}/${NS}
# 过滤NS(kube-public、kube-system)
if [[ $NS != "kube-public" && $NS != "kube-system" && $NS != "csdr" && $NS != "kube-node-lease" ]]; then
# 遍历k8s资源
for RESOURCE in ${RESOURCES[@]}; do
# 创建资源目录
mkdir -p ${K8S_YAML_BACKUP_DIR}/${GET_HOME_DIR}/${NS}/${RESOURCE}
# 遍历对应资源名称
for RESOURCE_NAME in $(kubectl get $RESOURCE -n $NS | awk '{print $1}' | tail -n +2);do
DATE_YAML=$(date +%Y-%m-%d--%H:%M:%S)
echo "${DATE_YAML} 导出YAML: ${NS} ${RESOURCE} ${RESOURCE_NAME} "
# 导出对应名称空间下对应资源的yaml
kubectl get ${RESOURCE} ${RESOURCE_NAME} -n ${NS} -o yaml > ${K8S_YAML_BACKUP_DIR}/${GET_HOME_DIR}/${NS}/${RESOURCE}/${RESOURCE_NAME}.yaml
echo ""
done
done
fi
done
}
DelYaml(){
for DIR_NAME in $(find ${K8S_YAML_BACKUP_DIR} -type d -mtime +7);do
DATE_YAML=$(date +%Y-%m-%d--%H:%M:%S)
echo "${DATE_YAML} 删除:$DIR_NAME" > ${K8S_YAML_SHELL_DIR}/del-yaml.logs
rm -rf ${DIR_NAME} >> ${K8S_YAML_SHELL_DIR}/del-yaml.logs
echo "" >> ${K8S_YAML_SHELL_DIR}/del-yaml.logs
done
}
DumpYaml
DelYaml
#!/bin/bash
###
# @Description: 一键备份K8s集群YAML文件脚本
# @Author: hanxiong
# @Mail: nnaigos@gmail.com
# @Date: 2022-05-27 10:54:24
# @LastEditTime: 2023-05-27 10:54:24
# @LastEditors: hanxiong
###
# 定时任务示例
# 0 0 */1 * * /usr/bin/bash /data/scritps/k8s-yaml-all-bak/k8s_yaml_backup.sh > /data/scritps/k8s-yaml-all-bak/yaml_bak.logs 2>&1
set -o nounset # Treat unset variables as an error
# 当前时间
DATE=$(date +%Y-%m-%d--%H-%M-%S)
# 备份路径
K8S_YAML_BACKUP_DIR="/data/backup/k8s_yaml_bak"
# 脚本存放目录
K8S_YAML_SHELL_DIR="/data/scritps/k8s-yaml-all-bak"
# 创建备份目录
if [ ! -d $K8S_YAML_BACKUP_DIR ];then
mkdir -p $K8S_YAML_BACKUP_DIR
fi
# 资源类型列表
RESOURCES=("pods" "services" "deployments" "configmaps" "secrets" "persistentvolumeclaims" "statefulsets" "daemonsets" "replicasets")
# 每次备份单独创建一个家目录+时间
mkdir -p ${K8S_YAML_BACKUP_DIR}/k8s-${DATE}
# 获取家目录
GET_HOME_DIR=$(ls -l ${K8S_YAML_BACKUP_DIR} | tail -n 1 | awk '{print $9}')
echo "备份路径:$K8S_YAML_BACKUP_DIR/$GET_HOME_DIR"
# 获取k8s名称空间
NAMESPACE=$(kubectl get ns | awk '{print $1}' | tail -n +2)
DumpYaml(){
# 遍历NS
for NS in $NAMESPACE ;do
# 创建NS备份目录
mkdir -p ${K8S_YAML_BACKUP_DIR}/${GET_HOME_DIR}/${NS}
# 过滤NS(kube-public、kube-system)
if [[ $NS != "kube-public" && $NS != "kube-system" && $NS != "csdr" && $NS != "kube-node-lease" ]]; then
# 遍历k8s资源
for RESOURCE in ${RESOURCES[@]}; do
# 创建资源目录
mkdir -p ${K8S_YAML_BACKUP_DIR}/${GET_HOME_DIR}/${NS}/${RESOURCE}
# 遍历对应资源名称
for RESOURCE_NAME in $(kubectl get $RESOURCE -n $NS | awk '{print $1}' | tail -n +2);do
DATE_YAML=$(date +%Y-%m-%d--%H:%M:%S)
echo "${DATE_YAML} 导出YAML: ${NS} ${RESOURCE} ${RESOURCE_NAME} "
# 导出对应名称空间下对应资源的yaml
kubectl get ${RESOURCE} ${RESOURCE_NAME} -n ${NS} -o yaml > ${K8S_YAML_BACKUP_DIR}/${GET_HOME_DIR}/${NS}/${RESOURCE}/${RESOURCE_NAME}.yaml
echo ""
done
done
fi
done
}
DelYaml(){
for DIR_NAME in $(find ${K8S_YAML_BACKUP_DIR} -type d -mtime +7);do
DATE_YAML=$(date +%Y-%m-%d--%H:%M:%S)
echo "${DATE_YAML} 删除:$DIR_NAME" > ${K8S_YAML_SHELL_DIR}/del-yaml.logs
rm -rf ${DIR_NAME} >> ${K8S_YAML_SHELL_DIR}/del-yaml.logs
echo "" >> ${K8S_YAML_SHELL_DIR}/del-yaml.logs
done
}
DumpYaml
DelYaml