Skip to content

Instantly share code, notes, and snippets.

@lawrencejones
Created January 18, 2019 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lawrencejones/933170a78b1ae3d3aab6fe4d0728673e to your computer and use it in GitHub Desktop.
Save lawrencejones/933170a78b1ae3d3aab6fe4d0728673e to your computer and use it in GitHub Desktop.
Create a MySQL replica from external host in GCP
#!/usr/bin/env bash
ACCESS_TOKEN="$(gcloud auth print-access-token)"
PROJECT="gc-prd-effc"
# Parameters
REGION="europe-west1" # "europe-west4"
DATABASE_VERSION="MYSQL_5_6"
DATABASE_TIER="D2" # "db-n1-standard-2"
CERTIFICATE_FILE="rds-ca-2015-root.pem"
REPLICATION_USER="replication"
REPLICATION_PASSWORD="replica-password"
GCS_DUMP_FILE="gs://lawrjone-dropbox/test-master.sql"
EXTERNAL_NAME="lawrence-test-master"
EXTERNAL_HOST="18.194.18.111" # test-master.cif6qtu2s5lx.eu-central-1.rds.amazonaws.com
EXTERNAL_PORT="3306"
INTERNAL_NAME="lawrence-test-replica-again"
set -euo pipefail
function google-curl() {
curl --silent \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
"https://www.googleapis.com$1" \
"${@:2}"
}
# <internal-master-instance-name> <region> <external-master-database-version> <external-host:ip>
function create-external-master() {
google-curl "/sql/v1beta4/projects/${PROJECT}/instances" -X POST -d """
{
\"name\": \"$1\",
\"region\": \"$2\",
\"databaseVersion\": \"$3\",
\"onPremisesConfiguration\": {
\"hostPort\": \"$4\"
}
}
"""
}
# <username> <password> <dump-file>
function create-internal-replica() {
google-curl "/sql/v1beta4/projects/${PROJECT}/instances" -X POST -d """
{
\"replicaConfiguration\": {
\"mysqlReplicaConfiguration\": {
\"username\": \"$1\",
\"password\": \"$2\",
\"dumpFilePath\": \"$3\",
\"caCertificate\": $(jq --slurp -Rn input < $CERTIFICATE_FILE)
}
},
\"settings\": {
\"tier\": \"${DATABASE_TIER}\",
\"activationPolicy\": \"ALWAYS\"
},
\"databaseVersion\": \"${DATABASE_VERSION}\",
\"masterInstanceName\": \"${EXTERNAL_NAME}\",
\"name\": \"${INTERNAL_NAME}\",
\"region\": \"${REGION}\"
}
"""
}
# create-external-master "${EXTERNAL_NAME}" "${REGION}" "${DATABASE_VERSION}" "${EXTERNAL_HOST}:${EXTERNAL_PORT}"
create-internal-replica "${REPLICATION_USER}" "${REPLICATION_PASSWORD}" "${GCS_DUMP_FILE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment