0
0
GCPcloud~5 mins

Disaster recovery strategies in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Disaster recovery strategies help you prepare for unexpected problems like data loss or system failures. They make sure your cloud services can quickly recover and keep running with minimal downtime.
When you want to protect your important data from accidental deletion or corruption.
When you need to keep your website or app running even if one cloud region goes down.
When you want to quickly restore your services after a natural disaster or cyber attack.
When you want to test your backup and recovery plans regularly to avoid surprises.
When you want to meet compliance rules that require data protection and recovery plans.
Config File - main.tf
main.tf
provider "google" {
  project = "example-project"
  region  = "us-central1"
}

resource "google_compute_disk" "primary_disk" {
  name  = "primary-disk"
  type  = "pd-standard"
  zone  = "us-central1-a"
  size  = 50
}

resource "google_compute_disk" "backup_disk" {
  name  = "backup-disk"
  type  = "pd-standard"
  zone  = "us-east1-b"
  size  = 50
}

resource "google_compute_snapshot" "disk_snapshot" {
  name       = "primary-disk-snapshot"
  source_disk = google_compute_disk.primary_disk.id
}

resource "google_storage_bucket" "backup_bucket" {
  name     = "example-backup-bucket"
  location = "US"
  versioning {
    enabled = true
  }
}

This Terraform file sets up a primary disk in one zone and a backup disk in another zone for disaster recovery. It creates a snapshot of the primary disk to capture its state. It also creates a storage bucket with versioning enabled to store backups safely.

This setup helps recover data if the primary disk or zone fails.

Commands
This command initializes Terraform in the current directory. It downloads the Google provider plugin and prepares Terraform to manage resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/google... - Installing hashicorp/google v4.0.0... - Installed hashicorp/google v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized!
This command applies the Terraform configuration to create the resources defined in the file. The -auto-approve flag skips the confirmation prompt.
Terminal
terraform apply -auto-approve
Expected OutputExpected
google_compute_disk.primary_disk: Creating... google_compute_disk.backup_disk: Creating... google_compute_disk.primary_disk: Creation complete after 10s [id=projects/example-project/zones/us-central1-a/disks/primary-disk] google_compute_disk.backup_disk: Creation complete after 10s [id=projects/example-project/zones/us-east1-b/disks/backup-disk] google_compute_snapshot.disk_snapshot: Creating... google_compute_snapshot.disk_snapshot: Creation complete after 5s [id=projects/example-project/global/snapshots/primary-disk-snapshot] google_storage_bucket.backup_bucket: Creating... google_storage_bucket.backup_bucket: Creation complete after 3s [id=example-backup-bucket] Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply without asking for confirmation
This command lists the snapshot created for the primary disk to verify it exists and is ready for recovery.
Terminal
gcloud compute snapshots list --filter="name=primary-disk-snapshot"
Expected OutputExpected
NAME DISK_NAME STATUS CREATION_TIMESTAMP primary-disk-snapshot primary-disk READY 2024-06-01T12:00:00.000-07:00
--filter - Filter the list to show only snapshots with the specified name
This command lists the contents and details of the backup storage bucket to confirm backups are stored with versioning.
Terminal
gsutil ls -L gs://example-backup-bucket
Expected OutputExpected
gs://example-backup-bucket/: Storage class: STANDARD Location constraint: US Versioning enabled: True Time created: 2024-06-01T11:50:00.000Z Labels: None Lifecycle: None Cors: None Logging: None Website: None Encryption: None Retention policy: None Default event-based hold: False Bucket Policy Only: Disabled Uniform bucket-level access: Disabled Public access prevention: Unspecified Default KMS key: None Requester Pays: Disabled Owner: user-example@example.com Project number: 123456789012
Key Concept

If you remember nothing else from this pattern, remember: regularly create backups and store them in a separate location to recover quickly from disasters.

Common Mistakes
Not enabling versioning on the backup storage bucket
Without versioning, you cannot recover previous versions of files if they are overwritten or deleted.
Always enable versioning on backup buckets to keep multiple versions of your data.
Creating snapshots only in the same zone as the primary disk
If the entire zone fails, snapshots in the same zone may be lost or inaccessible.
Store snapshots or backups in a different zone or region to ensure availability during zone failures.
Not verifying backups after creation
Backups might fail silently or be incomplete, leading to failed recovery when needed.
Regularly check and test your backups and snapshots to confirm they are complete and usable.
Summary
Initialize Terraform to prepare for resource management with 'terraform init'.
Apply the configuration to create disks, snapshots, and backup storage with 'terraform apply'.
Verify snapshots exist using 'gcloud compute snapshots list' to ensure recovery points.
Check backup storage bucket details with 'gsutil ls -L' to confirm versioning and data safety.