Sub topic 14
Backup and restore hooks for consistent data
This chapter assumes you can create Velero backups and restores, and that you know how to annotate Pods and write a Deployment manifest. It also assumes you know what a sidecar container is.
Why consistency needs help
A volume backup copies bytes while an application may still be writing. A database mid-write can leave a copy that looks complete but fails to start. Velero hooks are commands that Velero runs inside your containers at set points. They let you quiesce (pause writes to) an application before the copy and resume it afterward.
The four hook points
| Hook | Runs | Typical use |
|---|---|---|
| Pre-backup | Before the Pod's data is backed up | Freeze the file system, flush buffers |
| Post-backup | After the backup of that Pod finishes | Unfreeze, resume writes |
| Restore init | In an init container, before app containers start | Prepare restored data |
| Restore exec | In a container after it starts | Run recovery or migration steps |
Velero runs backup hooks through the Kubernetes exec API, so the command must exist in the target container.
Defining hooks with annotations
The simplest method is Pod annotations. Put them in the Pod template so that replacement Pods keep them. This Deployment adds a privileged fsfreeze sidecar that shares the data volume. It assumes a PersistentVolumeClaim named web-data exists in the shop namespace.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: shop
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
annotations:
pre.hook.backup.velero.io/container: fsfreeze
pre.hook.backup.velero.io/command: '["/sbin/fsfreeze", "--freeze", "/data"]'
pre.hook.backup.velero.io/on-error: Fail
pre.hook.backup.velero.io/timeout: 30s
post.hook.backup.velero.io/container: fsfreeze
post.hook.backup.velero.io/command: '["/sbin/fsfreeze", "--unfreeze", "/data"]'
spec:
containers:
- name: app
image: nginx:1.27
volumeMounts:
- name: data
mountPath: /data
- name: fsfreeze
image: ubuntu:24.04
command: ["sleep", "infinity"]
securityContext:
privileged: true
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: web-dataThe command annotation is a JSON array, not a shell string. If you need pipes or redirects, use ["/bin/sh", "-c", "..."].
A restore hook uses the same pattern with different keys. This annotation runs a command in the app container after it starts:
post.hook.restore.velero.io/container: app
post.hook.restore.velero.io/command: '["/bin/sh", "-c", "echo restored > /data/RESTORED"]'
post.hook.restore.velero.io/wait-timeout: 5m
post.hook.restore.velero.io/exec-timeout: 1mCommon errors
- A frozen file system never thaws. If the post-backup hook fails, writes stay blocked. Set
timeoutand test the unfreeze path. - Wrong
on-errorexpectation.Failmarks the backup as partially failed.Continue(the default) keeps going, and you may get an inconsistent copy without noticing. - Command not found. The binary must be in the named container's image. If you omit
container, Velero uses the first container in the Pod. - Annotations on the Deployment, not the Pod template. Velero reads Pod annotations, so metadata on the Deployment itself has no effect.
Exercise
A backup of a Pod with a pre-backup hook succeeds, but the application cannot write afterward. Which hook is likely missing or failing, and what setting limits the damage?
Show answer
The post-backup hook that unfreezes the file system is missing or failing. Setting a timeout on the hooks and checking the backup with velero backup describe --details for hook errors limits and exposes the problem.