From 4607163d4e7d33d377f43b4798eeb3704c475d84 Mon Sep 17 00:00:00 2001 From: Ruben Hensen Date: Mon, 3 Mar 2025 22:40:24 +0100 Subject: [PATCH] Change schedule backup --- .gitignore | 3 +- backup-testing/readme.md | 139 +++++++ backup-testing/setup-credentials.sh | 39 ++ backup-testing/test-velero-backup.sh | 371 ++++++++++++++++++ .../restore-backup}/README.md | 0 .../restore-backup}/restore-ubuntu.yaml | 0 longhorn/volume-snapshot.yaml | 2 +- velero/backup-schedule.yaml | 6 +- 8 files changed, 557 insertions(+), 3 deletions(-) create mode 100644 backup-testing/readme.md create mode 100755 backup-testing/setup-credentials.sh create mode 100755 backup-testing/test-velero-backup.sh rename {restore-backup => disable/restore-backup}/README.md (100%) rename {restore-backup => disable/restore-backup}/restore-ubuntu.yaml (100%) diff --git a/.gitignore b/.gitignore index 642b1ff..23b2616 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ ente-photos-ref/ repomix-output.txt -credentials-velero \ No newline at end of file +credentials-velero +velero-credentials \ No newline at end of file diff --git a/backup-testing/readme.md b/backup-testing/readme.md new file mode 100644 index 0000000..a0b0652 --- /dev/null +++ b/backup-testing/readme.md @@ -0,0 +1,139 @@ +# Velero Backup Testing Guide + +This guide explains how to test your Velero backups using a temporary K3d cluster. + +## Prerequisites + +Ensure you have the following tools installed on your local machine: + +- Docker +- kubectl +- Helm +- K3d +- Velero CLI + +## Setup + +1. First, clone this repository to your local machine: + +```bash +git clone https://github.com/yourusername/backup-testing.git +cd backup-testing +``` + +2. Make the scripts executable: + +```bash +chmod +x setup-credentials.sh test-velero-backup.sh validate-restore.sh +``` + +3. Set up your MinIO credentials: + +```bash +./setup-credentials.sh +``` + +This script will look for credentials in `~/.velero/credentials-velero` or prompt you to create them. + +## Running a Backup Test + +Execute the test script with appropriate parameters: + +```bash +./test-velero-backup.sh \ + --s3-access-key YOUR_ACCESS_KEY \ + --s3-secret-key YOUR_SECRET_KEY \ + --original-namespace immich +``` + +This will: +1. Create a temporary K3d cluster +2. Install Longhorn for storage +3. Install Velero configured to access your MinIO backup location +4. List available backups +5. Allow you to test restoring a specific backup + +To restore a specific backup, run: + +```bash +./test-velero-backup.sh \ + --s3-access-key YOUR_ACCESS_KEY \ + --s3-secret-key YOUR_SECRET_KEY \ + --original-namespace immich \ + --backup-name specific-backup-name +``` + +## Testing Specific Applications + +### Immich + +For testing Immich backups: + +```bash +./test-velero-backup.sh \ + --s3-access-key YOUR_ACCESS_KEY \ + --s3-secret-key YOUR_SECRET_KEY \ + --original-namespace immich \ + --test-namespace immich-test +``` + +### NextCloud + +For testing NextCloud backups: + +```bash +./test-velero-backup.sh \ + --s3-access-key YOUR_ACCESS_KEY \ + --s3-secret-key YOUR_SECRET_KEY \ + --original-namespace nextcloud \ + --test-namespace nextcloud-test +``` + +## Testing with Volume Snapshots + +To test CSI volume snapshot backups: + +```bash +./test-velero-backup.sh \ + --s3-access-key YOUR_ACCESS_KEY \ + --s3-secret-key YOUR_SECRET_KEY \ + --original-namespace immich \ + --include-volume-snapshots +``` + +## Customizing Validation + +You can extend the `validate-restore.sh` script to perform application-specific validation checks: + +1. Edit the script to add application-specific checks +2. Run your test with the modified validation: + +```bash +./test-velero-backup.sh \ + --s3-access-key YOUR_ACCESS_KEY \ + --s3-secret-key YOUR_SECRET_KEY \ + --original-namespace immich \ + --backup-name your-backup \ + --custom-validation-script ./your-custom-validation.sh +``` + +## Troubleshooting + +If you encounter issues: + +1. Check the logs of the Velero pod: + ``` + kubectl logs -n velero deploy/velero + ``` + +2. Examine restore details: + ``` + velero restore describe RESTORE_NAME + velero restore logs RESTORE_NAME + ``` + +3. Check cluster resources: + ``` + kubectl get pods -A + kubectl get pvc -A + ``` diff --git a/backup-testing/setup-credentials.sh b/backup-testing/setup-credentials.sh new file mode 100755 index 0000000..b77cc96 --- /dev/null +++ b/backup-testing/setup-credentials.sh @@ -0,0 +1,39 @@ +#!/bin/bash +# setup-credentials.sh - Script to help setup credentials for Velero testing + +# Directory where Velero credentials are stored on the production system +# This would be your production system's credentials for accessing MinIO +VELERO_CREDS_PATH="${HOME}/.velero/credentials-velero" + +if [ -f "${VELERO_CREDS_PATH}" ]; then + echo "Found Velero credentials at ${VELERO_CREDS_PATH}" + # Extract AWS credentials from the file + AWS_ACCESS_KEY_ID=$(grep aws_access_key_id ${VELERO_CREDS_PATH} | cut -d= -f2 | tr -d '[:space:]') + AWS_SECRET_ACCESS_KEY=$(grep aws_secret_access_key ${VELERO_CREDS_PATH} | cut -d= -f2 | tr -d '[:space:]') + + if [ -n "${AWS_ACCESS_KEY_ID}" ] && [ -n "${AWS_SECRET_ACCESS_KEY}" ]; then + echo "Credentials found and will be used for backup testing" + + # Store them temporarily for the test script to use + cat > /tmp/velero-test-credentials < velero-credentials < /dev/null; then + echo "Error: Velero CLI is not installed or not in PATH" + echo "Please install the Velero CLI by following instructions at: https://velero.io/docs/main/basic-install/" + exit 1 +fi + +# Verify Velero is connected to the storage location +echo "Checking Velero backup storage location..." +kubectl -n velero get backupstoragelocation default -o jsonpath='{.status.phase}' | grep -q "Available" || { + echo "Warning: Backup storage location is not available, it may take a few minutes to initialize" + echo "Storage location status:" + kubectl -n velero get backupstoragelocation default -o yaml + + # Wait a bit longer for the location to become available + echo "Waiting 30 seconds for backup storage location to become available..." + sleep 30 +} + +# 4. List backups +echo "Checking for available backups..." +if ! velero_output=$(velero backup get 2>&1); then + echo "Error listing backups: $velero_output" + + # Try to verify S3 connectivity + echo "Checking S3 connectivity..." + + # Install AWS CLI if needed + if ! command -v aws &> /dev/null; then + echo "AWS CLI not found, skipping S3 connectivity test" + else + # Create temporary AWS profile + mkdir -p ~/.aws + cat > ~/.aws/credentials < ~/.aws/config < validate-restore.sh <<'EOF' +#!/bin/bash +# validate-restore.sh - Script to validate Velero backup restoration + +set -e +echo "Starting validation of restored resources..." + +# Get namespace from command line or use default +NAMESPACE="${1:-default}" + +# Check all deployments +echo "Checking deployments..." +deployments=$(kubectl get deployment -n $NAMESPACE -o name) +if [[ -z "$deployments" ]]; then + echo "❌ No deployments found in namespace $NAMESPACE" +else + echo "Found deployments: $deployments" + + # Check if pods are running for each deployment + for deployment in $deployments; do + name=$(echo $deployment | cut -d'/' -f2) + echo -n "Checking deployment $name: " + + # Check if pods are running + ready_replicas=$(kubectl get deployment -n $NAMESPACE $name -o jsonpath='{.status.readyReplicas}') + if [[ -n "$ready_replicas" && "$ready_replicas" != "0" ]]; then + echo "✅ $ready_replicas pods ready" + else + echo "❌ No pods running" + fi + done +fi + +# Check all services +echo "Checking services..." +services=$(kubectl get service -n $NAMESPACE -o name | grep -v "kubernetes") +if [[ -z "$services" ]]; then + echo "❌ No services found in namespace $NAMESPACE" +else + echo "Found services: $services" + + # Check if services have endpoints + for service in $services; do + name=$(echo $service | cut -d'/' -f2) + echo -n "Checking service $name: " + + # Check if service has endpoints + endpoints=$(kubectl get endpoints -n $NAMESPACE $name -o jsonpath='{.subsets[*].addresses[*].ip}') + if [ -n "$endpoints" ]; then + echo "✅ Has endpoints" + else + echo "❌ No endpoints" + fi + done +fi + +# Check PVCs +echo "Checking PVCs..." +pvcs=$(kubectl get pvc -n $NAMESPACE -o name) +if [[ -z "$pvcs" ]]; then + echo "No PVCs found in namespace $NAMESPACE" +else + pvc_count=$(echo "$pvcs" | wc -l) + echo "Found $pvc_count PVCs" + + # Check if PVCs are bound + bound_count=$(kubectl get pvc -n $NAMESPACE -o jsonpath='{.items[?(@.status.phase=="Bound")].metadata.name}' | wc -w) + echo "$bound_count/$pvc_count PVCs are bound" + + if [ "$bound_count" -ne "$pvc_count" ]; then + echo "❌ Not all PVCs are bound" + else + echo "✅ All PVCs are bound" + fi +fi + +echo "Validation complete!" +exit 0 +EOF + chmod +x validate-restore.sh +fi + +echo "Running validation tests..." +./validate-restore.sh $TEST_NAMESPACE + +echo "" +echo "=== Backup test process completed ===" +echo "The cluster will be deleted when you close this terminal or press Ctrl+C" +echo "To access the cluster manually, use: kubectl config use-context k3d-backup-test" +echo "" + +# Keep the script running until user terminates it +read -p "Press Enter to clean up and exit..." \ No newline at end of file diff --git a/restore-backup/README.md b/disable/restore-backup/README.md similarity index 100% rename from restore-backup/README.md rename to disable/restore-backup/README.md diff --git a/restore-backup/restore-ubuntu.yaml b/disable/restore-backup/restore-ubuntu.yaml similarity index 100% rename from restore-backup/restore-ubuntu.yaml rename to disable/restore-backup/restore-ubuntu.yaml diff --git a/longhorn/volume-snapshot.yaml b/longhorn/volume-snapshot.yaml index 809743d..2953df1 100644 --- a/longhorn/volume-snapshot.yaml +++ b/longhorn/volume-snapshot.yaml @@ -1,5 +1,5 @@ -kind: VolumeSnapshotClass apiVersion: snapshot.storage.k8s.io/v1 +kind: VolumeSnapshotClass metadata: name: longhorn-backup-vsc labels: diff --git a/velero/backup-schedule.yaml b/velero/backup-schedule.yaml index 393ba80..538c10f 100644 --- a/velero/backup-schedule.yaml +++ b/velero/backup-schedule.yaml @@ -3,8 +3,10 @@ kind: Schedule metadata: name: daily-backup namespace: velero + annotations: + velero.io/csi-volumesnapshot-class_driver.longhorn.io: "longhorn-backup-vsc" spec: - schedule: "0 1 * * *" # Daily at 1 AM + schedule: "41 22 * * *" # Daily at 1 AM template: # includedNamespaces: # - immich @@ -19,6 +21,8 @@ kind: Schedule metadata: name: weekly-backup namespace: velero + annotations: + velero.io/csi-volumesnapshot-class_driver.longhorn.io: "longhorn-backup-vsc" spec: schedule: "0 0 * * 0" # Weekly on Sunday at midnight template: