Velero supports executing commands in containers in pods during a backup.
When performing a backup, you can specify one or more commands to execute in a container in a pod when that pod is being backed up. The commands can be configured to run before any custom action processing (“pre” hooks), or after all custom actions have been completed and any additional items specified by custom action have been backed up (“post” hooks). Note that hooks are not executed within a shell on the containers.
There are two ways to specify hooks: annotations on the pod itself, and in the Backup spec.
You can use the following annotations on a pod to make Velero execute a hook when backing up the pod:
Please see the documentation on the Backup API Type for how to specify hooks in the Backup spec.
This examples walks you through using both pre and post hooks for freezing a file system. Freezing the file system is useful to ensure that all pending disk I/O operations have completed prior to taking a snapshot.
The Velero example/nginx-app/with-pv.yaml serves as an example of adding the pre and post hook annotations directly to your declarative deployment. Below is an example of what updating an object in place might look like.
kubectl annotate pod -n nginx-example -l app=nginx \ pre.hook.backup.velero.io/command='["/sbin/fsfreeze", "--freeze", "/var/log/nginx"]' \ pre.hook.backup.velero.io/container=fsfreeze \ post.hook.backup.velero.io/command='["/sbin/fsfreeze", "--unfreeze", "/var/log/nginx"]' \ post.hook.backup.velero.io/container=fsfreeze
Now test the pre and post hooks by creating a backup. You can use the Velero logs to verify that the pre and post hooks are running and exiting without error.
velero backup create nginx-hook-test velero backup get nginx-hook-test velero backup logs nginx-hook-test | grep hookCommand
To use multiple commands, wrap your target command in a shell and separate them with
&&, or other shell conditional constructs.
pre.hook.backup.velero.io/command='["/bin/bash", "-c", "echo hello > hello.txt && echo goodbye > goodbye.txt"]'