Help! How do I undelete my deleted Google Cloud SQL instance?
For whatever reason, your CloudSQL instance is deleted. According to Google Cloud documentation, all data, including backups, is lost when your instance is deleted. How do I restore my instance? Where is my backup data?? What to do now???
Help! I need somebody. Help! Not just anybody. Help! You know I need someone. Help! — The Beatles
One answer to this question is to avoid this situation in the first place. There are several preventative measures to reduce the risk of accidentally deleting your CloudSQL instance. These include:
- Create Read Replicas
- Exporting data to cloud storage
- Conditional IAM
Read Replicas
One best practice to reduce the risk of accidental deletion is to create read replicas. A primary instance cannot be deleted without deleting all replicas first so replicas act as a safeguard. Replicas also copy your data for further redundancy.
Export Data to Cloud Storage
Another best practice is to export your data to Cloud Storage. This ensures data backups are stored permenantly and are available when you need them. You can export data manually. However, Google suggests using Cloud Scheduler and Cloud Functions to automatically export CloudSQL data to Cloud storage. Automating this process becomes part of an effective disaster recovery plan and Google provides a validated architecture and tutorial.
Conditional IAM
Conditional IAM enforces conditional, attribute-based access control for Google Cloud Resources including CloudSQL. With Conditional IAM, permissions are granted when specific conditions are met. An example use case is: delete test instances but not production instances. You can identify test and productions instances by using different tags and then set delete permissions only when the test instance tag is present. This prevents the accidental deletion of production instances.
The Last Resort — Undelete Cloud SQL Instance
According to the documentation:
It can take a week for the instance to be completely deleted, with the exception of read replicas, which are often deleted within a few minutes.
Because the instance may not be deleted immediately, you can contact Google Support and inquire if support can undelete your instance. There is a published grace period of four days before your Cloud SQL instance is purged so is there a chance your CloudSQL can be recovered. Provide the following information to support:
- Project Number
- Project ID
- CloudSQL Instance ID
Having said this, the guidance is not to plan or rely on this option and only use this option as a last resort. Instead, build an effective disaster recovery plan.