Last active
December 10, 2020 07:20
-
-
Save slayer/c78a357ee0cf043d18439fb38f5b59ca to your computer and use it in GitHub Desktop.
Google Cloud copy disks (images) between projects
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
#!/bin/bash | |
# Setup your projets and zones | |
SRC_PRJ="company-staging" | |
SRC_ZONE="northamerica-northeast1-b" | |
DST_PRJ="company-production" | |
DST_ZONE="us-central1-b" | |
DISKS="$@" | |
c_red=$(tput setaf 1);c_grn=$(tput setaf 2);c_rst=$(tput sgr0);c_white=$(tput setaf 7) | |
suffix=$(date +%Y%m%d-%H%M) | |
time="/bin/time -f ${c_grn}Elapsed${c_rst}:%E" | |
DISKS="$@" | |
if [ -z "${DISKS}" ]; then | |
echo "Usage $0 disk1 disk2 ..."; exit 1 | |
fi | |
for disk in ${DISKS?}; do | |
echo "Copying from ${c_grn}${SRC_PRJ?}/${SRC_ZONE?}${c_rst} to ${c_grn}${DST_PRJ?}/${DST_ZONE?}${c_rst}" | |
snap="${disk}-${suffix}" | |
# SRC Project | |
$time gcloud --project ${SRC_PRJ} compute disks snapshot ${disk} --snapshot-names=${snap} --zone ${SRC_ZONE} | |
$time gcloud --project ${SRC_PRJ} compute images create ${snap} --source-snapshot=${snap} | |
# Here we can create "disk" or "image" | |
$time gcloud --project ${DST_PRJ} compute images create ${snap} --source-image ${snap} --source-image-project ${SRC_PRJ} | |
#$time gcloud --project ${DST_PRJ} compute disks create ${disk} --image ${snap} --image-project ${SRC_PRJ} | |
# cleanup | |
$time gcloud --project ${SRC_PRJ} compute images delete --quiet ${snap} | |
$time gcloud --project ${SRC_PRJ} compute snapshots delete --quiet ${snap} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment