Skip to content

Instantly share code, notes, and snippets.

@jurno
Last active November 17, 2023 16:58
Show Gist options
  • Save jurno/cfa9ab572d2e37e07c11facc7b5409d5 to your computer and use it in GitHub Desktop.
Save jurno/cfa9ab572d2e37e07c11facc7b5409d5 to your computer and use it in GitHub Desktop.
Terraform Cloud statefile backup
#!/bin/bash
TOKEN=
ORG_NAME=azure-sandbox-pd
BACKUP_DIR=$(date +"%y%m%d-%H%M")
mkdir -p $BACKUP_DIR
WORKSPACE_ID=$(curl \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/vnd.api+json" \
"https://app.terraform.io/api/v2/organizations/$ORG_NAME/workspaces" | jq -r '.data [].id')
for ID in $WORKSPACE_ID; do
OBJECT_ID=$(curl \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/vnd.api+json" \
"https://app.terraform.io/api/v2/workspaces/$ID/current-state-version" | jq -r '.data.attributes."hosted-state-download-url"')
curl -H "Authorization: Bearer $TOKEN" "$OBJECT_ID" -o $BACKUP_DIR/$ID.tfstate
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment