Pod

#!/bin/bash

# 指定保存目录
SAVE_DIR="./pods_yaml"

# 创建保存目录
mkdir -p $SAVE_DIR

# 获取所有pod名称
PODS=$(kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.namespace}/{.metadata.name}{"\n"}{end}')

# 循环遍历所有pod,获取yaml文件并保存
for POD in $PODS
do
  NAMESPACE=$(echo $POD | cut -d '/' -f 1)
  NAME=$(echo $POD | cut -d '/' -f 2)
  kubectl get pod $NAME -n $NAMESPACE -o yaml > $SAVE_DIR/$NAMESPACE-$NAME.yaml
done

echo "保存完成!"

All 1

这里是备份Kubernetes各种资源为YAML文件的脚本,分别备份了ConfigMap、DaemonSet、StatefulSet、Deployment等资源:

#!/bin/bash

# Set the backup directory
backup_dir=K8sbackupv1--$(date +%Y%m%d_%H%M%S)
mkdir -p $backup_dir

# Backup ConfigMaps in all namespaces
for ns in $(kubectl get namespaces -o name | cut -d '/' -f 2); do
  mkdir -p $backup_dir/$ns
  kubectl get configmaps -o name -n $ns | xargs -I{} kubectl get {} -n $ns -o yaml > $backup_dir/$ns/configmaps.yaml
done

# Backup DaemonSets in all namespaces
for ns in $(kubectl get namespaces -o name | cut -d '/' -f 2); do
  mkdir -p $backup_dir/$ns
  kubectl get daemonsets -o name -n $ns | xargs -I{} kubectl get {} -n $ns -o yaml > $backup_dir/$ns/daemonsets.yaml
done

# Backup StatefulSets in all namespaces
for ns in $(kubectl get namespaces -o name | cut -d '/' -f 2); do
  mkdir -p $backup_dir/$ns
  kubectl get statefulsets -o name -n $ns | xargs -I{} kubectl get {} -n $ns -o yaml > $backup_dir/$ns/statefulsets.yaml
done

# Backup Deployments in all namespaces
for ns in $(kubectl get namespaces -o name | cut -d '/' -f 2); do
  mkdir -p $backup_dir/$ns
  kubectl get deployments -o name -n $ns | xargs -I{} kubectl get {} -n $ns -o yaml > $backup_dir/$ns/deployments.yaml
done

# Backup Secrets in all namespaces
for ns in $(kubectl get namespaces -o name | cut -d '/' -f 2); do
  mkdir -p $backup_dir/$ns
  kubectl get secrets -o name -n $ns | xargs -I{} kubectl get {} -n $ns -o yaml > $backup_dir/$ns/secrets.yaml
done

# Compress the backup directory
tar -czvf $backup_dir.tar.gz $backup_dir

# # Delete the backup directory
# rm -rf $backup_dir

您可以根据需要修改备份的资源类型,并将其保存到相应的 YAML 文件中。该脚本将逐个备份ConfigMap、DaemonSet、StatefulSet、Deployment等资源,以便您可以更好地控制备份的内容。

在这里插入图片描述

All 2

以下是备份Kubernetes所有资源为YAML文件的脚本,包括所有资源类型:

#!/bin/bash

# Set the backup directory
backup_dir=K8sbackup$(date +%Y%m%d_%H%M%S)
mkdir -p backup_dir

# Get all namespaced API resources that support the list verb
resources=$(kubectl api-resources --verbs=list --namespaced -o name)

# Backup each resource
for resource in $resources; do
  # Create a directory for each namespace
  for ns in $(kubectl get namespaces -o name | cut -d '/' -f 2); do
    mkdir -p $backup_dir/$ns
    # Backup the resource in the current namespace
    kubectl get $resource -n $ns -o yaml > $backup_dir/$ns/$resource.yaml
  done
done

# Compress the backup directory
tar -czvf $backup_dir.tar.gz $backup_dir

# # Delete the backup directory
# rm -rf $backup_dir

在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐