Created
February 9, 2024 18:27
-
-
Save aamir814/5594f03c4521621ac56ab95931dee2a8 to your computer and use it in GitHub Desktop.
AlloyDB corss-project backup/restore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| ##################################################################### | |
| # Purpose: Following is an example on how to restore AlloyDB Cluster | |
| # cross project | |
| # For CLOUD SQL backup/restore cross project, please see: | |
| # https://gist.github.com/anorth848/724808332e4d3e699eb9a05411e59e5e | |
| # | |
| # Assumption: | |
| # Source and target Google Projects exist | |
| # You already have an AlloyDB backup created. | |
| # | |
| # REFERENCES | |
| # - https://cloud.google.com/alloydb/docs/backup/restore#backup | |
| ##################################################################### | |
| # CHANGEME | |
| export SRC_PROJECT_ID=alydb-test-proj-1 | |
| export SRC_CLUSTER_NAME=alloydb1 | |
| export TRG_PROJECT_ID=alydb-test-proj-2 | |
| export TRG_CLUSTER_NAME=alloydb2 | |
| export GCP_REGION="us-central1" | |
| # set google cloud project to source project | |
| gcloud config set project $SRC_PROJECT_ID | |
| # get list of backups and copy the name of backup you want | |
| gcloud alloydb backups list --region=$GCP_REGION --filter=CLUSTER_NAME:$SRC_CLUSTER_NAME --format='table(NAME,CREATE_TIME,STATUS)' | |
| # set the following variable to the backup you want from the output above | |
| export SRC_ALYDB_BAK_NAME=projects/alydb-test-proj-1/locations/us-central1/backups/automated-bkp-20240203-23-1df55c95-0c19-4d9b-a938-f4ee19470a31 | |
| # switch to target project | |
| gcloud config set project $TRG_PROJECT_ID | |
| # enable alloyDB API if not enabled already | |
| gcloud services enable alloydb.googleapis.com | |
| # run restore command | |
| gcloud alloydb clusters restore $TRG_CLUSTER_NAME --region=$GCP_REGION --backup=$SRC_ALYDB_BAK_NAME | |
| # verify target cluster status | |
| gcloud alloydb clusters list | |
| ################################################# | |
| # CLEAN UP # | |
| ################################################# | |
| # delete cluster | |
| gcloud alloydb clusters delete $TRG_CLUSTER_NAME \ | |
| --region=$GCP_REGION \ | |
| --project=$TRG_PROJECT_ID | |
| # delete project | |
| gcloud projects delete $TRG_PROJECT_ID |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AlloyDB Backup and Restore cross-projects
A simple script that shows how to restore a backup of AlloyDB cluster to a different project.
Result