-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add backup and restore script for Vitess tablets
Signed-off-by: Shail Pujan <[email protected]>
- Loading branch information
1 parent
af88cfe
commit 6ae07f5
Showing
2 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/bin/bash | ||
|
||
# Copyright 2019 The Vitess Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# This script demonstrates how to create backups and restore tablets in Vitess. | ||
|
||
# Load common environment variables and functions from the specified file | ||
source ../common/env.sh | ||
|
||
# Set the keyspace and shard variables | ||
KEYSPACE="commerce" # Define the keyspace we will work with, which is 'commerce' | ||
SHARD="0" # Define the shard we will operate on, which is shard '0' | ||
|
||
# Helper function for logging messages with timestamps | ||
log() { | ||
# Print the current date, time, and log message to standard output | ||
echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | ||
} | ||
|
||
# Ensure the keyspace exists and is healthy | ||
log "Ensuring keyspace $KEYSPACE exists and shard $SHARD is healthy..." | ||
# Wait for the specified shard to be healthy. If it is not healthy, exit the script with an error. | ||
wait_for_healthy_shard $KEYSPACE $SHARD || exit 1 | ||
|
||
# Find the replica tablets dynamically | ||
log "Finding replica tablets..." | ||
# Use vtctldclient to get a list of replica tablets in the specified keyspace and shard, | ||
# extracting just the tablet names using awk. | ||
REPLICA_TABLETS=$(vtctldclient GetTablets --keyspace=$KEYSPACE --shard=$SHARD --tablet-type=replica | awk '{print $1}') | ||
# Count the number of replica tablets found | ||
REPLICA_COUNT=$(echo "$REPLICA_TABLETS" | wc -l) | ||
|
||
# Check if any replica tablets were found | ||
if [ "$REPLICA_COUNT" -lt 1 ]; then | ||
log "No replica tablets found. Attempting to temporarily demote primary for restore..." | ||
|
||
# If no replica tablets exist, find the primary tablet | ||
PRIMARY_TABLET=$(vtctldclient GetTablets --keyspace=$KEYSPACE --shard=$SHARD --tablet-type=primary | awk '{print $1}') | ||
log "Demoting primary tablet ($PRIMARY_TABLET) to replica mode for restore..." | ||
|
||
# Temporarily demote the primary tablet to a replica so it can be restored | ||
vtctldclient ChangeTabletType $PRIMARY_TABLET replica || fail "Failed to demote primary to replica" | ||
|
||
# Set the restore tablet to the primary tablet (now demoted) | ||
RESTORE_TABLET=$PRIMARY_TABLET | ||
else | ||
# If there are replicas, select the first replica tablet for the restore process | ||
RESTORE_TABLET=$(echo "$REPLICA_TABLETS" | head -n 1) | ||
fi | ||
|
||
# Restore from the backup on the chosen tablet | ||
log "Restoring tablet $RESTORE_TABLET from backup..." | ||
# Call the RestoreFromBackup command on the selected tablet. If it fails, exit with an error. | ||
vtctldclient RestoreFromBackup $RESTORE_TABLET || fail "Restore failed for tablet $RESTORE_TABLET" | ||
|
||
# Check if the primary tablet was the one that was restored | ||
if [ "$PRIMARY_TABLET" = "$RESTORE_TABLET" ]; then | ||
log "Promoting tablet $PRIMARY_TABLET back to primary..." | ||
# If the restored tablet was the primary, promote it back to primary status | ||
vtctldclient ChangeTabletType $PRIMARY_TABLET primary || fail "Failed to promote replica back to primary" | ||
fi | ||
|
||
# Complete the process with a success message | ||
log "Backup and Restore example completed successfully." |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/bin/bash | ||
|
||
# Copyright 2019 The Vitess Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# We should not assume that any of the steps have been executed. | ||
# This makes it possible for a user to cleanup at any point. | ||
|
||
source ../common/env.sh | ||
|
||
../common/scripts/vtadmin-down.sh | ||
|
||
../common/scripts/vtorc-down.sh | ||
|
||
../common/scripts/vtgate-down.sh | ||
|
||
for tablet in 100 200 300 400 500; do | ||
if vtctldclient --action_timeout 1s --server localhost:15999 GetTablet zone1-$tablet >/dev/null 2>&1; then | ||
# The zero tablet is up. Try to shutdown 0-2 tablet + mysqlctl | ||
for i in 0 1 2; do | ||
uid=$((tablet + i)) | ||
printf -v alias '%s-%010d' 'zone1' $uid | ||
echo "Shutting down tablet $alias" | ||
CELL=zone1 TABLET_UID=$uid ../common/scripts/vttablet-down.sh | ||
# because MySQL takes time to stop, we do this in parallel | ||
CELL=zone1 TABLET_UID=$uid ../common/scripts/mysqlctl-down.sh & | ||
done | ||
|
||
# without a sleep below, we can have the echo happen before the echo of mysqlctl-down.sh | ||
sleep 2 | ||
echo "Waiting mysqlctl to stop..." | ||
wait | ||
echo "mysqlctls are stopped!" | ||
fi | ||
done | ||
|
||
../common/scripts/vtctld-down.sh | ||
|
||
if [ "${TOPO}" = "zk2" ]; then | ||
CELL=zone1 ../common/scripts/zk-down.sh | ||
elif [ "${TOPO}" = "consul" ]; then | ||
CELL=zone1 ../common/scripts/consul-down.sh | ||
else | ||
CELL=zone1 ../common/scripts/etcd-down.sh | ||
fi | ||
|
||
# pedantic check: grep for any remaining processes | ||
|
||
if [ -n "$VTDATAROOT" ]; then | ||
if pgrep -f -l "$VTDATAROOT" >/dev/null; then | ||
echo "ERROR: Stale processes detected! It is recommended to manuallly kill them:" | ||
pgrep -f -l "$VTDATAROOT" | ||
else | ||
echo "All good! It looks like every process has shut down" | ||
fi | ||
|
||
# shellcheck disable=SC2086 | ||
rm -r ${VTDATAROOT:?}/* | ||
fi | ||
|
||
disown -a |