Files
CmiiDeploy/998-常用脚本/备份脚本/按APP切分备份命名空间.sh
2026-05-19 14:28:44 +08:00

111 lines
4.0 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 定义命名空间
namespace="命名空间"
# 创建一个带时间戳的备份目录,保持目录整洁
backup_dir="backup_${namespace}_$(date +%Y%m%d%H%M%S)"
mkdir -p "${backup_dir}"
# YQ 清理规则:去除指定的 metadata 字段以及整个 status 字段
# 注意:加入了 .metadata.generation这在 deployment/statefulset 中也很常见
yq_filter='del(.metadata.managedFields, .metadata.annotations, .metadata.creationTimestamp, .metadata.uid, .metadata.resourceVersion, .metadata.selfLink, .metadata.generation, .status)'
install_yq() {
echo "=> 检查并安装 yq 工具..."
if ! command -v yq &> /dev/null; then
wget https://oss.demo.uavcmlc.com/cmlc-installation/downloadfile/amd/yq_linux_amd64 -O /usr/local/bin/yq
# wget https://oss.demo.uavcmlc.com/cmlc-installation/downloadfile/aarch/yq_linux_arm64 -O /usr/local/bin/yq
chmod +x /usr/local/bin/yq
echo "yq 安装完成!"
else
echo "yq 已安装,跳过。"
fi
echo ""
}
# 备份单个应用的函数
backup_single_app() {
local app_name=$1
local target_file="${backup_dir}/${app_name}.yaml"
echo "=> 正在备份应用: ${app_name}"
# 创建空文件或清空已有文件
> "${target_file}"
# 1. 备份 Deployment
if kubectl get deployment "${app_name}" -n "${namespace}" &>/dev/null; then
echo "---" >> "${target_file}"
kubectl get deployment "${app_name}" -n "${namespace}" -o yaml | yq eval "${yq_filter}" - >> "${target_file}"
fi
# 2. 备份 StatefulSet
if kubectl get statefulset "${app_name}" -n "${namespace}" &>/dev/null; then
echo "---" >> "${target_file}"
kubectl get statefulset "${app_name}" -n "${namespace}" -o yaml | yq eval "${yq_filter}" - >> "${target_file}"
fi
# 3. 备份 Service (假设 Service 名称与应用名称一致)
if kubectl get service "${app_name}" -n "${namespace}" &>/dev/null; then
echo "---" >> "${target_file}"
kubectl get service "${app_name}" -n "${namespace}" -o yaml | yq eval "${yq_filter}" - >> "${target_file}"
fi
# 4. 备份 ConfigMap (假设主 ConfigMap 名称与应用名称一致)
if kubectl get configmap "${app_name}" -n "${namespace}" &>/dev/null; then
echo "---" >> "${target_file}"
kubectl get configmap "${app_name}" -n "${namespace}" -o yaml | yq eval "${yq_filter}" - >> "${target_file}"
fi
}
backup_ingress() {
echo "=> 正在单独备份所有 Ingress 资源..."
local ingress_file="${backup_dir}/all-ingress-${namespace}.yaml"
# 检查是否存在 ingress 资源
if [ "$(kubectl get ingress -n "${namespace}" --no-headers 2>/dev/null | wc -l)" -gt 0 ]; then
# 因为 get ingress 拿到的是一个 List所以需要加 .items[] 遍历处理
kubectl get ingress -n "${namespace}" -o yaml | yq eval '.items[] | '"${yq_filter}" - > "${ingress_file}"
# 在每个 apiVersion 前面加上 --- 分隔符
sed -i '/^apiVersion:/i ---' "${ingress_file}"
echo "Ingress 备份完成: ${ingress_file}"
else
echo "命名空间 ${namespace} 下未找到 Ingress 资源。"
fi
echo ""
}
main() {
install_yq
echo "================================================="
echo "开始执行命名空间 ${namespace} 的多应用切分备份"
echo "备份目录: ${backup_dir}"
echo "================================================="
# 获取该命名空间下所有的 Deployment 和 StatefulSet 的名称,作为应用的基准列表
# 使用 sort -u 去重,防止某些同名情况
app_list=$(kubectl get deployments,statefulsets -n "${namespace}" -o custom-columns=NAME:.metadata.name --no-headers | sort -u)
if [ -z "${app_list}" ]; then
echo "未在命名空间中找到任何 Deployment 或 StatefulSet。"
exit 0
fi
# 遍历每个应用名称,执行合并备份
for app in ${app_list}; do
backup_single_app "${app}"
done
echo ""
# 单独备份 Ingress
backup_ingress
echo "================================================="
echo "所有资源备份完毕!文件已存放在 ${backup_dir}/ 目录下。"
echo "================================================="
}
# 执行主流程
main