Longhorn can easily restore backups to a volume. For more information on how backups work, refer to the concepts section.

To programmatically restore backups you can use the generic kubernetes csi snapshot mechanism. To learn more about the CSI snapshot mechanism, click here.

Prerequisite

CSI snapshot support needs to be enabled on your cluster.

If your Kubernetes distribution does not provide the Kubernetes snapshot controller as well as the snapshot related custom resource definitions, you need to manually deploy them.

For more information, see Enable CSI Snapshot Support.

Restore a Backup via a VolumeSnapshot object

Create a PersistentVolumeClaim object where the dataSource field points to an existing VolumeSnapshot object.

The csi-provisioner will pick this up and instruct the Longhorn CSI driver to provision a new volume with the data from the associated backup.

You can use the same mechanism to restore Longhorn backups that have not been created via the CSI mechanism.

An example PersistentVolumeClaim is below. The dataSource field needs to point to an existing VolumeSnapshot object.

  1. apiVersion: v1
  2. kind: PersistentVolumeClaim
  3. metadata:
  4. name: test-restore-snapshot-pvc
  5. spec:
  6. storageClassName: longhorn
  7. dataSource:
  8. name: test-snapshot-pvc
  9. kind: VolumeSnapshot
  10. apiGroup: snapshot.storage.k8s.io
  11. accessModes:
  12. - ReadWriteOnce
  13. resources:
  14. requests:
  15. storage: 2Gi

Restore a Backup that Has No Associated VolumeSnapshot

To restore Longhorn backups that have not been created via the CSI mechanism, you have to first manually create a VolumeSnapshot and VolumeSnapshotContent object for the backup.

Create a VolumeSnapshotContent object with the snapshotHandle field set to bs://backup-volume/backup-name.

The backup-volume and backup-name values can be retrieved from the Backup page in the Longhorn UI.

  1. apiVersion: snapshot.storage.k8s.io/v1beta1
  2. kind: VolumeSnapshotContent
  3. metadata:
  4. name: test-existing-backup
  5. spec:
  6. volumeSnapshotClassName: longhorn
  7. driver: driver.longhorn.io
  8. deletionPolicy: Delete
  9. source:
  10. # NOTE: change this to point to an existing backup on the backupstore
  11. snapshotHandle: bs://test-vol/backup-625159fb469e492e
  12. volumeSnapshotRef:
  13. name: test-snapshot-existing-backup
  14. namespace: default

Create the associated VolumeSnapshot object with the name field set to test-snapshot-existing-backup, where the source field refers to a VolumeSnapshotContent object via the volumeSnapshotContentName field.

This differs from the creation of a backup, in which case the source field refers to a PerstistentVolumeClaim via the persistentVolumeClaimName field.

Only one type of reference can be set for a VolumeSnapshot object.

  1. apiVersion: snapshot.storage.k8s.io/v1beta1
  2. kind: VolumeSnapshot
  3. metadata:
  4. name: test-snapshot-existing-backup
  5. spec:
  6. volumeSnapshotClassName: longhorn
  7. source:
  8. volumeSnapshotContentName: test-existing-backup

Now you can create a PerstistantVolumeClaim object that refers to the newly created VolumeSnapshot object.

For an example see Restore a backup via a VolumeSnapshot object above.