Skip to content

Instantly share code, notes, and snippets.

@aamir814
Created February 9, 2024 18:27
Show Gist options
  • Select an option

  • Save aamir814/5594f03c4521621ac56ab95931dee2a8 to your computer and use it in GitHub Desktop.

Select an option

Save aamir814/5594f03c4521621ac56ab95931dee2a8 to your computer and use it in GitHub Desktop.
AlloyDB corss-project backup/restore
#!/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
@aamir814
Copy link
Author

aamir814 commented Feb 9, 2024

AlloyDB Backup and Restore cross-projects

A simple script that shows how to restore a backup of AlloyDB cluster to a different project.


Result

export SRC_ALYDB_BAK_NAME=projects/aamir-playground/locations/us-central1/backups/automated-bkp-20240203-23-1df55c95-0c19-4d9b-a938-f4ee19470a31

gcloud alloydb clusters restore $TRG_CLUSTER_NAME --region=$GCP_REGION --backup=$SRC_ALYDB_BAK_NAME
Operation ID: operation-1707491884173-610f46f368a7b-ada1c914-31e7154d
Restoring cluster...done.                                                                                                                                                                                                                                                                      

gcloud alloydb clusters list
NAME                                                                NETWORK                                       STATUS
projects/alydb-test-proj-2/locations/us-central1/clusters/alloydb2  projects/72652464227/global/networks/default  READY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment