Skip to content

Instantly share code, notes, and snippets.

@sumansrivastava
Created February 9, 2023 16:37
Show Gist options
  • Select an option

  • Save sumansrivastava/8808652c0fecb96fe84023c138148089 to your computer and use it in GitHub Desktop.

Select an option

Save sumansrivastava/8808652c0fecb96fe84023c138148089 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Set the name of the namespace
namespace="my-namespace"
# Create a backup directory
backup_dir="pdb-backup-$(date +%s)"
mkdir $backup_dir
# Get the names of all PDBs in the namespace
pdb_list=$(kubectl get pdb -n $namespace -o jsonpath='{.items[*].metadata.name}')
# Loop through the PDBs and backup their deployments and services
for pdb in $pdb_list; do
# Get the name of the deployment associated with the PDB
deployment=$(kubectl get pdb $pdb -n $namespace -o jsonpath='{.spec.selector.matchLabels.app}')
# Backup the deployment and its associated services
kubectl get deployment $deployment -n $namespace -o yaml > $backup_dir/$deployment.yaml
kubectl get service -l app=$deployment -n $namespace -o yaml > $backup_dir/$deployment-services.yaml
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment