raspiblitz/home.admin/XXcopyStation.sh

297 lines
9.5 KiB
Bash
Raw Normal View History

2019-05-18 21:25:46 +02:00
#!/bin/bash
# command info
2019-05-18 21:58:30 +02:00
if [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
2019-05-18 21:25:46 +02:00
echo "# Turns the RaspiBlitz into HDD CopyStation Mode"
echo "# lightning is deactivated during CopyStationMode"
echo "# reboot RaspiBlitz to set back to normal mode"
exit 1
fi
####### CONFIG #############
# where to find the BITCOIN data directory (no trailing /)
pathBitcoinBlockchain="/mnt/hdd/bitcoin"
# where to find the RaspiBlitz HDD template directory (no trailing /)
2020-02-24 18:38:57 +01:00
pathTemplateHDD="/mnt/hdd/app-storage/templateHDD"
2019-05-18 21:25:46 +02:00
####### SCRIPT #############
# check sudo
if [ "$EUID" -ne 0 ]; then
echo "Please run as root (with sudo)"
exit 1
fi
2020-02-24 18:40:25 +01:00
# get HDD info
source <(sudo /home/admin/config.scripts/blitz.datadrive.sh status)
# check if HDD is mounted
if [ ${isMounted} -eq 0 ]; then
2020-02-24 18:43:03 +01:00
echo "error='HDD is not mounted'"
2020-02-24 18:40:25 +01:00
exit 1
fi
# check if HDD is big enough
if [ ${hddGigaBytes} -lt 800 ]; then
2020-02-24 18:43:03 +01:00
echo "# To run copy station (+/- 1TB needed)"
echo "error='HDD is too small'"
2020-02-24 18:40:25 +01:00
exit 1
fi
2020-02-24 18:43:03 +01:00
# check that path information is valid
if [ -d "$pathBitcoinBlockchain" ]; then
echo "# OK found $pathBitcoinBlockchain"
else
echo "# FAIL path of 'pathBitcoinBlockchain' does not exists: ${pathBitcoinBlockchain}"
2021-10-25 11:31:12 +02:00
echo "error='pathBitcoinBlockchain not found'"
2020-02-24 18:43:03 +01:00
exit 1
fi
2020-02-24 18:40:25 +01:00
2019-05-18 21:25:46 +02:00
# make sure that its running in screen
# call with '-foreground' to prevent running in screen
if [ "$1" != "-foreground" ]; then
screenPID=$(screen -ls | grep "copystation" | cut -d "." -f1 | xargs)
if [ ${#screenPID} -eq 0 ]; then
2021-08-27 09:59:21 +02:00
# start copystation in screen
2020-02-24 23:09:42 +01:00
echo "# starting copystation screen session"
screen -S copystation -dm /home/admin/XXcopyStation.sh -foreground
screen -d -r
2019-05-18 23:37:34 +02:00
exit 0
2019-05-18 21:25:46 +02:00
else
2020-02-24 23:09:42 +01:00
echo "# changing into running copystation screen session"
2019-05-19 01:46:52 +02:00
screen -d -r
2019-05-18 23:37:34 +02:00
exit 0
2019-05-18 21:25:46 +02:00
fi
fi
clear
2020-02-24 23:09:42 +01:00
echo "# ******************************"
echo "# RASPIBLITZ COPYSTATION SCRIPT"
echo "# ******************************"
2019-05-18 21:25:46 +02:00
echo
2021-10-25 11:31:12 +02:00
echo "Make sure that no target HDD/SSDs are not connected yet .."
echo
sudo sed -i "s/^state=.*/state=copystation/g" /home/admin/raspiblitz.info 2>/dev/null
sleep 10
2019-05-18 21:25:46 +02:00
echo "*** CHECKING CONFIG"
# check that path information is valid
if [ -d "$pathTemplateHDD" ]; then
2020-02-24 23:09:42 +01:00
echo "# OK found $pathTemplateHDD"
2019-05-18 21:25:46 +02:00
else
2020-02-24 23:09:42 +01:00
echo "# Creating: ${pathTemplateHDD}"
2019-05-18 21:25:46 +02:00
mkdir ${pathTemplateHDD}
chmod 777 ${pathTemplateHDD}
fi
2021-08-27 09:59:21 +02:00
# make sure that lnd is stopped (if running)
2019-05-18 21:25:46 +02:00
systemctl stop lnd 2>/dev/null
systemctl stop background 2>/dev/null
# finding system drives (the drives that should not be synced to)
2020-02-24 23:09:42 +01:00
echo "# OK - the following drives detected as the system drive: $datadisk"
2019-05-18 21:25:46 +02:00
echo
# BASIC IDEA:
# 1. get fresh data from bitcoind --> template data
# 2. detect HDDs
# 3. sync HDDs with template data
# repeat
echo
echo "*** RUNNING ***"
lastBlockchainUpdateTimestamp=1
2021-10-25 11:31:12 +02:00
firstLoop=1
2019-05-18 21:25:46 +02:00
while :
do
2021-10-25 11:31:12 +02:00
# reset external data stores (needed because local vars dont work in deeper while loops)
echo "" > /var/cache/raspiblitz/copystationHddsInfoString.tmp
rm /var/cache/raspiblitz/copystationFoundTargets.flag
2019-05-18 21:25:46 +02:00
################################################
2021-10-25 11:31:12 +02:00
# 1. get fresh data from bitcoind for template data (skip on first loop)
2019-05-18 21:25:46 +02:00
# only execute every 30min
nowTimestamp=$(date +%s)
secondsDiff=$(echo "${nowTimestamp}-${lastBlockchainUpdateTimestamp}" | bc)
2020-02-24 23:09:42 +01:00
echo "# seconds since last update from bitcoind: ${secondsDiff}"
2019-05-18 21:25:46 +02:00
echo
if [ ${secondsDiff} -gt 3000 ]; then
echo "******************************"
echo "Bitcoin Blockchain Update"
echo "******************************"
# stop blockchains
2020-02-24 23:09:42 +01:00
echo "# Stopping Blockchain ..."
2019-05-18 21:25:46 +02:00
systemctl stop bitcoind 2>/dev/null
sleep 10
# sync bitcoin
2020-02-24 23:09:42 +01:00
echo "# Syncing Bitcoin to template folder ..."
2019-05-18 21:25:46 +02:00
2021-10-25 11:31:12 +02:00
sed -i "s/^message=.*/message='Updating Template: Bitcoin'/g" /home/admin/raspiblitz.info
2019-05-18 23:19:10 +02:00
2019-05-18 21:25:46 +02:00
# make sure the bitcoin directory in template folder exists
if [ ! -d "$pathTemplateHDD/bitcoin" ]; then
2020-02-24 23:09:42 +01:00
echo "# creating the bitcoin subfolder in the template folder"
2019-05-18 21:25:46 +02:00
mkdir ${pathTemplateHDD}/bitcoin
chmod 777 ${pathTemplateHDD}/bitcoin
fi
2020-02-24 23:09:42 +01:00
# do the sync to the template folder for BITCOIN
2020-02-28 21:40:05 +01:00
rsync -a --info=progress2 --delete ${pathBitcoinBlockchain}/chainstate ${pathBitcoinBlockchain}/blocks ${pathTemplateHDD}/bitcoin
2019-05-18 21:25:46 +02:00
# restart bitcoind (to let further setup while syncing HDDs)
2020-02-24 23:09:42 +01:00
echo "# Restarting Blockchain ..."
2019-05-18 21:25:46 +02:00
systemctl start bitcoind 2>/dev/null
# update timer
lastBlockchainUpdateTimestamp=$(date +%s)
fi
################################################
# 2. detect connected HDDs and loop thru them
2020-02-28 23:07:44 +01:00
echo
echo "**************************************"
2020-02-28 23:13:18 +01:00
echo "SYNCING TEMPLATE -> CONNECTED HDD/SSDs"
2020-02-28 23:07:44 +01:00
echo "**************************************"
2021-10-25 11:31:12 +02:00
sleep 2
lsblk -o NAME | grep "^[s|v]d" | while read -r detectedDrive ; do
2020-02-24 23:09:42 +01:00
isSystemDrive=$(echo "${datadisk}" | grep -c "${detectedDrive}")
2019-05-18 21:25:46 +02:00
if [ ${isSystemDrive} -eq 0 ]; then
2021-10-25 11:31:12 +02:00
# remember that disks were found
touch /var/cache/raspiblitz/copystationFoundTargets.flag
2019-05-18 21:25:46 +02:00
# check if drives 1st partition is named BLOCKCHAIN & in EXT4 format
isNamedBlockchain=$(lsblk -o NAME,FSTYPE,LABEL | grep "${detectedDrive}" | grep -c "BLOCKCHAIN")
isFormatExt4=$(lsblk -o NAME,FSTYPE,LABEL | grep "${detectedDrive}" | grep -c "ext4")
# init a fresh device
if [ ${isNamedBlockchain} -eq 0 ] || [ ${isFormatExt4} -eq 0 ]; then
2021-10-25 11:31:12 +02:00
echo
echo "**************************************************************"
echo "*** NEW EMPTY HDD FOUND ---> ${detectedDrive}"
2019-05-18 21:25:46 +02:00
echo "isNamedBlockchain: ${isNamedBlockchain}"
echo "isFormatExt4:" ${isFormatExt4}
# check if size is OK
size=$(lsblk -o NAME,SIZE -b | grep "^${detectedDrive}" | awk '$1=$1' | cut -d " " -f 2)
echo "size: ${size}"
2021-10-25 11:31:12 +02:00
if [ ${size} -lt 900000000000 ]; then
echo "!! THE HDD/SSD IS TOO SMALL <900GB - use at least 1TB"
sed -i "s/^message=.*/message='HDD smaller than 1TB: ${detectedDrive}'/g" /home/admin/raspiblitz.info
echo
sleep 10
2019-05-18 21:25:46 +02:00
else
2021-10-25 11:31:12 +02:00
choice=0
sed -i "s/^message=.*/message='Formatting new HDD: ${detectedDrive}'/g" /home/admin/raspiblitz.info
2019-05-18 21:25:46 +02:00
2020-02-24 23:09:42 +01:00
# format the HDD
echo "Starting Formatting of device ${detectedDrive} ..."
2021-10-25 11:31:12 +02:00
/home/admin/config.scripts/blitz.datadrive.sh format ext4 ${detectedDrive}
sleep 4
2020-02-24 23:09:42 +01:00
2019-05-18 21:25:46 +02:00
fi
2021-10-25 11:31:12 +02:00
else
echo
echo "*** ALREADY ACTIVE HDD FOUND ---> ${detectedDrive}"
sleep 1
fi
2019-05-18 21:25:46 +02:00
################################################
2021-10-25 11:31:12 +02:00
# 3. sync HDD with template data (skip on first loop)
2019-05-18 21:25:46 +02:00
partition=$(lsblk -o NAME,FSTYPE,LABEL | grep "${detectedDrive}" | grep "BLOCKCHAIN" | cut -d ' ' -f 1 | tr -cd "[:alnum:]")
2021-10-25 11:31:12 +02:00
if [ "${firstLoop}" != "1" ] && [ ${#partition} -gt 0 ]; then
2019-05-18 21:25:46 +02:00
# temp mount device
echo "mounting: ${partition}"
mkdir /mnt/hdd2 2>/dev/null
2021-10-25 11:31:12 +02:00
mount -t ext4 /dev/${partition} /mnt/hdd2
2019-05-18 21:25:46 +02:00
# rsync device
mountOK=$(lsblk -o NAME,MOUNTPOINT | grep "${detectedDrive}" | grep -c "/mnt/hdd2")
if [ ${mountOK} -eq 1 ]; then
2021-10-25 11:31:12 +02:00
hddsInfoString=$(cat /var/cache/raspiblitz/copystationHddsInfoString.tmp | tr '\n' ' ')
sed -i "s/^message=.*/message='${hddsInfoString} ${partition}>SYNC'/g" /home/admin/raspiblitz.info
2020-02-28 21:40:05 +01:00
rsync -a --info=progress2 --delete ${pathTemplateHDD}/* /mnt/hdd2
2021-10-25 11:31:12 +02:00
chmod -R 777 /mnt/hdd2
2019-05-20 01:05:57 +02:00
rm -r /mnt/hdd2/lost+found 2>/dev/null
2021-10-25 11:31:12 +02:00
echo "${partition}>OK" >> /var/cache/raspiblitz/copystationHddsInfoString.tmp
2019-05-18 21:25:46 +02:00
else
2020-02-24 23:09:42 +01:00
echo "# FAIL: was not able to mount --> ${partition}"
2019-05-18 21:25:46 +02:00
fi
# unmount device
2021-10-25 11:31:12 +02:00
umount -l /mnt/hdd2
2019-05-18 21:25:46 +02:00
fi
fi
done
2021-10-25 11:31:12 +02:00
# check for flag
foundTargets=$(ls /var/cache/raspiblitz/copystationFoundTargets.flag 2>/dev/null | grep -c "copystationFoundTargets.flag")
hddsInfoString=$(cat /var/cache/raspiblitz/copystationHddsInfoString.tmp | tr '\n' ' ')
2019-05-18 21:25:46 +02:00
clear
2021-10-25 11:31:12 +02:00
if [ "${foundTargets}" == "1" ] && [ "${firstLoop}" == "1" ]; then
# after script found discs and did formatting ... go into full loop
echo "OK first loop done ..."
firstLoop=0
sleep 1
elif [ "${foundTargets}" == "0" ]; then
echo "**** NO TARGET HDD/SSDs CONNECTED ****"
echo
echo "Best way to start a new batch:"
echo "- Disconnect powered USB-Hub (best unplug USB cable at USB-Hub)"
echo "- Connect all HDD/SSDs to the disconnected USB-Hub"
echo "- Connect powered USB-Hub to Blitz (plug USB cable in)"
echo "- During formatting remember names of physical HDD/SSDs"
echo "- As soon as you see an OK for that HDD/SSD name you can remove it"
echo
echo "Next round starts in 30 seconds ..."
echo "To stop copystation script: CTRL+c and then 'restart'"
echo "You can close SSH terminal and script will run in background can can be re-entered."
sed -i "s/^message=.*/message='No target HDDs connected - connect USB Hub'/g" /home/admin/raspiblitz.info
firstLoop=1
sleep 30
else
2019-05-18 21:25:46 +02:00
2021-10-25 11:31:12 +02:00
echo "**** SYNC LOOP DONE ****"
echo "HDDs ready synced:"
cat /var/cache/raspiblitz/copystationHddsInfoString.tmp
echo
echo "*************************"
echo
echo "Next round starts in 25 seconds ..."
echo "To stop copystation script: CTRL+c and then 'restart'"
echo "You can close SSH terminal and script will run in background can can be re-entered."
2019-05-18 21:25:46 +02:00
2021-10-25 11:31:12 +02:00
sed -i "s/^message=.*/message='Ready HDDs: ${hddsInfoString}'/g" /home/admin/raspiblitz.info
sleep 25
fi
2019-05-18 21:25:46 +02:00
clear
echo "starting new sync loop"
sleep 5
2021-10-25 11:31:12 +02:00
done