Backup and Restore Kubernetes Configurations

Kubernetes configurations such as deployments, services, config maps, and secrets are critical components of your cluster. Backing them up and restoring them ensures you can recover from issues like accidental deletions, failed upgrades, or cluster migrations. This guide provides a detailed bash script to automate the backup and restore of Kubernetes configurations.


Key Features of the Script for Backup and restore Kubernetes configurations

  1. Backup all Kubernetes resources (deployments, services, config maps, secrets, etc.) for a specified namespace or all namespaces.
  2. Save backups in organized directories with timestamps.
  3. Restore configurations from a specific backup file.
  4. Provide options to verify the backup and restoration process.

Script:  kubernetes cluster backup and restore

Below is the bash script with detailed explanations and examples for each step.

#!/bin/bash

# Kubernetes Backup and Restore Script
# Author: [Your Name]
# Date: [Date]
# Version: 1.0

# Icons for visual feedback
CHECK="\u2714" # ✔
CROSS="\u274C" # ✘
INFO="\u2139"  # ℹ

# Define backup directory
BACKUP_DIR="./k8s_backup"

# Function: Check if kubectl is installed
function check_kubectl() {
    if ! command -v kubectl &> /dev/null; then
        echo -e "${CROSS} Error: kubectl is not installed. Please install kubectl to use this script."
        exit 1
    fi
    echo -e "${CHECK} kubectl is installed."
}

# Function: Backup Kubernetes resources
function backup_k8s() {
    local namespace=$1
    local timestamp=$(date +"%Y%m%d%H%M%S")
    local backup_path="$BACKUP_DIR/$timestamp"
    
    mkdir -p "$backup_path"

    echo -e "${INFO} Starting backup for namespace: ${namespace:-all namespaces}"
    
    if [[ -z "$namespace" ]]; then
        kubectl get all --all-namespaces -o yaml > "$backup_path/all_resources.yaml"
        kubectl get configmaps --all-namespaces -o yaml > "$backup_path/configmaps.yaml"
        kubectl get secrets --all-namespaces -o yaml > "$backup_path/secrets.yaml"
    else
        kubectl get all -n "$namespace" -o yaml > "$backup_path/all_resources.yaml"
        kubectl get configmaps -n "$namespace" -o yaml > "$backup_path/configmaps.yaml"
        kubectl get secrets -n "$namespace" -o yaml > "$backup_path/secrets.yaml"
    fi

    if [[ $? -eq 0 ]]; then
        echo -e "${CHECK} Backup completed successfully. Files saved to $backup_path"
    else
        echo -e "${CROSS} Error: Backup failed."
        exit 1
    fi
}

# Function: Restore Kubernetes resources
function restore_k8s() {
    local backup_path=$1
    
    if [[ ! -d "$backup_path" ]]; then
        echo -e "${CROSS} Error: Backup directory $backup_path does not exist."
        exit 1
    fi

    echo -e "${INFO} Starting restoration from backup: $backup_path"
    kubectl apply -f "$backup_path/all_resources.yaml"
    kubectl apply -f "$backup_path/configmaps.yaml"
    kubectl apply -f "$backup_path/secrets.yaml"

    if [[ $? -eq 0 ]]; then
        echo -e "${CHECK} Restoration completed successfully."
    else
        echo -e "${CROSS} Error: Restoration failed."
        exit 1
    fi
}

# Function: List available backups
function list_backups() {
    echo -e "${INFO} Available backups in $BACKUP_DIR:"
    ls -1 "$BACKUP_DIR"
}

# Main Menu
echo "=== Kubernetes Backup and Restore Script ==="
check_kubectl

echo "Choose an option:"
echo "1) Backup Kubernetes configurations"
echo "2) Restore Kubernetes configurations"
echo "3) List available backups"
echo "4) Exit"

read -p "Enter your choice: " choice

case $choice in
    1)
        read -p "Enter namespace to backup (leave blank for all namespaces): " namespace
        backup_k8s "$namespace"
        ;;
    2)
        list_backups
        read -p "Enter the backup directory to restore from: " backup_dir
        restore_k8s "$BACKUP_DIR/$backup_dir"
        ;;
    3)
        list_backups
        ;;
    4)
        echo -e "${CHECK} Exiting script."
        exit 0
        ;;
    *)
        echo -e "${CROSS} Invalid choice. Exiting."
        exit 1
        ;;
esac

Step-by-Step Explanation

1. Checking Prerequisites

The script checks if kubectl is installed:

if ! command -v kubectl &> /dev/null; then

If kubectl is not found, the script exits with an error message.

2. Backup Kubernetes Configurations

The backup_k8s function:

  1. Creates a timestamped backup directory.
  2. Retrieves Kubernetes configurations using kubectl get commands.
  3. Saves the output in YAML format.

Backup all namespaces:

kubectl get all --all-namespaces -o yaml > all_resources.yaml

Backup specific namespaces:

kubectl get all -n <namespace> -o yaml > all_resources.yaml

It also backs up config maps and secrets separately for finer control.

3. Restore Kubernetes Configurations

The restore_k8s function applies the backup files:

kubectl apply -f <file.yaml>

This recreates all resources from the YAML backup files.

4. List Available Backups

The script lists available backups in the backup directory:

ls -1 "$BACKUP_DIR"

5. Interactive Menu

The script provides an easy-to-use menu with options to:

  1. Backup configurations.
  2. Restore configurations.
  3. List available backups.

Examples

Backup All Namespaces

  1. Run the script:
    ./k8s_backup_restore.sh
    
  2. Choose the backup option:
    Choose an option:
    1) Backup Kubernetes configurations
    2) Restore Kubernetes configurations
    3) List available backups
    4) Exit
    Enter your choice: 1
    
  3. Leave the namespace blank for all namespaces:
    Enter namespace to backup (leave blank for all namespaces): 
    ✔ Backup completed successfully. Files saved to ./k8s_backup/20250109090000
    

Restore from a Backup

  1. Run the script:
    ./k8s_backup_restore.sh
    
  2. Choose the restore option:
    Choose an option:
    1) Backup Kubernetes configurations
    2) Restore Kubernetes configurations
    3) List available backups
    4) Exit
    Enter your choice: 2
    
  3. Select a backup directory:
    Available backups in ./k8s_backup:
    20250109090000
    Enter the backup directory to restore from: 20250109090000
    ℹ Starting restoration from backup: ./k8s_backup/20250109090000
    ✔ Restoration completed successfully.
    

Scenarios

Scenario 1: Regular Backups

Schedule this script to run daily using a cron job:

  1. Open crontab:
    crontab -e
    
  2. Add the cron job:
    0 2 * * * /path/to/k8s_backup_restore.sh 1>>/var/log/k8s_backup.log 2>&1
    

Scenario 2: Cluster Migration

Use this script to backup configurations from one cluster and restore them in another:

  1. Backup configurations from the source cluster.
  2. Switch to the target cluster using kubectl config use-context.
  3. Restore configurations.

Technical Details

  1. Resource Backup:
    • kubectl get all fetches deployments, pods, services, etc.
    • kubectl get configmaps and kubectl get secrets ensure additional configuration data is preserved.
  2. Restoration Process:
    • YAML files retain the original state, making restoration seamless.
    • kubectl apply ensures resources are created or updated.
  3. Namespace Management:
    • Specifying namespaces prevents unnecessary resources from being backed up.
  4. Directory Structure: Backups are timestamped for easy identification and rollback.

Benefits of This Approach

  1. Reliability: Ensures critical Kubernetes configurations are always recoverable.
  2. Flexibility: Supports namespace-specific and all-namespace backups.
  3. Automation-Friendly: Can be easily integrated with CI/CD pipelines or cron jobs.

Reference Doc

  1. kubernetes Documentation
    Official Kubernetes documentation, including guides on managing resources and using kubectl.
  2. Backing Up and Restoring with Velero
    Velero is a popular open-source tool for backup and recovery of Kubernetes clusters.
  3. Kubernetes Disaster Recovery Strategies
    Google’s guide on disaster recovery for Kubernetes applications.
  4. kubectl Command Cheat Sheet
    A quick reference for kubectl commands, helpful for creating backups and restores.
  5. Cluster Data Management with etcd
    Information about etcd, the backend store for Kubernetes.
  6. How to Configure Kubernetes on Ubuntu

 

Related articles

GCP IAM roles vs permissions explained

GCP IAM Roles vs Permissions Explained: A Senior Architect’s Guide to Secure Access The distinction between GCP IAM roles...

Artificial Intelligence Terminology

Artificial Intelligence Terminology Introduction to Artificial Intelligence Terminology Before diving deep into the concepts of artificial intelligence (AI), it is...

Deepseek vs ChatGPT

Deepseek vs ChatGPT The artificial intelligence landscape has been significantly influenced by two prominent models: OpenAI's ChatGPT and DeepSeek's...

How to Create and Manage RDS Databases on AWS

📊 How to Create and Manage RDS Databases on AWS: A Complete Guide Managing databases efficiently is a cornerstone...