Skip to content

Instantly share code, notes, and snippets.

@MahdadGhasemian
Last active January 21, 2025 03:01
Show Gist options
  • Save MahdadGhasemian/0a0a88158d4c07f8082352aad488804e to your computer and use it in GitHub Desktop.
Save MahdadGhasemian/0a0a88158d4c07f8082352aad488804e to your computer and use it in GitHub Desktop.
Steps to restore backed-up data using Longhorn, with a PostgreSQL example.

Restore Backup with Longhorn

Instructions for restoring data using Longhorn backup and restore features.

Restore Backup

Example: Restoring PostgreSQL Data

  1. Scale Down the PostgreSQL StatefulSet:

    kubectl scale statefulset.apps/postgresql --replicas=0 -n postgresql
  2. Wait for the Volume to Detach:

    • Monitor the volume status until it is detached.
  3. Note the Current Volume Name:

    • For example: pvc-1be4dafb-399f-40d4-ac87-205eb56c2f44.
  4. Delete the Old Volume via Longhorn GUI:

    • Navigate to the Longhorn GUI and delete the specified volume.
  5. Restore the Backup to a New Volume:

    • Use the Longhorn GUI to restore the backup to a new volume.
    • Assign the new volume the same name noted in step 3 (pvc-1be4dafb-399f-40d4-ac87-205eb56c2f44).
  6. Wait for the Restore Process to Complete:

    • Monitor the Longhorn GUI until the restore process finishes.
  7. Create Persistent Volume (PV) / Persistent Volume Claim (PVC):

    • Navigate to the lonhorn GUI > Volume > at the right side of volume named pvc-1be4dafb-399f-40d4-ac87-205eb56c2f44 open operation button > select Create PV/PVC
    • Ensure the Create PVC option is checked.
    • Ensure the Use Previous PVC option is checked.

longhorn-volume-create-pv-pvc

longhorn-create-pv-pvc

  1. Wait for the PV/PVC to be Available:

    • Monitor the status until the PV/PVC is available and ready for use.
  2. Scale Up the PostgreSQL StatefulSet:

    kubectl scale statefulset.apps/postgresql --replicas=1 -n postgresql

By following these steps, you can successfully restore your data using Longhorn.

@MahdadGhasemian
Copy link
Author

How to scale down and up a postgresql statefulset :

kubectl scale statefulset.apps/postgresql --replicas=0 -n postgresql
kubectl scale statefulset.apps/postgresql --replicas=1 -n postgresql

How disable auto-sync in the argocd application and then scale it down and up :

1- Select the application and click on the DETAILS button up left corner :

Screenshot from 2024-10-27 21-32-25

2- At the Sync Policy section, hit the DISABLE AUTO-SYNC :

Screenshot from 2024-10-27 21-32-38

3- Now you can easily manage to scale down or up :

kubectl scale deployment.apps/storage --replicas=0 -n app-production-storage
kubectl scale deployment.apps/storage --replicas=1 -n app-production-storage

@MahdadGhasemian
Copy link
Author

How to force a pvc to be deleted :

1- Read the pvc's detail :

kubectl get pvc storage-pv-claim -n app-production-storage -o yaml

2- Set it's finalizers to null :

kubectl patch pvc storage-pv-claim -p '{"metadata":{"finalizers":null}}' -n app-production-storage

3- Now you can delete it easily :

kubectl delete pvc storage-pv-claim -n app-production-storage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment