State backup and recovery
State recovery is not service recovery
A state snapshot records Terraform’s associations between configured addresses and infrastructure objects, plus provider-maintained information. It is not a copy of the contents of a database, virtual disk or object store. Recovering a snapshot can change what Terraform believes it manages without restoring any of those objects. Consider the following fictional timeline.
Snapshot A: lineage L-demo, serial 18, objects web-1Snapshot B: lineage L-demo, serial 19, objects web-1, db-1Observed service inventory: web-1, db-1Candidate A is older and omits db-1; do not overwrite B.If a database was created after snapshot A, restoring A could leave Terraform unaware of that database. A subsequent plan might attempt a second creation. Recover database data using independently tested database backups, and recover state using a coordinated state procedure. Neither proves the application is working.
Compare lineage, serial and resource mappings
Lineage identifies a state history. Serial tracks revisions within a history, rather than a global clock across all states. A different lineage or an older candidate than the destination is a reason to stop and investigate. A higher serial is not by itself proof that the candidate describes reality. Check the backend, workspace, configuration revision, addresses, object IDs, provider associations and observed resources.
Candidate C: lineage L-other, serial 99Destination: lineage L-demo, serial 19Decision: HOLD (different history despite higher number)Terraform state push normally refuses different lineage or a destination with a higher serial. These protections are not complete recovery validation. Never disable them to make an uncertain restore pass. See https://developer.hashicorp.com/terraform/language/state/backends.
Build a recoverable backup process
Keep backend version history where supported, encrypted backup copies with restricted access, an independently protected integrity manifest, and an inventory of states and workspaces. S3 bucket versioning and GCS object versioning are examples of backend-specific recovery mechanisms. Versioning alone does not establish that a backup is complete, readable or protected from all administrative deletion. State may contain secrets even when outputs are marked sensitive. Restrict backup and log access, retain only approved copies, and test restoration in isolation. Record the engine version because reading state using a newer CLI can transform the emitted format.
Backup receipt (fictional)Workspace: dev / catalogSnapshot serial: 19Checksum: verified against protected manifestRestore rehearsal: passed in isolated destinationApplication backup: separate evidence requiredA checksum shows equality to the expected bytes; it does not prove that the expected snapshot is correct or authentic if the manifest itself is untrusted.
Respond to a failed state write
A provider operation can succeed while the backend rejects the state update. Terraform may preserve an updated snapshot locally to avoid losing the mapping. Treat that file as sensitive evidence; do not run another apply to see what happens. Freeze automated and manual writers, record the error without secrets, confirm the backend identity, preserve current remote and local recovery copies, and reconcile each affected object. The state pull command retrieves the selected state to standard output; state push is an exceptional write to the configured destination.
Command reference only — not a runbookterraform state pullterraform state push <reviewed-state-file>Never print real snapshots to a shared CI log. Do not copy a recovery file to a different workspace or retry blindly. Only an authorized operator with exclusive access, verified backup provenance, matched resource mappings and a reviewed recovery decision should perform a write. If a normal push refuses the candidate, escalate; do not bypass the protection. See https://developer.hashicorp.com/terraform/cli/state/recover.
Locks and emergency recovery
A lock coordinates concurrent operations when the backend supports it; it does not repair corrupt state or replace access control. If a run still owns a lock, wait for or coordinate with that run. If a failed run left a lock behind, verify its owner and termination using authoritative runner/backend evidence, and obtain approval before considering force-unlock. The command accepts a lock ID and does not itself change infrastructure; removing an active lock can permit competing writers. Never infer that a lock is abandoned just from its age. A recovered state must still be compared with the real resource inventory and a fresh plan. Hold deployment if the plan unexpectedly creates, replaces or deletes anything. See https://developer.hashicorp.com/terraform/language/state/locking.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
State backup and recovery — knowledge check
Review state recovery, backend migration and disaster recovery.
A state snapshot predates creation of a database. What does restoring it guarantee?
A candidate has a higher serial but a different lineage. What is the correct next step?
A backend write fails after a resource creation. What is the safest immediate action?
What does an integrity checksum verify?
A lock is old but its owner may still be running. What should happen?