Backup and restore scenarios
A reference guide for managing Backup and Restore in Kubernetes using Velero.
Note: If your application has been upgraded and you want to restore it to
an older version from a Velero backup, use the
--existing-resource-policy
update flag. This ensures that existing resources are updated to match the backup, even if they
already
exist.velero restore create --from-backup <backup_name> --existing-resource-policy updateCreate backup of a namespace
velero backup create <Backup_Name> --include-namespaces <Namespace> -n backup
velero backup get -n backup
- What it does:
- Creates a backup of all resources inside the specified namespace.
- When to use:
- To save the current state of all resources (pods, services, etc.) in a namespace.
velero backup getlists all backups.
Restore backup to the same namespace
velero restore create --from-backup <Backup_Name> -n backup
velero restore get -n backup
- What it does:
- Restores the backup to the original namespace.
- When to use:
- To recover or duplicate resources in the same namespace.
velero restore getlists all restore jobs and their status.
Restore backup to a different namespace
velero restore create --from-backup <Backup_Name> --namespace-mappings <Original_Namespace>:<New_Namespace> -n backup
velero restore get -n backup
- What it does:
- Restores the backup but remaps resources to a new namespace.
- When to use:
- To move or copy resources between namespaces.
Backup all cluster-level resources
velero backup create <Backup_Name> --include-cluster-resources=true -n backup
velero backup get -n backup
- What it does:
- Backs up cluster-wide resources, like custom resource definitions (CRDs), roles, storage classes.
- When to use:
- To create a full backup of your Kubernetes cluster state.
Backup only specific cluster resources
velero backup create full-backup --include-resources='customresourcedefinitions,clusterroles,clusterrolebindings,namespaces,persistentvolumes,persistentvolumeclaims,storageclasses,mutatingwebhookconfigurations,validatingwebhookconfigurations' --include-cluster-resources=true -n backup
velero backup get -n backup
- What it does:
- Backs up only selected critical cluster-wide resources.
- When to use:
- To back up only important clsuter components.
Backup PersistentVolumeClaims (PVCs) across all namespaces
velero backup create <Backup_Name> --include-resources persistentvolumeclaims --include-namespaces '*' -n backup
velero backup get -n backup
- What it does:
- Backs up PVCs in every namespace.
- When to use:
- To protect storage claims separately from pods or other resources.
Backup PersistentVolumes (PVs) and PVCs cluster-wide
velero backup create <Backup_Name> --include-resources=persistentvolumeclaims,persistentvolumes --include-namespaces '*' --include-cluster-resources=true -n backup velero backup get -n backup
- What it does:
- Backs up both PVs and PVCs for the entire cluster.
- When to use:
- To have a full backup of storage resources.
Backup namespace without pods
velero backup create <Backup_Name> --include-namespaces <Namespace> --exclude-resources pods -n backup
- What it does:
- Backs up everything except pods in the namespace.
- When to use:
- Pods are often recreated automatically. This saves space and time.
Backup resources using label selectors
First, label a pod.
kubectl label pod nginx app=web -n <Namespace>
Then, create the
backup:
velero backup create label-backup --selector app=web --include-namespaces <Namespace> -n backup
velero backup get -n backup- What it does:
- Backs up only resources with a specific label.
- When to use:
- To back up selective parts of your application.
Create backup with Expiration Time (TTL)
velero backup create <Backup_Name> --include-namespaces <Namespace> --ttl 90d -n backup velero backup get -n backup
- What it does:
- Creates a backup that expires automatically after 90 days.
- When to use:
- To manage storage by keeping backups only for a limited time.
View details of a backup
velero backup describe <Backup_Name> --details -n backup
- What it does:
- Shows detailed information about what resources were backed up.
- When to use:
- To verify the contents of your backup.
View logs for a backup
velero backup logs <Backup_Name> -n backup
- What it does:
- Displays logs that are generated during backup creation.
- When to use:
- To troubleshoot backup failures or issues.
Restore backup excluding pods to different namespace
velero restore create --from-backup <Backup_Name> --namespace-mappings <Original_Namespace>:<New_Namespace> -n backup
- What it does:
- Restores resources excluding pods into another namespace.
- When to use:
- To migrate resources without pods (pods will be recreated by controllers).
Schedule backups every mintue
velero schedule create <Schedule_Name> --schedule="*/1 * * * *" --include-namespaces <Namespace> -n backup velero schedule get -n backup
- What it does:
- Creates an automatic backup schedule that runs every minute.
velero schedule getlists all scheduled backups. - When to use:
- For very frequent backups during testing or for critical workloads.
Delete scheduled backups
velero schedule delete <Schedule_Name> -n backup
- What it does:
- Creates an automatic backup schedule that runs every minute.
velero schedule getlists all scheduled backups. - When to use:
- For very frequent backups during testing or for critical workloads.