Documentation for version v1.7 is no longer actively maintained. The version you are currently viewing is a static snapshot. For up-to-date documentation, see the latest version.
Velero supports Restore Hooks, custom actions that can be executed during or after the restore process. There are two kinds of Restore Hooks:
Use an InitContainer
hook to add init containers into a pod before it’s restored. You can use these init containers to run any setup needed for the pod to resume running from its backed-up state.
The InitContainer added by the restore hook will be the first init container in the podSpec
of the restored pod.
In the case where the pod had volumes backed up using restic, then, the restore hook InitContainer will be added after the restic-wait
InitContainer.
NOTE: This ordering can be altered by any mutating webhooks that may be installed in the cluster.
There are two ways to specify InitContainer
restore hooks:
Below are the annotations that can be added to a pod to specify restore hooks:
init.hook.restore.velero.io/container-image
init.hook.restore.velero.io/container-name
init.hook.restore.velero.io/command
ENTRYPOINT
for the init container being added. This command is not executed within a shell and the container image’s ENTRYPOINT
is used if this is not provided.Use the below commands to add annotations to the pods before taking a backup.
$ kubectl annotate pod -n <POD_NAMESPACE> <POD_NAME> \
init.hook.restore.velero.io/container-name=restore-hook \
init.hook.restore.velero.io/container-image=alpine:latest \
init.hook.restore.velero.io/command='["/bin/ash", "-c", "date"]'
With the annotation above, Velero will add the following init container to the pod when it’s restored.
{
"command": [
"/bin/ash",
"-c",
"date"
],
"image": "alpine:latest",
"imagePullPolicy": "Always",
"name": "restore-hook"
...
}
Init container restore hooks can also be specified using the RestoreSpec
.
Please refer to the documentation on the
Restore API Type for how to specify hooks in the Restore spec.
Below is an example of specifying restore hooks in RestoreSpec
apiVersion: velero.io/v1
kind: Restore
metadata:
name: r2
namespace: velero
spec:
backupName: b2
excludedResources:
...
includedNamespaces:
- '*'
hooks:
resources:
- name: restore-hook-1
includedNamespaces:
- app
postHooks:
- init:
initContainers:
- name: restore-hook-init1
image: alpine:latest
volumeMounts:
- mountPath: /restores/pvc1-vm
name: pvc1-vm
command:
- /bin/ash
- -c
- echo -n "FOOBARBAZ" >> /restores/pvc1-vm/foobarbaz
- name: restore-hook-init2
image: alpine:latest
volumeMounts:
- mountPath: /restores/pvc2-vm
name: pvc2-vm
command:
- /bin/ash
- -c
- echo -n "DEADFEED" >> /restores/pvc2-vm/deadfeed
The hooks
in the above RestoreSpec
, when restored, will add two init containers to every pod in the app
namespace
{
"command": [
"/bin/ash",
"-c",
"echo -n \"FOOBARBAZ\" >> /restores/pvc1-vm/foobarbaz"
],
"image": "alpine:latest",
"imagePullPolicy": "Always",
"name": "restore-hook-init1",
"resources": {},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"volumeMounts": [
{
"mountPath": "/restores/pvc1-vm",
"name": "pvc1-vm"
}
]
...
}
and
{
"command": [
"/bin/ash",
"-c",
"echo -n \"DEADFEED\" >> /restores/pvc2-vm/deadfeed"
],
"image": "alpine:latest",
"imagePullPolicy": "Always",
"name": "restore-hook-init2",
"resources": {},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"volumeMounts": [
{
"mountPath": "/restores/pvc2-vm",
"name": "pvc2-vm"
}
]
...
}
Use an Exec Restore hook to execute commands in a restored pod’s containers after they start.
There are two ways to specify Exec
restore hooks:
If a pod has the annotation post.hook.restore.velero.io/command
then that is the only hook that will be executed in the pod.
No hooks from the restore spec will be executed in that pod.
Below are the annotations that can be added to a pod to specify exec restore hooks:
post.hook.restore.velero.io/container
post.hook.restore.velero.io/command
post.hook.restore.velero.io/on-error
Fail
and Continue
. Defaults to Continue
. With Continue
mode, execution failures are logged only. With Fail
mode, no more restore hooks will be executed in any container in any pod and the status of the Restore will be PartiallyFailed
. Optional.post.hook.restore.velero.io/exec-timeout
post.hook.restore.velero.io/wait-timeout
Use the below commands to add annotations to the pods before taking a backup.
$ kubectl annotate pod -n <POD_NAMESPACE> <POD_NAME> \
post.hook.restore.velero.io/container=postgres \
post.hook.restore.velero.io/command='["/bin/bash", "-c", "psql < /backup/backup.sql"]' \
post.hook.restore.velero.io/wait-timeout=5m \
post.hook.restore.velero.io/exec-timeout=45s \
post.hook.restore.velero.io/on-error=Continue
Exec restore hooks can also be specified using the RestoreSpec
.
Please refer to the documentation on the
Restore API Type for how to specify hooks in the Restore spec.
Below is an example of specifying restore hooks in a RestoreSpec
.
When using the restore spec it is possible to specify multiple hooks for a single pod, as this example demonstrates.
All hooks applicable to a single container will be executed sequentially in that container once it starts.
The ordering of hooks executed in a single container follows the order of the restore spec.
In this example, the pg_isready
hook is guaranteed to run before the psql
hook because they both apply to the same container and the pg_isready
hook is defined first.
If a pod has multiple containers with applicable hooks, all hooks for a single container will be executed before executing hooks in another container. In this example, if the postgres container starts before the sidecar container, both postgres hooks will run before the hook in the sidecar. This means the sidecar container may be running for several minutes before its hook is executed.
Velero guarantees that no two hooks for a single pod are executed in parallel, but hooks executing in different pods may run in parallel.
apiVersion: velero.io/v1
kind: Restore
metadata:
name: r2
namespace: velero
spec:
backupName: b2
excludedResources:
...
includedNamespaces:
- '*'
hooks:
resources:
- name: restore-hook-1
includedNamespaces:
- app
postHooks:
- exec:
execTimeout: 1m
waitTimeout: 5m
onError: Fail
container: postgres
command:
- /bin/bash
- '-c'
- 'while ! pg_isready; do sleep 1; done'
- exec:
container: postgres
waitTimeout: 6m
execTimeout: 1m
command:
- /bin/bash
- '-c'
- 'psql < /backup/backup.sql'
- exec:
container: sidecar
command:
- /bin/bash
- '-c'
- 'date > /start'
To help you get started, see the documentation.