Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the initial script for the 40x example for backup and restore #16893

Merged
merged 16 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions examples/local/101_backup_restore.sh
frouioui marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/bin/bash

# Copyright 2024 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 is part of the 101 example series and demonstrates how to create backups and restore tablets in Vitess.

source ../common/env.sh

# Set the keyspace and shard variables
KEYSPACE="commerce"
SHARD="0"
SIDECAR_DB_NAME="_vt"
TABLETS="100 101 102"

# Helper function for logging
log() {
echo "$(date +'%Y-%m-%d %H:%M:%S') - $1"
}

# Start the topo server
log "Starting Topo server..."
if [ "${TOPO}" = "zk2" ]; then
CELL=zone1 ../common/scripts/zk-up.sh
elif [ "${TOPO}" = "consul" ]; then
CELL=zone1 ../common/scripts/consul-up.sh
else
CELL=zone1 ../common/scripts/etcd-up.sh
fi

# Start vtctld
log "Starting vtctld..."
CELL=zone1 ../common/scripts/vtctld-up.sh

# Initialize the keyspace for backup/restore
if vtctldclient GetKeyspace $KEYSPACE > /dev/null 2>&1 ; then
# Keyspace exists
log "Keyspace $KEYSPACE already exists. Setting durability policy..."
vtctldclient SetKeyspaceDurabilityPolicy --durability-policy=semi_sync $KEYSPACE || fail "Failed to set durability policy"
else
# Create the keyspace
log "Creating keyspace $KEYSPACE with durability policy semi_sync..."
vtctldclient CreateKeyspace --sidecar-db-name="$SIDECAR_DB_NAME" --durability-policy=semi_sync $KEYSPACE || fail "Failed to create keyspace"
fi

# Start mysqlctl for the tablets
log "Starting MySQL instances..."
for tablet in $TABLETS; do
CELL=zone1 TABLET_UID=$tablet ../common/scripts/mysqlctl-up.sh &
done
sleep 2
wait
log "MySQL instances started."

# Start vttablets for each tablet in the keyspace
log "Starting vttablets..."
for tablet in $TABLETS; do
CELL=zone1 KEYSPACE=$KEYSPACE TABLET_UID=$tablet ../common/scripts/vttablet-up.sh
done

# Wait for the primary tablet to be elected and healthy
log "Waiting for a healthy primary tablet..."
wait_for_healthy_shard $KEYSPACE $SHARD || exit 1

# Apply schema to the keyspace
log "Applying schema to keyspace $KEYSPACE..."
vtctldclient ApplySchema --sql-file create_commerce_schema.sql $KEYSPACE || fail "Failed to apply schema"

# Apply vschema to the keyspace
log "Applying vschema to keyspace $KEYSPACE..."
vtctldclient ApplyVSchema --vschema-file vschema_commerce_initial.json $KEYSPACE || fail "Failed to apply vschema"

# New backup logic
log "Finding a replica tablet to backup..."
REPLICA_TABLET=$(vtctldclient GetTablet zone1-0000000101 | grep 'type: REPLICA' > /dev/null && echo "zone1-0000000101" || echo "zone1-0000000102")

if [ -z "$REPLICA_TABLET" ]; then
log "No replica tablet found. Attempting to backup primary with --allow_primary flag..."
vtctldclient Backup --allow_primary zone1-0000000100 || fail "Backup failed for primary tablet"
else
log "Backing up replica tablet ($REPLICA_TABLET)..."
vtctldclient Backup $REPLICA_TABLET || fail "Backup failed for replica tablet"
fi

# Restore another tablet (tablet 101) from the backup
RESTORE_TABLET="zone1-0000000102"
if [ "$RESTORE_TABLET" = "$REPLICA_TABLET" ]; then
RESTORE_TABLET="zone1-0000000101"
fi

log "Restoring tablet $RESTORE_TABLET from backup..."
vtctldclient RestoreFromBackup $RESTORE_TABLET || fail "Restore failed for tablet $RESTORE_TABLET"

# Verify the backup exists
log "Verifying backups exist for keyspace $KEYSPACE, shard $SHARD..."
vtctldclient GetBackups $KEYSPACE/$SHARD || fail "No backups found"

# Complete
log "Backup and Restore example completed successfully."
75 changes: 75 additions & 0 deletions examples/local/401_backup_restore.sh
frouioui marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash

# Copyright 2019 The Vitess Authors.
frouioui marked this conversation as resolved.
Show resolved Hide resolved
#
# 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."
72 changes: 72 additions & 0 deletions examples/local/501_teardown.sh
mattlord marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/bash

# Copyright 2019 The Vitess Authors.
frouioui marked this conversation as resolved.
Show resolved Hide resolved
#
# 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
Loading