Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

OpenShift APIs for Data Protection: VM pre-backup hooks

July 22, 2025
Doron Turgeman
Related topics:
Virtualization
Related products:
Red Hat OpenShiftRed Hat OpenShift Virtualization

Share:

    OpenShift APIs for Data Protection is an operator provided by Red Hat. It facilitates backup, recovery, and migration of applications, data, and Kubernetes resources in Red Hat OpenShift clusters. OpenShift APIs for Data Protection leverages Velero, an open source Kubernetes backup tool, and integrates it with cloud and storage providers to provide comprehensive data protection for Red Hat OpenShift Container Platform workloads.

    In a few scenarios, when backing up a virtual machine (VM), you need pre-migration hooks inside the virtual machine to:

    • Minimize the virtual machine size, save backup storage space, and purge unnecessary files such as log files.
    • Get Postgres consistent point-in-time snapshot for KVM.
    • Flush file system buffers.
    • Trigger custom application scripts.

    The classic OpenShift APIs for Data Protection hook solution (creating backup hooks) will not work for the guest VM since it can only run hooks inside a pod, but it does not have the permissions or configuration to log into the Kvirt guest VM to run the hook. This article describes how to execute operations before doing a VM backup with OpenShift APIs for Data Protection.

    VM pre-freeze hooks during backup

    It is possible to execute one or more scripts before the VM freezes. Failure or blocking of the script execution will not prevent the backup from being performed. Hence, scripts cannot be a condition to the backup.

    How to add your scripts

    The scripts will be automatically executed when located inside the virtual machine under /etc/qemu-ga/fsfreeze-hook.d.

    The hook script logs will be written to /var/log/qga-fsfreeze-hook.log.

    For example, say you need to purge files before VM backup. To do this, you will need to create hooks that run before the VM backup and perform the purge script to minimize the VM size before the backup.

    The following steps will help you achieve this:

    1. Log in to the VM.

      The folder /etc/qemu-ga contains:

      • Master script fsfreeze-hook that will trigger before VM freeze and will trigger scripts inside fsfreeze-hook.d.
      • A folder fsfreeze-hook.d  that creates scripts and places them in this folder to run before freeze.
    2. Add the following script purge.sh to delete the log files:
    #!/bin/bash
    # Path to the folder that contains the log files
    DIR="root/my-app/logs"
    if [ -d "$DIR" ]; 
    then
    rm -rf “$DIR”/*
    fi
    1. Ensure the created scripts are executable via chmod +x purge.sh.
    2. Ensure that the deleted files have write permissions for Linux and SELinux (see guidelines for SELinux below).

    Important points

    This documentation provides instructions on creating the VM backup with OADP. Likewise, these describe how to restore a VM with OADP.

    Note that these permission rules on files need to exist:

    1. The script runs as Linux root user permissions.
    2. If SELinux is enabled, follow the rules to allow the script to perform activities on the files. Refer to this blog for more information.
    3. First, run ausearch to generate SELinux denial module.
    4. Run semodule to apply the policy (see how in this blog which contains examples).
    5. To check the SELinux permissions:
      • For the target file, use ls -Z to see the SELinux user/role.
      • To understand the script's permissions, use echo $(id -Z) >> logfile.log to print the permissions.

     

    Related Posts

    • Back up Kubernetes persistent volumes using OADP

    • Kubernetes admission control with validating webhooks

    • Building virtual machines with Red Hat Developer Hub: The what, why, and how

    • Get started with OpenShift APIs for Data Protection

    Recent Posts

    • Cloud bursting with confidential containers on OpenShift

    • Reach native speed with MacOS llama.cpp container inference

    • A deep dive into Apache Kafka's KRaft protocol

    • Staying ahead of artificial intelligence threats

    • Strengthen privacy and security with encrypted DNS in RHEL

    What’s up next?

    This concise guide helps VMware administrators efficiently manage virtual machines (VMs) within the powerful OpenShift platform.

    Get the cheat sheet
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue