script backup namespace,deployment etc.. from kubernetes
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If you want a lightweight backup of Kubernetes configuration objects such as namespaces, deployments, services, and config maps, the simplest approach is to export their YAML definitions with kubectl. This is useful for configuration recovery and auditing, but it is not the same as full disaster recovery because it does not automatically include persistent volume data, secrets policy decisions, or cluster-wide control-plane state.
A Basic Backup Script
The following shell script exports common namespaced resources into a timestamped directory:
This creates one folder per namespace and stores the exported manifests there.
Why This Is Useful
This type of backup helps with:
- recreating workloads after accidental deletion
- reviewing cluster configuration over time
- migrating definitions to another cluster
- keeping an external snapshot of declarative objects
If your workloads are already fully defined in GitOps manifests or Helm charts, this export may be less important as a source of truth, but it is still useful as a cluster-state snapshot.
Clean Up the YAML Before Restore
Raw kubectl get -o yaml output includes fields that should not usually be applied back directly, such as:
- '
status' - resource versions
- UIDs
- managed fields
For a restore-friendly export, strip those fields before reuse. A common pattern is to process the YAML with yq, but even without that, treat the backup as a reference snapshot first and a direct restore input second.
Namespace-Specific Backup
If you only care about one namespace, keep the script narrower:
This is often enough for application-level backup workflows.
Know the Limits
Manifest export is not a complete Kubernetes backup strategy by itself. It does not automatically preserve:
- persistent volume contents
- external cloud resources created by controllers
- RBAC expectations outside the exported objects
- cluster add-on state or CRD definitions unless you include them explicitly
For fuller backup and restore workflows, tools such as Velero are often a better fit than ad hoc shell scripts.
Common Pitfalls
The most common mistake is assuming exported YAML is sufficient to restore everything exactly as it was. Persistent data and cloud-integrated resources usually need separate backup logic.
Another issue is restoring raw manifests without removing generated metadata such as resourceVersion or status. Those fields often make restore attempts fail or behave unpredictably.
A third pitfall is exporting only Deployment objects and forgetting the related services, config maps, ingress resources, secrets, and namespace definitions that the workloads depend on.
Finally, back up secrets carefully. Including them in plain YAML may be necessary operationally, but it also changes the security profile of your backup storage immediately.
Summary
- '
kubectl get -o yamlscripts are a simple way to back up Kubernetes object definitions.' - Export common namespaced resources into a timestamped directory structure.
- Treat raw YAML exports as configuration snapshots, not perfect one-step restores.
- Remember that persistent volume data and some external resources need separate backup handling.
- Use dedicated backup tools when you need full cluster recovery rather than lightweight manifest export.
Related reading
- Script to continuously follow kubectl get pods
- seccompunconfined for a container in a kubernetes pod? Or changing default in docker 1.10?
- Secret management in Helm Charts
- Secure access to a private helm repository
- Script for running port-forward automatically
- Security Group and Subnet Belongs to different networks
- Security Yaml Bomb user can restart kube-api by sending configmap
- securityContext.privileged Forbidden disallowed by cluster policy

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.