#!/bin/bash
################################################################################
# Sample Cassandra restore snapshot script #
# NOTE: This MUST be adapted for and validated in your environment before use! #
################################################################################ # Replace the xxx values below to match your environment
CASSANDRA_DATA_DIR="xxx"
KEYSPACE_NAME="xxx"
SNAPSHOT_NAME="xxx"
BACKUP_ROOT_DIR="xxx" # Example:
# CASSANDRA_DATA_DIR="/opt/cassandra/data/data"
# KEYSPACE_NAME="x9fa003e2_d975_4a4a_a27e_280ab7fd8a5_group_2"
# SNAPSHOT_NAME="Group2-20181127_2144_28"
# BACKUP_ROOT_DIR="/backup/cassandra-snapshots"
##### Do NOT change anything below this line #####
backupdir="${BACKUP_ROOT_DIR}/${SNAPSHOT_NAME}"
keyspace_path="${CASSANDRA_DATA_DIR}/${KEYSPACE_NAME}"
echo -e "\n\tRestoring tables from directory, '${backupdir}', to directory, '${keyspace_path}'"
echo -e "\tRestore snapshot, '${SNAPSHOT_NAME}', to keyspace, '${KEYSPACE_NAME}'"
read -n 1 -p "Continue (y/n)?" answer
echo -e "\n"
if [ "$answer" != "y" ] && [ "$answer" != "Y" ]; then
exit 1
fi set -e
trap '[ "$?" -eq 0 ] || echo \*\*\* FATAL ERROR \*\*\*' EXIT $? if ! [ -d "${backupdir}" ]; then echo -e "\nERROR: Backup not found at '${backupdir}'";exit 1; fi
if ! [ -d "${keyspace_path}" ]; then echo -e "\nERROR: Keyspace path '${keyspace_path}' is not valid";exit 1; fi
keyspace_tables=$(mktemp)
find "${keyspace_path}/" -maxdepth 1 -mindepth 1 -type d -fprintf ${keyspace_tables} "%f\n"
sort -o ${keyspace_tables} ${keyspace_tables} backup_tablenames=$(mktemp)
find "${backupdir}/" -maxdepth 1 -mindepth 1 -type d -fprintf ${backup_tablenames} "%f\n"
sort -o ${backup_tablenames} ${backup_tablenames} keyspace_tablenames=$(mktemp)
table_names=()
table_uuids=()
while IFS= read -r table
do
str=${table##*/}
uuid=${str##*-}
len=$((${#str} - ${#uuid} - 1))
name=${str:0:${len}}
echo "${name}" >> ${keyspace_tablenames}
table_names+=(${name})
table_uuids+=(${uuid})
done < ${keyspace_tables} set +e
sort -o ${keyspace_tablenames} ${keyspace_tablenames}
diff -a -q ${keyspace_tablenames} ${backup_tablenames}
if [ $? -ne 0 ]; then
echo -e "\nERROR: The tables on the keyspace at, '${keyspace_path}', are not the same as the ones from the backup at,
'${backupdir}'"
exit 1
fi for ((i=0; i<${#table_names[*]}; i++));
do
echo "Restoring table, '${table_names[i]}'"
table_dir="${keyspace_path}/${table_names[i]}-${table_uuids[i]}"
rm -r "${table_dir}"
mkdir "${table_dir}"
src="${backupdir}/${table_names[i]}"
cp -r -a "${src}"/* "${table_dir}/"
done
|