-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add support for restoring AlloyDB clusters via backup restore and PITR #8575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a705911
145121a
01bdf49
1698b7b
a0a2a68
cd32e8d
e886c86
50611a2
22dcd5c
6910f0f
ce93ae5
9c2350c
95b98e6
8c6cbba
ef517d6
f37f890
8c47937
5fa7584
43565fd
a06326f
3c49109
a0a9c6e
ed9730b
81dd01f
b6c5c8f
bf6af4b
f6866e2
b05962b
aa2192c
4262154
8f109a4
e967f0f
0344f20
944b4bb
f9a4379
22d6b07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| resource "google_alloydb_cluster" "<%= ctx[:primary_resource_id] %>" { | ||
| cluster_id = "<%= ctx[:vars]['alloydb_cluster_name'] %>" | ||
| location = "us-central1" | ||
| network = data.google_compute_network.default.id | ||
|
|
||
| initial_user { | ||
| password = "<%= ctx[:vars]['alloydb_cluster_name'] %>" | ||
| } | ||
| } | ||
|
|
||
| resource "google_alloydb_instance" "<%= ctx[:primary_resource_id] %>" { | ||
| cluster = google_alloydb_cluster.<%= ctx[:primary_resource_id] %>.name | ||
| instance_id = "<%= ctx[:vars]['alloydb_instance_name'] %>" | ||
| instance_type = "PRIMARY" | ||
|
|
||
| machine_config { | ||
| cpu_count = 2 | ||
| } | ||
|
|
||
| depends_on = [google_service_networking_connection.vpc_connection] | ||
| } | ||
|
|
||
| resource "google_alloydb_backup" "<%= ctx[:primary_resource_id] %>" { | ||
| backup_id = "<%= ctx[:vars]['alloydb_backup_id'] %>" | ||
| location = "us-central1" | ||
| cluster_name = google_alloydb_cluster.<%= ctx[:primary_resource_id] %>.name | ||
|
|
||
| depends_on = [google_alloydb_instance.<%= ctx[:primary_resource_id] %>] | ||
| } | ||
|
|
||
| resource "google_alloydb_cluster" "restored_from_backup" { | ||
| cluster_id = "<%= ctx[:vars]['alloydb_backup_restored_cluster_name'] %>" | ||
| location = "us-central1" | ||
| network = data.google_compute_network.default.id | ||
| restore_backup_source { | ||
| backup_name = google_alloydb_backup.<%= ctx[:primary_resource_id] %>.name | ||
| } | ||
| } | ||
|
|
||
| resource "google_alloydb_cluster" "restored_via_pitr" { | ||
| cluster_id = "<%= ctx[:vars]['alloydb_pitr_restored_cluster_name'] %>" | ||
| location = "us-central1" | ||
| network = data.google_compute_network.default.id | ||
|
|
||
| restore_continuous_backup_source { | ||
| cluster = google_alloydb_cluster.<%= ctx[:primary_resource_id] %>.name | ||
| point_in_time = "2023-08-03T19:19:00.094Z" | ||
| } | ||
| } | ||
|
|
||
| data "google_project" "project" {} | ||
|
|
||
| data "google_compute_network" "default" { | ||
| name = "<%= ctx[:vars]['network_name'] %>" | ||
| } | ||
|
|
||
| resource "google_compute_global_address" "private_ip_alloc" { | ||
| name = "<%= ctx[:vars]['alloydb_cluster_name'] %>" | ||
| address_type = "INTERNAL" | ||
| purpose = "VPC_PEERING" | ||
| prefix_length = 16 | ||
| network = data.google_compute_network.default.id | ||
| } | ||
|
|
||
| resource "google_service_networking_connection" "vpc_connection" { | ||
| network = data.google_compute_network.default.id | ||
| service = "servicenetworking.googleapis.com" | ||
| reserved_peering_ranges = [google_compute_global_address.private_ip_alloc.name] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Read the restore variables from obj and remove them, since they do not map to anything in the cluster | ||
|
GoogleMarcfont marked this conversation as resolved.
|
||
| var backupSource interface{} | ||
| var continuousBackupSource interface{} | ||
| if val, ok := obj["restoreBackupSource"]; ok { | ||
| backupSource = val | ||
| delete(obj, "restoreBackupSource") | ||
| } | ||
| if val, ok := obj["restoreContinuousBackupSource"]; ok { | ||
| continuousBackupSource = val | ||
| delete(obj, "restoreContinuousBackupSource") | ||
| } | ||
|
|
||
| restoreClusterRequestBody := make(map[string]interface{}) | ||
| if backupSource != nil { | ||
| // If restoring from a backup, set the backupSource | ||
| restoreClusterRequestBody["backup_source"] = backupSource | ||
| } else if continuousBackupSource != nil { | ||
| // Otherwise if restoring via PITR, set the continuousBackupSource | ||
| restoreClusterRequestBody["continuous_backup_source"] = continuousBackupSource | ||
| } | ||
|
|
||
| if backupSource != nil || continuousBackupSource != nil { | ||
| // Use restore API if this is a restore instead of a create cluster call | ||
| url = strings.Replace(url, "clusters?clusterId", "clusters:restore?clusterId", 1) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like we call the restore endpoint for creation when one of the backup fields is set and users can also specify cluster settings via restore. Will cluster setting override backup ones if there's conflict?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apologies, I'm not sure what you mean by "users can also specify cluster settings via restore". The restore API has two main fields that need to be set:
See proto definition: http://google3/google/cloud/alloydb/v1main/service.proto;l=1492;rcl=553404819 The cluster can contain all the usual fields (encryption, automated backup policy, continuous backup config, etc.). This has no overlap with the source field, which just describes the backup or source cluster/time to restore from. Let me know if that answers your question.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I was not clear earlier. By "users can also specify cluster settings via restore", I mean if users specify any fields in the |
||
|
|
||
| // Copy obj which contains the cluster into a cluster map | ||
| cluster := make(map[string]interface{}) | ||
| for k,v := range obj { | ||
| cluster[k] = v | ||
| } | ||
| restoreClusterRequestBody["cluster"] = cluster | ||
| obj = restoreClusterRequestBody | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.