2022-10-19 07:10:15 -04:00
|
|
|
#!/usr/bin/env bash
|
2022-11-10 04:28:35 -05:00
|
|
|
|
2022-10-19 07:10:15 -04:00
|
|
|
set -euo pipefail
|
2022-11-10 04:28:35 -05:00
|
|
|
shopt -s inherit_errexit
|
2022-10-19 07:10:15 -04:00
|
|
|
|
2022-11-10 08:17:04 -05:00
|
|
|
if [[ -z ${CONFIG_FILE-} ]] && [[ -f ${CONFIG_FILE-} ]]; then
|
|
|
|
# shellcheck source=/dev/null
|
|
|
|
. "${CONFIG_FILE}"
|
2022-10-19 07:10:15 -04:00
|
|
|
fi
|
|
|
|
AZURE_SUBSCRIPTION=$(az account show --query id -o tsv)
|
|
|
|
POSITIONAL_ARGS=()
|
|
|
|
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case $1 in
|
2022-11-10 08:17:04 -05:00
|
|
|
-n | --name)
|
|
|
|
AZURE_VM_NAME="$2"
|
|
|
|
shift # past argument
|
|
|
|
shift # past value
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
echo "Unknown option $1"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
POSITIONAL_ARGS+=("$1") # save positional arg
|
|
|
|
shift # past argument
|
|
|
|
;;
|
2022-10-19 07:10:15 -04:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
|
|
|
|
|
|
|
|
VM_DISK=$(az vm show -g "${AZURE_RESOURCE_GROUP_NAME}" --name "${AZURE_VM_NAME}" --query "storageProfile.osDisk.managedDisk.id" -o tsv)
|
|
|
|
LOCATION=$(az disk show --ids "${VM_DISK}" --query "location" -o tsv)
|
|
|
|
|
|
|
|
az snapshot create \
|
2022-11-10 08:17:04 -05:00
|
|
|
-g "${AZURE_RESOURCE_GROUP_NAME}" \
|
|
|
|
--source "${VM_DISK}" \
|
|
|
|
--name "${AZURE_SNAPSHOT_NAME}" \
|
|
|
|
-l "${LOCATION}"
|
2022-10-19 07:10:15 -04:00
|
|
|
|
|
|
|
# Azure CLI does not implement getSecureVMGuestStateSAS for snapshots yet
|
|
|
|
# az snapshot grant-access \
|
|
|
|
# --duration-in-seconds 3600 \
|
|
|
|
# --access-level Read \
|
|
|
|
# --name "${AZURE_SNAPSHOT_NAME}" \
|
|
|
|
# -g "${AZURE_RESOURCE_GROUP_NAME}"
|
|
|
|
|
|
|
|
BEGIN=$(az rest \
|
2022-11-10 08:17:04 -05:00
|
|
|
--method post \
|
|
|
|
--url "https://management.azure.com/subscriptions/${AZURE_SUBSCRIPTION}/resourceGroups/${AZURE_RESOURCE_GROUP_NAME}/providers/Microsoft.Compute/snapshots/${AZURE_SNAPSHOT_NAME}/beginGetAccess" \
|
|
|
|
--uri-parameters api-version="2021-12-01" \
|
|
|
|
--body '{"access": "Read", "durationInSeconds": 3600, "getSecureVMGuestStateSAS": true}' \
|
|
|
|
--verbose 2>&1)
|
2022-10-19 07:10:15 -04:00
|
|
|
ASYNC_OPERATION_URI=$(echo "${BEGIN}" | grep Azure-AsyncOperation | cut -d ' ' -f 7 | tr -d "'")
|
|
|
|
sleep 10
|
|
|
|
ACCESS=$(az rest --method get --url "${ASYNC_OPERATION_URI}")
|
|
|
|
VMGS_URL=$(echo "${ACCESS}" | jq -r '.properties.output.securityDataAccessSAS')
|
|
|
|
|
2023-01-19 05:22:31 -05:00
|
|
|
curl -fsSL -o "${AZURE_VMGS_FILENAME}" "${VMGS_URL}"
|
2022-10-19 07:10:15 -04:00
|
|
|
|
|
|
|
az snapshot revoke-access \
|
2022-11-10 08:17:04 -05:00
|
|
|
--name "${AZURE_SNAPSHOT_NAME}" \
|
|
|
|
-g "${AZURE_RESOURCE_GROUP_NAME}"
|
2022-10-19 07:10:15 -04:00
|
|
|
az snapshot delete \
|
2022-11-10 08:17:04 -05:00
|
|
|
--name "${AZURE_SNAPSHOT_NAME}" \
|
|
|
|
-g "${AZURE_RESOURCE_GROUP_NAME}"
|
2022-10-19 07:10:15 -04:00
|
|
|
echo "VMGS saved to ${AZURE_VMGS_FILENAME}"
|