restore script is feature complete 🐐

This commit is contained in:
Aaron Riedel 2023-08-29 01:06:11 +02:00
parent 9f2c417164
commit e7c2737414
Signed by: aaron
GPG key ID: 643004654D40D577

View file

@ -76,12 +76,115 @@ for pod_name in $pod_names; do
fi fi
done done
echo "Please restore the pvs and pvcs. After that write done: " function yes_or_no {
while :; do while true; do
read INPUT read -p "$* [y/n]: " yn
if [[ $INPUT == "done" ]]; then case $yn in
break [Yy]*) return 0 ;;
fi [Nn]*) return 1 ;;
done 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" echo "Restore everything else"
velero restore create --from-backup "$BACKUPNAME" --exclude-resources persistentvolumes,persistentvolumeclaims restore-part-3 || exit 1 velero restore create --from-backup "$BACKUPNAME" --exclude-resources persistentvolumes,persistentvolumeclaims restore-part-3 || exit 1