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.

Revisions

  1. jurno revised this gist Nov 17, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backup-terraform-cloud.sh
    Original file line number Diff line number Diff line change
    @@ -37,5 +37,5 @@ for ID in $WORKSPACE_ID; do
    -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
    curl -L -H "Authorization: Bearer $TOKEN" "$OBJECT_ID" -o $BACKUP_DIR/$ID.tfstate
    done
  2. jurno revised this gist May 22, 2022. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions backup-terraform-cloud.sh
    Original file line number Diff line number Diff line change
    @@ -11,10 +11,10 @@

    if [ -z $1 ]
    then
    echo "[ERROR] Musis zadat nazov TFC organization ako parameter."
    echo " Napriklad: ./backup-terraform-cloud.sh azure-sandbox-pd"
    echo ""
    exit 1
    echo "[ERROR] Musis zadat nazov TFC organization ako parameter."
    echo " Napriklad: ./backup-terraform-cloud.sh azure-sandbox-pd"
    echo ""
    exit 1
    fi

    TOKEN=$(cat ~/.terraform.d/credentials.tfrc.json | jq -r '.[]."app.terraform.io".token')
  3. jurno revised this gist May 22, 2022. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions backup-terraform-cloud.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,14 @@
    #!/bin/bash

    # Odzalohuje vsetky statefiles z organization v Terraform Cloud. Skript predpoklada,
    # ze pouzivatel je prihlaseny do Terraform Cloud cez `terraform login` a takto ziskany
    # token ma ulozeny defaultnym sposobom.
    # https://www.terraform.io/cli/commands/login#credentials-storage

    # Pouzitie:
    # chmod +x ./backup-terraform-cloud.sh
    # ./backup-terraform-cloud.sh <ORGANIZATION>

    if [ -z $1 ]
    then
    echo "[ERROR] Musis zadat nazov TFC organization ako parameter."
  4. jurno revised this gist May 22, 2022. 1 changed file with 10 additions and 2 deletions.
    12 changes: 10 additions & 2 deletions backup-terraform-cloud.sh
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,19 @@
    #!/bin/bash

    if [ -z $1 ]
    then
    echo "[ERROR] Musis zadat nazov TFC organization ako parameter."
    echo " Napriklad: ./backup-terraform-cloud.sh azure-sandbox-pd"
    echo ""
    exit 1
    fi

    TOKEN=$(cat ~/.terraform.d/credentials.tfrc.json | jq -r '.[]."app.terraform.io".token')
    ORG_NAME=azure-sandbox-pd
    ORG_NAME=$1
    YEAR=$(date +"%Y")
    MONTH=$(date +"%m")
    DIR=$(date +"%y%m%d-%H%M")
    BACKUP_DIR=$YEAR/$MONTH/$DIR
    BACKUP_DIR=$YEAR/$MONTH/$DIR/$ORG_NAME

    mkdir -p $BACKUP_DIR

  5. jurno revised this gist Apr 3, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backup-terraform-cloud.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash

    TOKEN=
    TOKEN=$(cat ~/.terraform.d/credentials.tfrc.json | jq -r '.[]."app.terraform.io".token')
    ORG_NAME=azure-sandbox-pd
    YEAR=$(date +"%Y")
    MONTH=$(date +"%m")
  6. jurno revised this gist Mar 19, 2022. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion backup-terraform-cloud.sh
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,10 @@

    TOKEN=
    ORG_NAME=azure-sandbox-pd
    BACKUP_DIR=$(date +"%y%m%d-%H%M")
    YEAR=$(date +"%Y")
    MONTH=$(date +"%m")
    DIR=$(date +"%y%m%d-%H%M")
    BACKUP_DIR=$YEAR/$MONTH/$DIR

    mkdir -p $BACKUP_DIR

  7. jurno created this gist Mar 18, 2022.
    21 changes: 21 additions & 0 deletions backup-terraform-cloud.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    #!/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