liveBook · Manning

Backup and restore

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

A state backup is a preserved copy of Terraform state that can be restored as a prior state snapshot.

Terraform state records what Terraform manages. Before changing state directly, preserve a copy: a mistaken edit can be destructive, and even a reliable backend cannot prevent every outage or accidental deletion. If state becomes corrupt, Terraform may be unable to read it at all; restoring a backup may be the only option. section 6.5.1 section 6.2.1 section 6.6.4

Save the current state

Run `terraform state pull > my_backup.tfstate` before manipulating state. `state pull` reads the latest state from the configured backend and writes it to standard output; the redirect saves that snapshot as a local file. section 6.5.1

Restore the snapshot

Run `terraform state push my_backup.tfstate` to send the saved file to the backend. You can also pipe it through standard input with `cat my_backup.tfstate | terraform state push -`. Terraform rejects a push when the snapshot has a different lineage or a lower serial than the backend state. If deliberately reverting to an older backup, `terraform state push -force my_backup.tfstate` bypasses those protections; confirm that this is the snapshot you intend to restore. section 6.5.1 section 6.3

Make recovery a backend practice, not just an emergency command. Keep reliable backups, restrict and log state access, and test backups regularly so the team knows they work. Backend versioning can help with corruption, but external backups still matter if the backend itself becomes unavailable. The exact setup depends on the backend. section 6.4.2 section 6.6.4

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Walks through state pull and push, including file and stdin input plus lineage and serial checks; defers backend-specific configuration.
Examples
Uses a saved `my_backup.tfstate` file to demonstrate state pull, state push, stdin restoration, and forced restoration.
What it assumes
Assumes a configured Terraform backend and teaches the pull and push commands without requiring manual state editing.
What you can do after
Practice/perform it: save and restore Terraform state snapshots, and test backend backups before an emergency.
How it connects
Connects backups to Terraform state, remote backends, state lineage, backend versioning, access controls, and state corruption.

Also explained in

Compare how 10 books explain backup and restore →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.