Hooks

TOC

Overview

Custom hooks are extension commands that run in containers within the cluster. By configuring hooks, you can perform tailored operations during backup and restore. For special configuration requirements, contact technical support.

Backup Hook

During backup execution, when a Pod is being backed up, one or more commands can be executed in containers within the Pod. These commands can run before (pre) or after (post) the Pod backup operation completes.

Hooks can be configured under the .spec.template.spec.hooks field of the schedule resource or the .spec.hooks field of the backup resource.

Configuration example and parameter descriptions:

hooks:
    resources:
      -
        # Hook name, displayed in logs.
        name: my-hook
        # (Optional) Namespaces to run the hook in (array). If not set, the hook runs in all namespaces.
        includedNamespaces:
        - '*'
        # (Optional) Namespaces to exclude (array).
        excludedNamespaces:
        - some-namespace
        # Resources to run the hook on. Currently supports only pods.
        includedResources:
        - pods
        # (Optional) Label selector for target resources.
        labelSelector:
          matchLabels:
            app: velero
            component: server
        # Pod actions to execute before backup (array). Only exec hooks are supported.
        pre:
          -
            exec:
              # (Optional) Name of container to execute the command in. If not set, defaults to the first container in the Pod.
              container: my-container
              # Command to execute (array). Required.
              command:
                - /bin/uname
                - -a
              # (Optional) Strategy for failed command execution: Continue or Fail. Default: Fail.
              onError: Fail
              # (Optional) Timeout for command execution. Default: 30s.
              timeout: 10s
        # Pod actions to execute after backup (array). Only exec hooks are supported.
        post:
          # Same as pre

Restore Hooks

The backup and restore component supports executing custom operations through hooks during or after the restore task.

Two types of restore hooks are supported:

  • initContainer restore hook: adds an init container to the restored Pod to perform necessary setup before the application container starts.

  • exec restore hook: used to execute commands or scripts in the container of the restored Pod.

Restore hooks can be set in the .spec.hooks field of the restore resource. Configuration example and parameter descriptions:

hooks:
    resources:
    # Hook name
    - name: restore-hook-1
      # (Optional) Namespaces to run the hook in (array). If not set, the hook runs in all namespaces.
      includedNamespaces:
      - ns1
      # (Optional) Namespaces to exclude (array).
      excludedNamespaces:
      - ns3
      # Resources to run the hook on. Currently supports only pods.
      includedResources:
      - pods
      # (Optional) Label selector for target resources.
      labelSelector:
        matchLabels:
          app: velero
          component: server
      # Hook list. Supports init and exec types.
      postHooks:
      - init:
          # List of containers to add as initContainers.
          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
      - exec:
        # (Optional) Name of container to execute the command in. If not set, defaults to the first container in the Pod.
        container: my-container
        # Command to execute (array). Required.
        command:
          - /bin/uname
          - -a
        # (Optional) Strategy for failed command execution: Continue or Fail. Default: Fail.
        onError: Fail
        # (Optional) Timeout for command execution. Default: 30s.
        timeout: 10s