Docker image to export variables from Terraform Cloud and back them up to a Restic repository on Backblaze B2. The image can also initialize the Restic repository on the existing Backblaze B2 bucket.
During the review of a disaster recovery plan, we realized that we didn't have a record of the values we set for variables in Terraform Cloud workspaces. It would be difficult to recover from the accidental deletion of a Terraform Cloud workspace. A Perl script exports workspaces, variables, and variable sets to JSON files using the Terraform Cloud API. The JSON files are then backed up using Restic to a repository on a Backblaze B2 bucket.
Two files are created for each Terraform Cloud workspace:
- workspace-name-attributes.json
- workspace-name-variables.json
Two files are created for each Terraform Cloud Variable Set:
- varset-variable-set-name-attributes.json
- varset-variable-set-name-variables.json
Spaces in the variable set name are replaced with hyphens (-).
- Copy
local.env.disttolocal.env. - Set the values for the variables contained in
local.env. - Obtain a Terraform Cloud access token. Go to https://app.terraform.io/app/settings/tokens to create an API token.
- Add the access token as the value for
ATLAS_TOKENinlocal.env. - Create a Backblaze B2 bucket. Set the
File LifecycletoKeep only the last version. - Add the B2 bucket name to
RESTIC_REPOSITORYinlocal.env. - Obtain a Backblaze Application Key. Restrict its access to the B2 bucket you just created. Ensure the application key has these capabilities:
deleteFiles,listBuckets,listFiles,readBuckets,readFiles,writeBuckets,writeFiles. - Add the application key and secret to
local.envas the values ofB2_ACCOUNT_IDandB2_ACCOUNT_KEYrespectively. - Initialize the Restic repository (one time only):
docker run --env-file=local.env --env BACKUP_MODE=init silintl/tfc-backup-b2:latest - Run the Docker image:
docker run --env-file=local.env silintl/tfc-backup-b2:latest
ATLAS_TOKEN- Terraform Cloud access tokenB2_ACCOUNT_ID- Backblaze keyIDB2_ACCOUNT_KEY- Backblaze applicationKeyFSBACKUP_MODE-initinitializes the Restic repository at$RESTIC_REPOSITORY(only do this once),backupperforms a backupORGANIZATION- Name of the Terraform Cloud organization to be backed upRESTIC_BACKUP_ARGS- additional arguments to pass torestic backupcommandRESTIC_FORGET_ARGS- additional arguments to pass torestic forget --prunecommand (e.g.,--keep-daily 7 --keep-weekly 5 --keep-monthly 3 --keep-yearly 2)RESTIC_HOST- hostname to be used for the backupRESTIC_PASSWORD- password for the Restic repositoryRESTIC_REPOSITORY- Restic repository location (e.g.,b2:bucketname:restic)RESTIC_TAG- tag to apply to the backupSOURCE_PATH- Full path to the directory to be backed up
This image is built automatically on Docker Hub as silintl/tfc-backup-b2