blob: 33fa5caa421884b7b589f74857ba399b43466b47 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001#!/bin/bash
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01002# 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.
Copybara854996b2021-09-07 19:36:02 +00005
6# The existing replicas all have this prefix:
7REPLICA_PREFIX="replica"
8
9# The new replicas made from the restored primary will have this prefix:
10NEW_REPLICA_PREFIX="replica-1"
11
12CLOUD_PROJECT="monorail-staging"
13
14DRY_RUN=true
15
16echo Restoring backups to primary for ${CLOUD_PROJECT}. Dry run: ${DRY_RUN}
17echo This will delete all read replicas with the prefix "${REPLICA_PREFIX}"
18echo and create a new set of replicas with the prefix "${NEW_REPLICA_PREFIX}"
19echo
20echo Checking for existing read replicas to delete:
21
22EXISTING_REPLICAS=($(gcloud sql instances list --project=${CLOUD_PROJECT} | grep ${REPLICA_PREFIX}- | awk '{print $1}'))
23
24if [ ${#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
29fi
30
31echo Deleting ${#EXISTING_REPLICAS[@]} existing replicas found with the prefix ${REPLICA_PREFIX}
32
33for 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
40done
41
42echo Checking for available backups:
43
44DUE_TIMES=($(gcloud sql backups list --instance primary --project=${CLOUD_PROJECT} | grep SUCCESSFUL | awk '{print $1}'))
45
46for index in ${!DUE_TIMES[*]}; do
47 echo "[${index}] ${DUE_TIMES[${index}]}"
48done
49
50echo "Choose one of the above due_time values."
51echo "NOTE: selecting anything besides 0 will require you to manually"
52echo "complete the rest of the restore process."
53echo "Recover from date [0: ${DUE_TIMES[0]}]:"
54read DUE_TIME_INDEX
55
56DUE_TIME=${DUE_TIMES[${DUE_TIME_INDEX}]}
57
58cmd="gcloud sql backups restore ${DUE_TIME} --project=${CLOUD_PROJECT} --restore-instance=primary"
59echo ${cmd}
60if [ ${DRY_RUN} == false ]; then
61 ${cmd}
62fi
63
64if [ "${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
72fi
73
74echo "Finding restore operation ID..."
75
76RESTORE_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.
79echo Waiting on restore operation ID: ${RESTORE_OP_IDS[0]}
80
81if [ ${DRY_RUN} == false ]; then
82 gcloud sql operations wait ${RESTORE_OP_IDS[0]} --project=${CLOUD_PROJECT}
83fi
84
85echo Restore is finished on primary. Now create the new set of read replicas with the new name prefix ${NEW_REPLICA_PREFIX}:
86
87TIER=($(gcloud sql instances describe primary --project=${CLOUD_PROJECT} | grep tier | awk '{print $2}'))
88
89for 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
95done
96
97echo If the replica creation steps above did not succeed due to authentication
98echo errors, you may need to retry them manually.
99echo
100echo
101echo Backup restore is nearly complete. Check the instances page on developer console to see when
102echo all of the replicas are "Runnable" status. Until then, you may encounter errors in issue search.
103echo In the mean time:
104echo - edit settings.py to change the db_replica_prefix variable to be "${NEW_REPLICA_PREFIX}-"
105echo Then either "make deploy_prod" or "make deploy_staging" for search to pick up the new prefix.
106echo Then set the newly deploy version for besearch and besearch2 on the dev console Versons page.
107echo Follow-up:
108echo - Submit the change.
109echo - Delete old versions of besearch because they run up the GAE bill.