diff --git a/velero/restore.sh b/velero/restore.sh index 678de6f..2b059b2 100755 --- a/velero/restore.sh +++ b/velero/restore.sh @@ -76,12 +76,115 @@ for pod_name in $pod_names; do fi done -echo "Please restore the pvs and pvcs. After that write done: " -while :; do - read INPUT - if [[ $INPUT == "done" ]]; then - break - fi -done +function yes_or_no { + while true; do + read -p "$* [y/n]: " yn + case $yn in + [Yy]*) return 0 ;; + [Nn]*) return 1 ;; + esac + done +} + +if yes_or_no "Should the script restore all backups automatically?"; then + command_exists "curl" + command_exists "jq" + + while :; do + echo -n "longhorn username: " + read USERNAME + echo -n "longhorn password: " + read -s PASSWORD + echo "" + # check if credentials work + API_STATUS_CODE=$(curl -o /dev/null -su "$USERNAME:$PASSWORD" -w "%{http_code}" -X "GET" "https://longhorn.services.yolokube.de/v1") + if [[ $API_STATUS_CODE == "200" ]]; then + break + else + echo "login credentials seem to be wrong, got error $API_STATUS_CODE" + fi + done + + # get Backup Target + BACKUPTARGET=$(curl -su "$USERNAME:$PASSWORD" https://longhorn.services.yolokube.de/v1/backuptargets | jq -r ".data[0].backupTargetURL") + while IFS= read -r b; do + LASTBACKUP=$(echo $b | jq -r ".lastBackupName") + VOLUMEID=$(echo $b | jq -r ".id") + API_STATUS_CODE=$(curl -o /dev/null -su "$USERNAME:$PASSWORD" -w "%{http_code}" -X "POST" "https://longhorn.services.yolokube.de/v1/volumes" \ + -H 'Content-Type: application/json' \ + -d $"{ + \"name\": \"${VOLUMEID}\", + \"numberOfReplicas\": 3, + \"accessMode\": \"rwo\", + \"encrypted\": false, + \"restoreVolumeRecurringJob\": \"ignored\", + \"nodeSelector\": [], + \"diskSelector\": [], + \"fromBackup\": \"${BACKUPTARGET}?backup=${LASTBACKUP}&volume=${VOLUMEID}\", + \"staleReplicaTimeout\": 20 + }") + if [[ $API_STATUS_CODE != "200" ]]; then + echo "could not restore $VOLUMEID" + fi + done <<< "$(curl -su "$USERNAME:$PASSWORD" https://longhorn.services.yolokube.de/v1/backupvolumes | jq -c '.data[]')" + + wait_for_volume () { + counter=0 + while [ $counter -lt 50 ]; do + # get volume info + VOLUME=$(curl -su "$USERNAME:$PASSWORD" https://longhorn.services.yolokube.de/v1/volumes/$1) + # parse json + READY=$(echo $VOLUME | jq -r ".ready") + STATE=$(echo $VOLUME | jq -r ".state") + # if the volume is ready return + if [ $READY == "true" ] && [ $STATE == "detached" ]; then + return + fi + let counter++ + sleep 5 + done + echo "timeout while waiting for volume $1 to become ready" + } + # wait a while for longhorn to create the volumes from backup + echo "waiting for backups to be restored" + sleep 10 + + # go through all volumes + while IFS= read -r b; do + VOLUMEID=$(echo $b | jq -r ".id") + VOLUMENAME=$(echo $b | jq -r ".name") + NAMESPACE=$(echo $b | jq -r ".kubernetesStatus.namespace") + PVCNAME=$(echo $b | jq -r ".kubernetesStatus.pvcName") + echo "wait for $VOLUMEID to bevome ready" + wait_for_volume $VOLUMEID + echo "volume is ready... create PVs and PVCs" + API_STATUS_CODE=$(curl -o /dev/null -su "$USERNAME:$PASSWORD" -w "%{http_code}" -X "POST" "https://longhorn.services.yolokube.de/v1/volumes/$VOLUMEID?action=pvCreate" \ + -H 'Content-Type: application/json' \ + -d $"{ + \"pvName\":\"${VOLUMENAME}\", + \"fsType\":\"ext4\" + }") + if [[ $API_STATUS_CODE != "200" ]]; then + echo "could not create PV for $VOLUMEID" + fi + API_STATUS_CODE=$(curl -o /dev/null -su "$USERNAME:$PASSWORD" -w "%{http_code}" -X "POST" "https://longhorn.services.yolokube.de/v1/volumes/$VOLUMEID?action=pvcCreate" \ + -H 'Content-Type: application/json' \ + -d $"{ + \"pvcName\":\"${PVCNAME}\", + \"namespace\":\"${NAMESPACE}\" + }") + if [[ $API_STATUS_CODE != "200" ]]; then + echo "could not create PVC for $VOLUMEID" + fi + done <<< "$(curl -su "$USERNAME:$PASSWORD" https://longhorn.services.yolokube.de/v1/volumes | jq -c '.data[]')" +else + echo "Please restore the pvs and pvcs manually. After that write done: " + while :; do + read INPUT + if [[ $INPUT == "done" ]]; then + break + fi + done +fi echo "Restore everything else" velero restore create --from-backup "$BACKUPNAME" --exclude-resources persistentvolumes,persistentvolumeclaims restore-part-3 || exit 1