Skip to content

Instantly share code, notes, and snippets.

@manuthu
Created March 17, 2021 19:32
Show Gist options
  • Save manuthu/9d87bd352626b16d4e539cdb5f853b22 to your computer and use it in GitHub Desktop.
Save manuthu/9d87bd352626b16d4e539cdb5f853b22 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
# Database clone script.
# ENV
# dev, staging, demo, prod
ENV=dev
CLUSTER=dms-dev-cluster-1
BACKUP_NAME=$CLUSTER-before-migration-$RANDOM
# SQL Instance
SOURCE_SQL_INSTANCE=dmsv2-dev-database
TARGET_SQL_INSTANCE=dmsv2-dev-database-$RANDOM
# If resize is not required, then set the TARGET_SQL_MEM_GB to 0
TARGET_SQL_MEM_GB=8
# This must be an even number
TARGET_SQL_CPU=4
# Enable HA. A value of 0 means that HA will not be enabled.
SQL_ENABLE_HA=1
# Increase the MAX_CONNECTIONS. A value of 0 means that no changes will be effected
MAX_CONNECTIONS=300
function log() {
echo ""
echo ""
echo -e "\e[34m["$(date "+%Y-%m-%d %H:%M:%S")"] INFO " $1
echo -e "\e[0m"
echo ""
}
function log2() {
echo -e "\e[34m["$(date "+%Y-%m-%d %H:%M:%S")"] INFO \e[34m" $1
echo -e "\e[0m"
}
function use_prod_context() {
kubectl config use-context gke_twigadms_us-central1-a_dms-production-cluster-1
}
function use_dev_context() {
kubectl config use-context gke_twigadms_us-central1-a_dms-dev-cluster-1
}
function use_staging_context() {
kubectl config use-context gke_twigadms_us-central1-a_dms-staging-cluster-1
}
function use_demo_context() {
kubectl config use-context gke_twigait_europe-west1-b_dms-quality-cluster-1
}
function switch_env() {
if [ "$ENV" = "dev" ]; then
log "Using the $ENV env"
use_dev_context
elif [ "$ENV" = "staging"]; then
log "Using the $ENV env"
use_staging_context
elif [ "$ENV" = "demo"]; then
log "Using the $ENV env"
use_demo_context
elif [ "$ENV" = "prod"]; then
log "Using the $ENV env"
use_prod_context
else
log "Specify an ENV to use. Either dev, staging, demo or prod"
exit
fi
}
function switch_project() {
log "Switching GCP project to $1"
gcloud config set project $1
}
function switch_to_twigait() {
switch_project twigait
}
function switch_to_dms() {
switch_project twigadms
}
function create_backup() {
backup=$1
log "Create a backup of the cluster => $backup"
velero backup create $backup
}
function clone_database() {
source_db=$1
target_db=$2
log "Clone Database $source_db to $target_db"
log "Available cloud sql instances"
gcloud sql instances list
log "Creating a new cloud instance from $target_db"
gcloud sql instances clone $source_db $target_db
log "Available cloud sql instances"
gcloud sql instances list
}
function enable_query_insights() {
target_db=$1
log "Enabling Query insights to the database $target_db"
gcloud sql instances patch $target_db --insights-config-query-insights-enabled --insights-config-record-application-tags --insights-config-record-client-address
}
function resize_database() {
if [[ $TARGET_SQL_MEM_GB -eq 0 ]]; then
log "Done with the db clone and no resize done"
return 0
fi
target_db=$1
target_mem_GB=$2
target_cpu=$3
log "Resize the $target_db to $target_mem_GB"
gcloud sql instances patch $target_db --memory=$target_mem_GB --cpu=$target_cpu
}
function enable_ha() {
if [[ $SQL_ENABLE_HA -eq 0 ]]; then
log "HA not enabled for instance"
return 0
fi
target_db=$1
log "Enabling HA to the instance $target_db. This operation takes a few minutes. Grab a cup of coffee."
gcloud sql instances patch $target_db --availability-type REGIONAL
}
function set_max_connections() {
# log "TODO: Refer to this - https://cloud.google.com/sql/docs/postgres/quotas"
# log "https://cloud.google.com/sql/docs/postgres/flags"
if [[ $MAX_CONNECTIONS -eq 0 ]]; then
log "Not MAX_CONNECTIONS configured."
return 0
fi
target_db=$1
connections=$2
log "Requires a restart. Confirm before proceeding."
gcloud sql instances patch $target_db --database-flags max_connections=$connections
}
switch_env
create_backup $BACKUP_NAME
clone_database $SOURCE_SQL_INSTANCE $TARGET_SQL_INSTANCE
enable_query_insights $TARGET_SQL_INSTANCE
resize_database $TARGET_SQL_INSTANCE $TARGET_SQL_MEM_GB $TARGET_SQL_CPU
enable_ha $TARGET_SQL_INSTANCE
set_max_connections $TARGET_SQL_INSTANCE $MAX_CONNECTIONS
log "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment