Files
purple-explorer/scripts/sync.sh
T

161 lines
4.5 KiB
Bash
Raw Normal View History

2020-12-04 10:59:58 -07:00
#!/bin/bash
readonly EXPLORER_PATH=$(dirname $(dirname $(readlink -f "$0")))
NODE_PATH=""
MODE=""
# Check if parameters were passed into the script
if [ -n "${1}" ]; then
# At least one parameter has been passed in
case ${1} in
"update")
# Index update
MODE="index update"
;;
"check")
# Index check
MODE="index check"
# Check if the next parameter exists
if [ -n "${2}" ]; then
# Add onto the check cmd
MODE="${MODE} ${2}"
fi
2020-12-04 10:59:58 -07:00
;;
"reindex")
# Index reindex
MODE="index reindex"
;;
"reindex-rich")
# Index reindex-rich
MODE="index reindex-rich"
;;
"reindex-txcount")
# Index reindex-txcount
MODE="index reindex-txcount"
;;
2021-01-24 15:26:17 -07:00
"reindex-last")
# Index reindex-last
MODE="index reindex-last"
;;
2020-12-04 10:59:58 -07:00
"market")
# Market update
MODE="market"
;;
"peers")
# Peers update
MODE="peers"
;;
2020-12-30 18:22:02 -07:00
"masternodes")
# Masternodes update
MODE="masternodes"
;;
2020-12-04 10:59:58 -07:00
*)
# Check if this is a file that exists on the filesystem
if [ -f ${1} ]; then
# The file exists. Assume this is the path to node
NODE_PATH="${1}"
fi
;;
esac
# Check if the mode is already set
if [ -z "${MODE}" ]; then
# Mode is not set so check if the next parameter exists
if [ -n "${2}" ]; then
# Determine which mode this last parameter is
case ${2} in
"update")
# Index update
MODE="index update"
;;
"check")
# Index check
MODE="index check"
# Check if the next parameter exists
if [ -n "${3}" ]; then
# Add onto the check cmd
MODE="${MODE} ${3}"
fi
2020-12-04 10:59:58 -07:00
;;
"reindex")
# Index reindex
MODE="index reindex"
;;
"reindex-rich")
# Index reindex-rich
MODE="index reindex-rich"
;;
"reindex-txcount")
# Index reindex-txcount
MODE="index reindex-txcount"
;;
2021-01-24 15:26:17 -07:00
"reindex-last")
# Index reindex-last
MODE="index reindex-last"
;;
2020-12-04 10:59:58 -07:00
"market")
# Market update
MODE="market"
;;
"peers")
# Peers update
MODE="peers"
2020-12-30 18:22:02 -07:00
;;
"masternodes")
# Masternodes update
MODE="masternodes"
;;
2020-12-04 10:59:58 -07:00
esac
elif [ -n "${NODE_PATH}" ]; then
# Node path was specified but no mode, so default to 'index update' mode
MODE="index update"
fi
fi
else
# No parameters specified so default to 'index update' mode
MODE="index update"
fi
# Check if the mode is set
if [ -n "${MODE}" ]; then
# Mode is set
# Check if the desired mode requires a lock
2020-12-30 18:22:02 -07:00
if [ "${MODE}" != "peers" ] && [ "${MODE}" != "masternodes" ]; then
2020-12-04 10:59:58 -07:00
# A lock is required
2022-03-19 18:44:36 -06:00
# Check if this is a reindex
if [ "${MODE}" = "index reindex" ]; then
# Prompt for the reindex
echo "You are about to delete all blockchain data (transactions and addresses)"
echo "and resync from the genesis block."
echo "Are you sure you want to do this? [y/n]: ";
read -p "" REINDEX_ANSWER
# Determine if the reindex should proceed
case "$REINDEX_ANSWER" in
y|Y|yes|Yes|YES) ;;
*) echo "Process aborted. Nothing was deleted." && exit ;;
esac
fi
2020-12-04 10:59:58 -07:00
# Check if the script is already running (tmp/index.pid file already exists)
if [ -f "${EXPLORER_PATH}/tmp/index.pid" ]; then
# The tmp/index.pid file exists. Check if the process is actually still running
ps -p `cat ${EXPLORER_PATH}/tmp/index.pid` > /dev/null
if [ $? -eq 0 ]; then
# Script is running so the data is locked and we must exit now and try again later
echo "Script already running.."
exit 1
else
# Script is not actually running so we can delete the lock file
rm "${EXPLORER_PATH}/tmp/index.pid"
fi
fi
fi
# Check if the node path was specified
if [ -z "${NODE_PATH}" ]; then
# Node path not specified so lookup using the 'which' cmd
eval "cd ${EXPLORER_PATH} && $(which node) scripts/sync.js ${MODE}"
else
# Node path specified
eval "cd ${EXPLORER_PATH} && ${NODE_PATH} scripts/sync.js ${MODE}"
fi
else
# Mode not set so load the sync script without specifying the mode to return the usage options
eval "cd ${EXPLORER_PATH} && $(which node) scripts/sync.js"
fi