Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | #!/bin/bash |
Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 2 | # Copyright 2016 The Chromium Authors |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 5 | |
| 6 | # The existing replicas all have this prefix: |
| 7 | REPLICA_PREFIX="replica" |
| 8 | |
| 9 | # The new replicas made from the restored primary will have this prefix: |
| 10 | NEW_REPLICA_PREFIX="replica-1" |
| 11 | |
| 12 | CLOUD_PROJECT="monorail-staging" |
| 13 | |
| 14 | DRY_RUN=true |
| 15 | |
| 16 | echo Restoring backups to primary for ${CLOUD_PROJECT}. Dry run: ${DRY_RUN} |
| 17 | echo This will delete all read replicas with the prefix "${REPLICA_PREFIX}" |
| 18 | echo and create a new set of replicas with the prefix "${NEW_REPLICA_PREFIX}" |
| 19 | echo |
| 20 | echo Checking for existing read replicas to delete: |
| 21 | |
| 22 | EXISTING_REPLICAS=($(gcloud sql instances list --project=${CLOUD_PROJECT} | grep ${REPLICA_PREFIX}- | awk '{print $1}')) |
| 23 | |
| 24 | if [ ${#EXISTING_REPLICAS[@]} -eq 0 ]; then |
| 25 | echo No replicas found with prefix ${REPLICA_PREFIX} |
| 26 | echo List instances to find the replica prefix by running: |
| 27 | echo gcloud sql instances list --project=${CLOUD_PROJECT} |
| 28 | exit 1 |
| 29 | fi |
| 30 | |
| 31 | echo Deleting ${#EXISTING_REPLICAS[@]} existing replicas found with the prefix ${REPLICA_PREFIX} |
| 32 | |
| 33 | for r in "${EXISTING_REPLICAS[@]}"; do |
| 34 | echo Deleting ${r} |
| 35 | cmd="gcloud sql instances delete ${r} --project=${CLOUD_PROJECT}" |
| 36 | echo ${cmd} |
| 37 | if [ ${DRY_RUN} == false ]; then |
| 38 | ${cmd} |
| 39 | fi |
| 40 | done |
| 41 | |
| 42 | echo Checking for available backups: |
| 43 | |
| 44 | DUE_TIMES=($(gcloud sql backups list --instance primary --project=${CLOUD_PROJECT} | grep SUCCESSFUL | awk '{print $1}')) |
| 45 | |
| 46 | for index in ${!DUE_TIMES[*]}; do |
| 47 | echo "[${index}] ${DUE_TIMES[${index}]}" |
| 48 | done |
| 49 | |
| 50 | echo "Choose one of the above due_time values." |
| 51 | echo "NOTE: selecting anything besides 0 will require you to manually" |
| 52 | echo "complete the rest of the restore process." |
| 53 | echo "Recover from date [0: ${DUE_TIMES[0]}]:" |
| 54 | read DUE_TIME_INDEX |
| 55 | |
| 56 | DUE_TIME=${DUE_TIMES[${DUE_TIME_INDEX}]} |
| 57 | |
| 58 | cmd="gcloud sql backups restore ${DUE_TIME} --project=${CLOUD_PROJECT} --restore-instance=primary" |
| 59 | echo ${cmd} |
| 60 | if [ ${DRY_RUN} == false ]; then |
| 61 | ${cmd} |
| 62 | fi |
| 63 | |
| 64 | if [ "${DUE_TIME_INDEX}" -ne "0" ]; then |
| 65 | echo "You've restored an older-than-latest backup. Please contact speckle-oncall@" |
| 66 | echo "to request an on-demand backup of the primary before attempting to restart replicas," |
| 67 | echo "which this script does not do automatically in this case." |
| 68 | echo "run 'gcloud sql instances create' commands to create new replicas manually after" |
| 69 | echo "you have confirmed with speckle-oncall@ the on-demand backup is complete." |
| 70 | echo "Exiting" |
| 71 | exit 0 |
| 72 | fi |
| 73 | |
| 74 | echo "Finding restore operation ID..." |
| 75 | |
| 76 | RESTORE_OP_IDS=($(gcloud sql operations list --instance=primary --project=${CLOUD_PROJECT} | grep RESTORE_VOLUME | awk '{print $1}')) |
| 77 | |
| 78 | # Assume the fist RESTORE_VOLUME is the operation we want; they're listed in reverse chronological order. |
| 79 | echo Waiting on restore operation ID: ${RESTORE_OP_IDS[0]} |
| 80 | |
| 81 | if [ ${DRY_RUN} == false ]; then |
| 82 | gcloud sql operations wait ${RESTORE_OP_IDS[0]} --project=${CLOUD_PROJECT} |
| 83 | fi |
| 84 | |
| 85 | echo Restore is finished on primary. Now create the new set of read replicas with the new name prefix ${NEW_REPLICA_PREFIX}: |
| 86 | |
| 87 | TIER=($(gcloud sql instances describe primary --project=${CLOUD_PROJECT} | grep tier | awk '{print $2}')) |
| 88 | |
| 89 | for i in {00..09}; do |
| 90 | cmd="gcloud sql instances create ${NEW_REPLICA_PREFIX}-${i} --master-instance-name=primary --project=${CLOUD_PROJECT} --tier=${TIER} --region=us-central1" |
| 91 | echo ${cmd} |
| 92 | if [ ${DRY_RUN} == false ]; then |
| 93 | ${cmd} |
| 94 | fi |
| 95 | done |
| 96 | |
| 97 | echo If the replica creation steps above did not succeed due to authentication |
| 98 | echo errors, you may need to retry them manually. |
| 99 | echo |
| 100 | echo |
| 101 | echo Backup restore is nearly complete. Check the instances page on developer console to see when |
| 102 | echo all of the replicas are "Runnable" status. Until then, you may encounter errors in issue search. |
| 103 | echo In the mean time: |
| 104 | echo - edit settings.py to change the db_replica_prefix variable to be "${NEW_REPLICA_PREFIX}-" |
| 105 | echo Then either "make deploy_prod" or "make deploy_staging" for search to pick up the new prefix. |
| 106 | echo Then set the newly deploy version for besearch and besearch2 on the dev console Versons page. |
| 107 | echo Follow-up: |
| 108 | echo - Submit the change. |
| 109 | echo - Delete old versions of besearch because they run up the GAE bill. |