diff --git a/velero/restore.sh b/velero/restore.sh new file mode 100755 index 0000000..af21f50 --- /dev/null +++ b/velero/restore.sh @@ -0,0 +1,86 @@ +#!/usr/bin/env bash +# written by Aaron 2023-08-27 + +command_exists() { + command -v "$1" >/dev/null 2>&1 + if [[ $? -ne 0 ]]; then + echo "Software dependency not met: $1" + exit 1 + fi +} +command_exists "velero" +command_exists "kubectl" + +velero backup get || exit 1 +echo -n "Please specify backup to be restored: " +read BACKUPNAME +echo "restore CRDs and services (services are just restored to create the namespaces)" +velero restore create --from-backup $BACKUPNAME --include-resources customresourcedefinitions,services --include-cluster-resources=true restore-part-1 || exit 1 +echo "restore Longhorn and ingress controller" +velero restore create --from-backup $BACKUPNAME --include-namespaces nginx-ingress,longhorn-system --include-cluster-resources=true restore-part-2 || exit 1 +echo "" + +###### +# Wait for Longhorn deployment +###### + +namespace="longhorn-system" +pod_name_pattern="instance-manager" +wait_timeout=300 # Maximum time to wait for pod creation in seconds +wait_interval=5 # Interval for checking pod creation in seconds + +echo "Waiting for $pod_name_pattern pods to be created in namespace $namespace..." + +# Wait loop to check pod creation +elapsed_time=0 +while [ $elapsed_time -lt $wait_timeout ]; do + pod_names=$(kubectl get pods -n "$namespace" --output=jsonpath='{.items[*].metadata.name}' | grep -E "^$pod_name_pattern") + + if [ -n "$pod_names" ]; then + break + fi + + sleep $wait_interval + elapsed_time=$((elapsed_time + wait_interval)) +done + +if [ -z "$pod_names" ]; then + echo "No pods matching the pattern were created within the specified timeout." + exit 1 +fi + +echo "$pod_name_pattern pods detected. Proceeding with deletion..." + + +###### +# Delete Longhorn Instance Pods +###### + +namespace="longhorn-system" +pod_name_pattern="instance-manager" + +# Get the list of pod names in the specified namespace +pod_names=$(kubectl get pods -n "$namespace" --output=jsonpath='{.items[*].metadata.name}') + +if [ -z "$pod_names" ]; then + echo "No pods found in namespace $namespace." + exit 0 +fi + +# Delete the matching pods one by one +for pod_name in $pod_names; do + if echo "$pod_name" | grep -q "^$pod_name_pattern"; then + kubectl delete pod "$pod_name" -n "$namespace" + echo "Deleted pod: $pod_name" + fi +done + +echo "Please restore the pvs and pvcs. After that write done: " +while :; do + read INPUT + if [[ $INPUT == "done" ]]; then + break + fi +done +echo "Restore everything else" +velero restore create --from-backup $BACKUPNAME --exclude-resources persistentvolumes,persistentvolumeclaims restore-part-3 || exit 1