Overview and What You Will Learn
A backup is only useful if you can restore from it, so this lab treats the restore test as the main event. You will schedule Persistent Disk snapshots, restore one onto a new disk and read the data back, then enable Cloud SQL automated backups with point-in-time recovery and restore a backup into a second instance.
By the end you will be able to:
- Create a snapshot schedule with retention and attach it to a disk.
- Restore a disk from a snapshot and verify the file contents.
- Enable Cloud SQL backups and point-in-time recovery for PostgreSQL.
- Restore a Cloud SQL backup into another instance and check the data.
- Explain recovery point and recovery time objectives for each mechanism.
Why This Matters in Production
A completed backup job proves that data was written somewhere, and nothing more. Consider a team whose nightly database backup reports success for a year, and who learns during a real incident that one critical table was never captured. The only defense is a restore test that checks real data, run on a schedule, because that is the one step that catches silent failures, missing tables, and expired retention before they cost you the data.
Core Principles
Each GCP resource has its own backup mechanism, and each needs its own tested restore. Two numbers describe the outcome: the recovery point objective (RPO), how much data you can afford to lose, and the recovery time objective (RTO), how long you can afford to be down.
| Mechanism | Protects | Typical RPO |
|---|---|---|
| Disk snapshot schedule | Persistent Disk data | Schedule interval, such as 24 hours |
| Cloud SQL automated backup | Whole database instance | Up to 24 hours |
| Point-in-time recovery | Cloud SQL between backups | Seconds to minutes |
Snapshots are incremental, so only changed blocks are stored after the first one. Point-in-time recovery for PostgreSQL works by archiving the write-ahead log, which is why it has its own flag, separate from daily backups.
Detailed Step-by-Step Practical Lab
You need gcloud, a billing account ID, and the psql client for the Cloud SQL part. Cloud Shell has all of these.
Step 1: Create a project and enable the APIs
export BILLING_ACCOUNT=0X0X0X-0X0X0X-0X0X0X gcloud projects create gcp-backup-lab-2026 --name="Backup and DR Lab"gcloud billing projects link gcp-backup-lab-2026 --billing-account=$BILLING_ACCOUNTgcloud config set project gcp-backup-lab-2026gcloud services enable compute.googleapis.com sqladmin.googleapis.comStep 2: Create a VM and a data disk with known contents
gcloud compute disks create disk-app-data-backup-test \ --zone=asia-south1-a --size=20GB --type=pd-balanced gcloud compute instances create vm-backup-test \ --zone=asia-south1-a --machine-type=e2-small \ --image-family=debian-12 --image-project=debian-cloud gcloud compute instances attach-disk vm-backup-test \ --zone=asia-south1-a --disk=disk-app-data-backup-test --device-name=appdata gcloud compute ssh vm-backup-test --zone=asia-south1-a --quiet --command=' sudo mkfs.ext4 -F /dev/disk/by-id/google-appdata && sudo mkdir -p /mnt/appdata && sudo mount /dev/disk/by-id/google-appdata /mnt/appdata && echo "order-1001 paid 4599.00" | sudo tee /mnt/appdata/orders.txt && sudo sync'Step 3: Create a daily snapshot schedule and attach it
gcloud compute resource-policies create snapshot-schedule daily-snapshot-policy \ --region=asia-south1 \ --daily-schedule \ --start-time=03:00 \ --max-retention-days=14 \ --on-source-disk-delete=keep-auto-snapshots gcloud compute disks add-resource-policies disk-app-data-backup-test \ --zone=asia-south1-a \ --resource-policies=daily-snapshot-policyThe start time is in UTC, so 03:00 is 08:30 in Mumbai.
Step 4: Take a manual snapshot now
gcloud compute disks snapshot disk-app-data-backup-test \ --zone=asia-south1-a \ --snapshot-names=snapshot-manual-testStep 5: Restore the snapshot to a new disk and prove it
gcloud compute disks create disk-restored-from-snapshot \ --zone=asia-south1-a \ --source-snapshot=snapshot-manual-test gcloud compute instances attach-disk vm-backup-test \ --zone=asia-south1-a --disk=disk-restored-from-snapshot --device-name=restored gcloud compute ssh vm-backup-test --zone=asia-south1-a --quiet --command=' sudo mkdir -p /mnt/restored && sudo mount -o ro,noload /dev/disk/by-id/google-restored /mnt/restored && cat /mnt/restored/orders.txt'Seeing order-1001 paid 4599.00 is the proof. A snapshot command that finished without errors proves nothing by itself.
Step 6: Create a Cloud SQL instance with backups and point-in-time recovery
For PostgreSQL, point-in-time recovery uses --enable-point-in-time-recovery. The --enable-bin-log flag is for MySQL only.
gcloud sql instances create pg-backup-test \ --database-version=POSTGRES_15 \ --edition=ENTERPRISE \ --tier=db-f1-micro \ --region=asia-south1 \ --backup-start-time=03:00 \ --enable-point-in-time-recovery \ --retained-backups-count=7 \ --retained-transaction-log-days=3 gcloud sql users set-password postgres \ --instance=pg-backup-test --password='Lab-Only-Pass-2026'Step 7: Add data, then take an on-demand backup
cat > orders.sql << 'SQL_EOF'CREATE TABLE orders (id INT PRIMARY KEY, status TEXT);INSERT INTO orders VALUES (1001, 'paid'), (1002, 'shipped');SQL_EOF PGPASSWORD='Lab-Only-Pass-2026' gcloud sql connect pg-backup-test \ --user=postgres --quiet < orders.sql gcloud sql backups create --instance=pg-backup-testgcloud sql backups list --instance=pg-backup-testStep 8: Restore the backup into a second instance and check the data Copy the backup ID from the list output. Restoring overwrites the target, so use a separate instance rather than the source.
export BACKUP_ID=1760000000000 gcloud sql instances create pg-backup-test-restored \ --database-version=POSTGRES_15 --edition=ENTERPRISE \ --tier=db-f1-micro --region=asia-south1 gcloud sql backups restore $BACKUP_ID \ --restore-instance=pg-backup-test-restored \ --backup-instance=pg-backup-test --quiet gcloud sql users set-password postgres \ --instance=pg-backup-test-restored --password='Lab-Only-Pass-2026' echo "SELECT * FROM orders;" > check.sqlPGPASSWORD='Lab-Only-Pass-2026' gcloud sql connect pg-backup-test-restored \ --user=postgres --quiet < check.sqlBoth rows should come back. Note how long the restore took, because that real duration is your measured RTO.
Step 9: Clean up
gcloud compute instances delete vm-backup-test --zone=asia-south1-a --quietgcloud compute disks delete disk-app-data-backup-test disk-restored-from-snapshot \ --zone=asia-south1-a --quietgcloud compute snapshots delete snapshot-manual-test --quietgcloud sql instances delete pg-backup-test-restored --quietgcloud sql instances delete pg-backup-test --quietrm -f orders.sql check.sqlgcloud projects delete gcp-backup-lab-2026 --quietProduction Best Practices & Common Pitfalls
- Schedule real restore tests. Quarterly is a sensible start, and a test should verify actual data such as row counts or file checksums, not just that a restore command succeeded.
- Match retention to requirements. A workload with a seven-year regulatory rule needs a policy built for it, not the 14-day window in this lab.
- Store copies in another region. Snapshots accept
--storage-location, and Cloud SQL supports cross-region read replicas that can be promoted during a regional outage. - Restore a point in time with a clone. To recover from a bad
DELETE, clone the instance to a moment just before it. This works only within the transaction log retention window:
gcloud sql instances clone pg-backup-test pg-backup-test-pitr \ --point-in-time='2026-10-13T08:15:00Z'- Protect the backups themselves. Turn on
--deletion-protectionfor production instances, restrict who can delete snapshots, and consider customer-managed encryption keys. - Consider Backup and DR Service. For many workloads across projects, the managed Backup and DR Service adds central policies and immutable, access-controlled vaults.
- Alert on failures. Create a log-based alert for failed backup operations so a silent failure becomes a page.
Common MistakeConfiguring backups and never performing a test restore. The job reports success every night, and the first real restore is the first time anyone learns it was incomplete.
Common MistakeUsing the MySQL flag
--enable-bin-logon a PostgreSQL instance. Point-in-time recovery then is not configured the way you think, so confirm it in the instance settings.
SecurityThe lab password is for a disposable instance only. In production use IAM database authentication or Secret Manager, and remember a snapshot or backup contains the same sensitive data as the original.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud compute resource-policies create snapshot-schedule |
Define a snapshot schedule |
gcloud compute disks add-resource-policies |
Attach a schedule to a disk |
gcloud compute disks snapshot |
Take a manual snapshot |
gcloud compute disks create --source-snapshot= |
Restore a snapshot to a new disk |
gcloud sql backups create |
Take an on-demand backup |
gcloud sql backups restore |
Restore a backup into an instance |
gcloud sql instances clone --point-in-time= |
Recover to a specific moment |
| Symptom | Likely fix |
|---|---|
| Restored disk will not mount | Check the filesystem type and mount read-only with noload if needed |
| Restore fails with a size or version error | The target needs the same database version and at least the source's storage |
| Point-in-time clone fails | The timestamp is outside the log retention window or before PITR was enabled |
| Scheduled snapshots missing | Confirm the policy is attached and check the schedule's UTC start time |