To programmatically create backups, you can use the generic Kubernetes CSI snapshot mechanism. To learn more about the CSI snapshot mechanism, click .

    To create a backup using the CSI mechanism, create a Kubernetes object via kubectl. An example is here.

    Result: A backup is created. The VolumeSnapshot object creation leads to the creation of a VolumeSnapshotContent Kubernetes object.

    The VolumeSnapshotContent refers to a Longhorn backup in its VolumeSnapshotContent.snapshotHandle field with the name bs://backup-volume/backup-name.

    This creates a new Longhorn snapshot named snapshot-uuid.

    Then a backup of that snapshot is initiated, and the CSI request returns.

    Afterwards a VolumeSnapshotContent object named snapcontent-uuid is created.

    The CSI snapshotter sidecar periodically queries the Longhorn CSI plugin to evaluate the backup status.

    Once the backup is completed, the VolumeSnapshotContent.readyToUse flag is set to true.

    For information on how to restore a volume via a VolumeSnapshot object, refer to

    An example VolumeSnapshot object is below. The needs to point to the PVC of the Longhorn volume for which a backup should be created.

    The volumeSnapshotClassName field points to a VolumeSnapshotClass.

    We create a default class named longhorn, which uses Delete as its deletionPolicy.

    If you want the associated backup for a volume to be retained when the VolumeSnapshot is deleted, create a new VolumeSnapshotClass with set as the deletionPolicy.