Bash script to help with using write-hashtbl. Use “-h” for help.
Simply, you provide it the path to your hashstore, and it will walk you through each store on each satellite, optionally rebuilding one or all of them, removing 0 byte files - no batch mode.
#!/bin/bash
SATELLITES=("121RTSDpyNZVcEU84Ticf2L1ntiuUimbWgfATz21tuvgk3vzoA6" "12EayRS2V1kEsWESU9QMRseFhdxYxKicsiFmxrsLZHeLUtdps3S" "12L9ZFwhzVpuEKMUNUqkaTLGzwY9G24tbiigLiXpmZWKwmcNDDs" "1wFTAgs9DP5RSnCqKV1eLf6N9wtk4EAtmN5DpSxcs8EjT69tGE")
STORES=("s0" "s1")
DEFAULT_DIR="/TestStorj/config/storage/hashstore"
# Store Command line argument
STORAGE_DIR=${1:-$DEFAULT_DIR}
WRITE_FLAGS="${@:2}"
function ShowHelpExit() {
echo -e "\nrebuildHash.sh <HASHSTORE_PATH> <WRITE_HASHTBL>"
echo -e "\n <HASHSTORE_PATH> Path to Hashtables. eg: /mnt/hdd/config/storage/hashtable"
echo -e " <WRITE_HASHTBL> Flags to parse to 'write_hashtbl' command"
echo -e "\033[24G Flags:"
echo -e "\033[28G -f, --fast Skip some checks for faster processing"
echo -e "\033[28G -s, --slots uint64 logSlots to use instead of counting"
echo -e "\033[28G -k, --kind string Kind of table to write\n"
exit 1
}
function PromptYesNo() {
read -p "Do you want to proceed? (y/n/CtrlC): " response
case "$response" in
[yY]|[yY][eE][sS])
echo "Proceeding..."
return 0
;;
[nN]|[nN][oO])
echo "No. Exiting..."
return 1
;;
*)
echo "No. Exiting"
return 1
;;
esac
return 1
}
function FindInPath() {
IFS=':' read -ra PATH_DIRS <<< "$PATH"
local FOUND=0
local FILENAME="${1}"
for DIR in "${PATH_DIRS[@]}"
do
FILE_PATH="$DIR/$FILENAME"
if [ -f "$FILE_PATH" ]; then
echo "File '$FILENAME' found at: $FILE_PATH"
return 0
fi
done
return 1
}
function RecreateHashtable() {
if [ "$1" == "" ]; then
echo "Failed. No Directory"
return 2
fi
local location="${1}"
echo "ReCreate Hashtable: ${location}"
cd "${location}"
if [ -f "${location}/meta/lock" ]; then
echo "lock File exists. Ensure StorageNode is NOT running."
echo "Corruption may occur if hashtables are recreated whilst Node is running"
echo "Ensure StorageNode is stopped before proceeding"
echo ""
return 2
fi
# 0 Byte Hashtable is useless, just remove them
find ./meta -type f -size 0c -delete
# Find 0B Files, Give option to delete them.
local noFiles=$(eval "find . -type f -size 0c | wc -l")
if [[ "$noFiles" -gt 0 ]]; then
echo "Found ${noFiles} NULL Files - Remove?"
find . -type f -size 0c
if PromptYesNo; then
echo "Deleting NULL Files"
find . -type f -size 0c -delete
else
echo "Not Deleting NULL Files"
fi
fi
# Option to Backup existing hashtable
#
echo "Checkup for Backup"
if [ -d "${location}/meta.bak" ]; then
echo "Remove Previous hashtable Backup"
if PromptYesNo; then
echo "Removing Previous Backup"
rm -f meta.bak/*
echo "Backup Existing meta Data"
mv meta/* meta.bak/
else
# Backup contents Contents of "meta" directory
echo "Removing Content of 'meta' directory"
mv -f meta/hashtbl* meta.bak/
fi
else
# No Previous Backup, Create Backup Automatically
echo "Backup Existing meta Data"
mkdir meta.bak
mv -f meta/* meta.bak/
fi
cd meta
echo "Running: write-hashtbl ${WRITE_FLAGS} ${location}"
write-hashtbl ${WRITE_FLAGS} ${location}
return $?
}
# Check for Help Option
if [[ "$1" == "-h" ]]; then
ShowHelpExit
fi
# Check we can find write-hashtbl binary.
#
export PATH=$PATH:~/go/bin
FindInPath write-hashtbl
RESULT=$?
if [[ "$RESULT" -eq 1 ]]; then
echo "Binary NOT Found. Please ensure 'write-hashtbl' is in PATH and executable"
exit 1
fi
# Check Storage Directory Exists
#
if [ ! -d $STORAGE_DIR ]; then
echo "Directory Does not Exist\n"
showHelpExit
fi
# Iterate through all Satellites and Stores
#
storeCount=0
for item in "${SATELLITES[@]}"
do
for store in "${STORES[@]}"
do
((storeCount++))
currentDir="${STORAGE_DIR}/${item}/${store}"
echo ""
if [ -d $currentDir ]; then
echo "Rebuild Hashstore: ${item}/${store} - #${storeCount}"
if PromptYesNo; then
RecreateHashtable ${currentDir}
RESULT=$?
if [[ "${RESULT}" -eq 2 ]]; then
if ! PromptYesNo; then
exit 1
fi
fi
else
echo "Skipping ${item}/${store}"
fi
else
echo -e "WARNING: Logfile Directory ${currentDir} Does NOT Exist\n"
fi
done
done
exit 0